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 com.google.gson.JsonObject;
22  import org.jdom2.Element;
23  import org.mycore.common.MCRException;
24  import org.mycore.datamodel.common.MCRISO8601Date;
25  import org.mycore.datamodel.common.MCRISO8601Format;
26  
27  import java.util.Objects;
28  
29  /**
30   * This class implements all method for handling with the MCRMetaDateLangText part
31   * of a metadata object. The MCRMetaDateLangText class present a single item, which
32   * has quadruples of a text and his corresponding language and optional a type and date.
33   *
34   * @version $Revision$ $Date$
35   */
36  public class MCRMetaDateLangText extends MCRMetaLangText {
37  
38      protected MCRISO8601Date isoDate;
39  
40      /**
41       * This is the constructor. <br>
42       * The language element was set to <b>en </b>. All other elements was set to
43       * an empty string. The <em>form</em> Attribute is set to 'plain'.
44       */
45      public MCRMetaDateLangText() {
46          super();
47      }
48  
49      /**
50       * This is the constructor. <br>
51       * The language element was set. If the value of <em>lang</em> is
52       * null, empty or false <b>en </b> was set. The subtag element was set to
53       * the value of <em>subtag</em>. If the value of <em>subtag</em>
54       * is null or empty an exception was thrown. The type element was set to
55       * the value of <em>type</em>, if it is null, an empty string was set
56       * to the type element. The text element was set to the value of
57       * <em>text</em>, if it is null, an empty string was set
58       * to the text element.
59       * @param subtag       the name of the subtag
60       * @param lang     the default language
61       * @param type         the optional type string
62       * @param inherted     a value &gt;= 0
63       * @param form         the format string, if it is empty 'plain' is set.
64       * @param text         the text string
65       *
66       * @exception MCRException if the subtag value is null or empty
67       */
68      public MCRMetaDateLangText(String subtag, String lang, String type, int inherted, String form, String text)
69          throws MCRException {
70          super(subtag, lang, type, inherted, form, text);
71      }
72  
73      /**
74       * sets the date for this meta data object
75       *
76       * @param isoDate
77       *            the new date, may be null
78       */
79      public void setDate(MCRISO8601Date isoDate) {
80          this.isoDate = isoDate;
81      }
82  
83      /**
84       * Returns the date.
85       *
86       * @return the date, may be null
87       */
88      public MCRISO8601Date getDate() {
89          return isoDate;
90      }
91  
92      /**
93       * This method read the XML input stream part from a DOM part for the
94       * metadata of the document.
95       *
96       * @param element
97       *            a relevant JDOM element for the metadata
98       */
99      @Override
100     public void setFromDOM(Element element) {
101         super.setFromDOM(element);
102 
103         String tempDate = element.getAttributeValue("date");
104 
105         if (tempDate != null) {
106 
107             MCRISO8601Date tempIsoDate = new MCRISO8601Date();
108 
109             String tempFormat = element.getAttributeValue("format");
110 
111             if (tempFormat != null) {
112                 tempIsoDate.setFormat(tempFormat);
113             }
114 
115             tempIsoDate.setDate(tempDate);
116 
117             isoDate = tempIsoDate;
118 
119         }
120     }
121 
122     /**
123      * This method create a XML stream for all data in this class, defined by
124      * the MyCoRe XML MCRMetaLangText definition for the given subtag.
125      *
126      * @exception MCRException
127      *                if the content of this class is not valid
128      * @return a JDOM Element with the XML MCRMetaLangText part
129      */
130     @Override
131     public Element createXML() throws MCRException {
132         Element elm = super.createXML();
133 
134         if (isoDate != null) {
135             elm.setAttribute("date", isoDate.getISOString());
136             MCRISO8601Format isoFormat = isoDate.getIsoFormat();
137             if (isoFormat != null && isoFormat != MCRISO8601Format.COMPLETE_HH_MM_SS_SSS) {
138                 elm.setAttribute("format", isoDate.getIsoFormat().toString());
139             }
140         }
141 
142         return elm;
143     }
144 
145     /**
146      * Creates the JSON representation. Extends the {@link MCRMetaLangText#createJSON()} method
147      * with the following data.
148      *
149      * <pre>
150      *   {
151      *     text: "Hallo Welt",
152      *     form: "plain",
153      *     date: "2000-01-01",
154      *     format: "YYYY-MM-DD"
155      *   }
156      * </pre>
157      *
158      */
159     @Override
160     public JsonObject createJSON() {
161         JsonObject obj = super.createJSON();
162         if (isoDate != null) {
163             obj.addProperty("date", isoDate.getISOString());
164             MCRISO8601Format isoFormat = isoDate.getIsoFormat();
165             if (isoFormat != null) {
166                 obj.addProperty("format", isoDate.getIsoFormat().toString());
167             }
168         }
169         return obj;
170     }
171 
172     /**
173      * clone of this instance
174      *
175      * you will get a (deep) clone of this element
176      *
177      * @see Object#clone()
178      */
179     @Override
180     public MCRMetaDateLangText clone() {
181         MCRMetaDateLangText clone = (MCRMetaDateLangText) super.clone();
182 
183         clone.isoDate = this.isoDate; // this is ok because iso Date is immutable
184 
185         return clone;
186     }
187 
188     /**
189      * Check the equivalence between this instance and the given object.
190      *
191      * @param obj the MCRMetaDateLangText object
192      * @return true if its equal
193      */
194     @Override
195     public boolean equals(Object obj) {
196         if (!super.equals(obj)) {
197             return false;
198         }
199         final MCRMetaDateLangText other = (MCRMetaDateLangText) obj;
200         return Objects.equals(this.text, other.text) && Objects.equals(this.form, other.form)
201             && Objects.equals(this.isoDate, other.isoDate);
202     }
203 
204 }