View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.datamodel.metadata;
20  
21  import java.util.Date;
22  import java.util.Objects;
23  
24  import org.apache.logging.log4j.LogManager;
25  import org.apache.logging.log4j.Logger;
26  import org.jdom2.Element;
27  import org.jdom2.Namespace;
28  import org.mycore.common.MCRException;
29  import org.mycore.datamodel.common.MCRISO8601Date;
30  import org.mycore.datamodel.common.MCRISO8601Format;
31  
32  import com.google.gson.JsonObject;
33  
34  /**
35   * provides support for a restricted range of formats, all of which are valid
36   * ISO 8601 dates and times.
37   * 
38   * The range of supported formats is exactly the same range that is suggested by
39   * the W3C <a href="http://www.w3.org/TR/NOTE-datetime">datetime profile</a> in
40   * its version from 1997-09-15.
41   * 
42   * @author Thomas Scheffler (yagee)
43   * 
44   * @version $Revision$ $Date$
45   * @since 1.3
46   */
47  public final class MCRMetaISO8601Date extends MCRMetaDefault {
48  
49      private Element export;
50  
51      private boolean changed = true;
52  
53      private static final Namespace DEFAULT_NAMESPACE = Namespace.NO_NAMESPACE;
54  
55      private MCRISO8601Date isoDate;
56  
57      private static final Logger LOGGER = LogManager.getLogger();
58  
59      /**
60       * constructs a empty instance.
61       * 
62       * @see MCRMetaDefault#MCRMetaDefault()
63       */
64      public MCRMetaISO8601Date() {
65          super();
66          this.isoDate = new MCRISO8601Date();
67      }
68  
69      /**
70       * same as superImplentation but sets lang attribute to "null"
71       * 
72       * @see MCRMetaDefault#MCRMetaDefault(String, String, String, int)
73       */
74      public MCRMetaISO8601Date(String subtag, String type, int inherted) {
75          super(subtag, null, type, inherted);
76          this.isoDate = new MCRISO8601Date();
77      }
78  
79      /*
80       * (non-Javadoc)
81       * 
82       * @see org.mycore.datamodel.metadata.MCRMetaDefault#createXML()
83       */
84      @Override
85      public Element createXML() throws MCRException {
86          if (!changed) {
87              return export.clone();
88          }
89          Element elm = super.createXML();
90          if (!(isoDate.getIsoFormat() == null || isoDate.getIsoFormat() == MCRISO8601Format.COMPLETE_HH_MM_SS_SSS)) {
91              elm.setAttribute("format", isoDate.getIsoFormat().toString());
92          }
93          elm.setText(getISOString());
94          export = elm;
95          changed = false;
96          return export.clone();
97      }
98  
99      /**
100      * Creates the JSON representation. Extends the {@link MCRMetaDefault#createJSON()} method
101      * with the following data.
102      * 
103      * <pre>
104      *   {
105      *     date: "2016-02-08",
106      *     format: "YYYY-MM-DD"
107      *   }
108      * </pre>
109      */
110     @Override
111     public JsonObject createJSON() {
112         JsonObject obj = super.createJSON();
113         obj.addProperty("date", getISOString());
114         if (isoDate.getIsoFormat() != null) {
115             obj.addProperty("format", isoDate.getIsoFormat().toString());
116         }
117         return obj;
118     }
119 
120     @Override
121     public void setFromDOM(Element element) {
122         super.setFromDOM(element);
123         setFormat(element.getAttributeValue("format"));
124         setDate(element.getTextTrim());
125         export = element.clone();
126     }
127 
128     /**
129      * returns the namespace of this element
130      * 
131      * @return Returns the ns.
132      */
133     protected static Namespace getNs() {
134         return DEFAULT_NAMESPACE;
135     }
136 
137     /**
138      * sets the date for this meta data object
139      * 
140      * @param isoString
141      *            Date in any form that is a valid W3C dateTime
142      */
143     public void setDate(String isoString) {
144         isoDate.setDate(isoString);
145     }
146 
147     /**
148      * returns the Date representing this element.
149      * 
150      * @return a new Date instance of the time set in this element
151      */
152     public Date getDate() {
153         return isoDate.getDate();
154     }
155 
156     /**
157      * sets the date for this meta data object
158      * 
159      * @param dt
160      *            Date object representing date String in Element
161      */
162     public void setDate(Date dt) {
163         isoDate.setDate(dt);
164     }
165 
166     /**
167      * returns a ISO 8601 conform String using the current set format.
168      * 
169      * @return date in ISO 8601 format, or null if date is unset.
170      */
171     public String getISOString() {
172         return isoDate.getISOString();
173     }
174 
175     /**
176      * sets the input and output format.
177      * 
178      * please use only the formats defined on the <a
179      * href="http://www.w3.org/TR/NOTE-datetime">W3C Page</a>, which are also
180      * exported as static fields by this class.
181      * 
182      * @param format
183      *            a format string that is valid conforming to xsd:duration
184      *            schema type.
185      * 
186      */
187     public void setFormat(String format) {
188         isoDate.setFormat(format);
189     }
190 
191     public String getFormat() {
192         return isoDate == null || isoDate.getIsoFormat() == null ? null : isoDate.getIsoFormat().toString();
193     }
194 
195     /**
196      * Returns the internal date.
197      * 
198      * @return the base date
199      */
200     public MCRISO8601Date getMCRISO8601Date() {
201         return isoDate;
202     }
203 
204     /**
205      * This method put debug data to the logger (for the debug mode).
206      */
207     @Override
208     public void debug() {
209         if (LOGGER.isDebugEnabled()) {
210             super.debugDefault();
211             LOGGER.debug("Date={}", isoDate != null ? isoDate.getISOString() : "null");
212             if (isoDate != null) {
213                 MCRISO8601Format isoFormat = isoDate.getIsoFormat();
214                 LOGGER.debug("Format={}", isoFormat);
215             }
216         }
217     }
218 
219     /**
220      * clone of this instance
221      * 
222      * you will get a (deep) clone of this element
223      * 
224      * @see java.lang.Object#clone()
225      */
226     @Override
227     public MCRMetaISO8601Date clone() {
228         MCRMetaISO8601Date clone = (MCRMetaISO8601Date) super.clone();
229 
230         clone.changed = this.changed;
231         clone.export = this.export.clone();
232         clone.isoDate = this.isoDate; // this is ok because iso Date is immutable
233 
234         return clone;
235     }
236 
237     /**
238      * Validates this MCRMetaISO8601Date. This method throws an exception if:
239      * <ul>
240      * <li>the subtag is not null or empty</li>
241      * <li>the lang value was supported</li>
242      * <li>the inherited value is lower than zero</li>
243      * <li>the isoDate or the temporal accessor is null</li>
244      * </ul>
245      * 
246      * @throws MCRException the MCRMetaISO8601Date is invalid
247      */
248     public void validate() throws MCRException {
249         super.validate();
250         if (isoDate == null || isoDate.getDt() == null) {
251             throw new MCRException(getSubTag() + ": date is invalid");
252         }
253     }
254 
255     @Override
256     public boolean equals(Object obj) {
257         if (!super.equals(obj)) {
258             return false;
259         }
260         final MCRMetaISO8601Date other = (MCRMetaISO8601Date) obj;
261         return Objects.equals(this.isoDate, other.isoDate);
262     }
263 
264 }