001package com.hfg.xml.msofficexml.part;
002
003import java.time.ZoneOffset;
004import java.time.ZonedDateTime;
005import java.util.Date;
006
007import com.hfg.datetime.DateUtil;
008import com.hfg.xml.XMLSchema;
009import com.hfg.xml.XMLTag;
010import com.hfg.xml.msofficexml.OfficeOpenXmlDocument;
011import com.hfg.xml.msofficexml.OfficeXML;
012
013//------------------------------------------------------------------------------
014/**
015 Represents an Office Open XML document properties part.
016
017 @author J. Alex Taylor, hairyfatguy.com
018 */
019//------------------------------------------------------------------------------
020// com.hfg XML/HTML Coding Library
021//
022// This library is free software; you can redistribute it and/or
023// modify it under the terms of the GNU Lesser General Public
024// License as published by the Free Software Foundation; either
025// version 2.1 of the License, or (at your option) any later version.
026//
027// This library is distributed in the hope that it will be useful,
028// but WITHOUT ANY WARRANTY; without even the implied warranty of
029// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
030// Lesser General Public License for more details.
031//
032// You should have received a copy of the GNU Lesser General Public
033// License along with this library; if not, write to the Free Software
034// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
035//
036// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
037// jataylor@hairyfatguy.com
038//------------------------------------------------------------------------------
039/*
040Ex from microsoft.com/en-us/library/office/aa338205(v=office.12).aspx#office2007aboutnewfileformat_structureoftheofficexmlformats
041
042<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
043<CoreProperties xmlns="http://schemas.microsoft.com/package/2005/06/md/core-properties">
044   <Title>Word Document Sample</Title>
045   <Subject>Microsoft Office Word 2007</Subject>
046   <Creator>2007 Microsoft Office System User</Creator>
047   <Keywords/>
048   <Description>2007 Microsoft Office system .docx file</Description>
049   <LastModifiedBy>2007 Microsoft Office System User</LastModifiedBy>
050   <Revision>2</Revision>
051   <DateCreated>2005-05-05T20:01:00Z</DateCreated>
052   <DateModified>2005-05-05T20:02:00Z</DateModified>
053</CoreProperties>
054 */
055
056public class DocumentPropertiesPart extends OfficeXMLPart
057{
058
059   //---------------------------------------------------------------------------
060   public DocumentPropertiesPart(OfficeOpenXmlDocument inOfficeDoc)
061   {
062      super(inOfficeDoc);
063
064      setFile(OfficeXML.CORE_PROPERTIES_FILE);
065
066      XMLTag rootTag = new XMLTag(OfficeXML.CORE_PROPERTIES);
067      rootTag.addXMLNamespaceDeclaration(OfficeXML.DUBLIN_CORE_NAMESPACE);
068      rootTag.addXMLNamespaceDeclaration(OfficeXML.DUBLIN_CORE_TERMS_NAMESPACE);
069      rootTag.addXMLNamespaceDeclaration(OfficeXML.DUBLIN_CORE_TYPES_NAMESPACE);
070      rootTag.addXMLNamespaceDeclaration(XMLSchema.XML_SCHEMA_INSTANCE_NAMESPACE);
071
072      setRootNode(rootTag);
073
074      // Set some initial values:
075      setTitle("");
076      setSubject("");
077      setCreator(System.getProperty("user.name"));
078//      setKeywords("");
079      setLastModifiedBy(System.getProperty("user.name"));
080      setRevision("1");
081
082      ZonedDateTime now = ZonedDateTime.now();
083      setDateCreated(now);
084      setDateModified(now);
085
086   }
087
088   //---------------------------------------------------------------------------
089   public DocumentPropertiesPart setTitle(String inValue)
090   {
091      getOrCreateSubtag(OfficeXML.TITLE).setContent(inValue);
092
093      return this;
094   }
095
096   //---------------------------------------------------------------------------
097   public DocumentPropertiesPart setCreator(String inValue)
098   {
099      getOrCreateSubtag(OfficeXML.CREATOR).setContent(inValue);
100
101      return this;
102   }
103
104   //---------------------------------------------------------------------------
105   public DocumentPropertiesPart setDescription(String inValue)
106   {
107      getOrCreateSubtag(OfficeXML.DESCRIPTION).setContent(inValue);
108
109      return this;
110   }
111
112   //---------------------------------------------------------------------------
113   public DocumentPropertiesPart setSubject(String inValue)
114   {
115      getOrCreateSubtag(OfficeXML.SUBJECT).setContent(inValue);
116
117      return this;
118   }
119
120   //---------------------------------------------------------------------------
121   public DocumentPropertiesPart setRevision(String inValue)
122   {
123      getOrCreateSubtag(OfficeXML.REVISION).setContent(inValue);
124
125      return this;
126   }
127
128   //---------------------------------------------------------------------------
129   public DocumentPropertiesPart setKeywords(String inValue)
130   {
131      getOrCreateSubtag(OfficeXML.KEYWORDS).setContent(inValue);
132
133      return this;
134   }
135
136   //---------------------------------------------------------------------------
137   public DocumentPropertiesPart setLastModifiedBy(String inValue)
138   {
139      getOrCreateSubtag(OfficeXML.LAST_MODIFIED_BY).setContent(inValue);
140
141      return this;
142   }
143
144   
145   //---------------------------------------------------------------------------
146   public DocumentPropertiesPart setDateCreated(Date inValue)
147   {
148      return setDateCreated(DateUtil.convertToZonedDateTime(inValue));
149   }
150
151   //---------------------------------------------------------------------------
152   public DocumentPropertiesPart setDateCreated(ZonedDateTime inValue)
153   {
154      // Convert time to UTC
155      ZonedDateTime utc = ZonedDateTime.ofInstant(inValue.toInstant(), ZoneOffset.UTC.normalized());
156
157      // This was sometimes creating datetimes with too many decimals (9) that caused Excel to throw errors.
158      // Ex: 2021-04-13T23:09:15.754474687Z
159//      getOrCreateSubtag(OfficeXML.CREATED).setContent(DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(utc))
160      getOrCreateSubtag(OfficeXML.CREATED).setContent(DateUtil.W3CDTF_FORMATTER.format(utc))
161                                          .setAttribute(XMLSchema.TYPE_ATT, "dcterms:W3CDTF");
162      return this;
163   }
164
165
166   //---------------------------------------------------------------------------
167   public DocumentPropertiesPart setDateModified(Date inValue)
168   {
169      return setDateModified(DateUtil.convertToZonedDateTime(inValue));
170   }
171   
172   //---------------------------------------------------------------------------
173   public DocumentPropertiesPart setDateModified(ZonedDateTime inValue)
174   {
175      // Convert time to UTC
176      ZonedDateTime utc = ZonedDateTime.ofInstant(inValue.toInstant(), ZoneOffset.UTC.normalized());
177
178      // This was sometimes creating datetimes with too many decimals (9) that caused Excel to throw errors.
179      // Ex: 2021-04-13T23:09:15.754474687Z
180//      getOrCreateSubtag(OfficeXML.MODIFIED).setContent(DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(utc))
181      getOrCreateSubtag(OfficeXML.MODIFIED).setContent(DateUtil.W3CDTF_FORMATTER.format(utc))
182                                           .setAttribute(XMLSchema.TYPE_ATT, "dcterms:W3CDTF");
183      return this;
184   }
185
186}