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.Objects;
22  
23  import org.apache.logging.log4j.LogManager;
24  import org.apache.logging.log4j.Logger;
25  import org.jdom2.Element;
26  import org.mycore.common.MCRException;
27  
28  import com.google.gson.JsonObject;
29  
30  /**
31   * This class implements all methods for handling with the MCRMetaAddress part
32   * of a metadata object. The MCRMetaAddress class represents a natural address
33   * specified by a list of names.
34   * 
35   * @author J. Vogler
36   */
37  public final class MCRMetaAddress extends MCRMetaDefault {
38      // MetaAddress data
39      private String country;
40  
41      private String state;
42  
43      private String zipCode;
44  
45      private String city;
46  
47      private String street;
48  
49      private String number;
50  
51      private static final Logger LOGGER = LogManager.getLogger();
52  
53      /**
54       * This is the constructor. <br>
55       * The language element was set to <b>en </b>. All other elemnts are set to
56       * an empty string.
57       */
58      public MCRMetaAddress() {
59          super();
60      }
61  
62      /**
63       * This is the constructor. <br>
64       * The language element was set. If the value of <em>defaultLang</em> is
65       * null, empty or false <b>en </b> was set. The subtag element was set to
66       * the value of <em>subtag</em>. If the value of <em>subtag</em>
67       * is null or empty an exception was thrown. The type element was set to
68       * the value of <em>type</em>, if it is null, an empty string was set
69       * to the type element. The country, state, zipCode, city, street and
70       * number element was set to the value of <em>...</em>, if they are null,
71       * an empty string was set to this element.
72       * @param subtag      the name of the subtag
73       * @param defaultLang    the default language
74       * @param type        the optional type string
75       * @param inherted    a value &gt;= 0
76       * @param country     the country name
77       * @param state       the state name
78       * @param zipcode     the zipCode string
79       * @param city        the city name
80       * @param street      the street name
81       * @param number      the number string
82       *
83       * @exception MCRException if the parameter values are invalid
84       */
85      public MCRMetaAddress(final String subtag, final String defaultLang, final String type,
86          final int inherted, final String country,
87          final String state, final String zipcode, final String city, final String street,
88          final String number) throws MCRException {
89          super(subtag, defaultLang, type, inherted);
90          setCountry(country);
91          setState(state);
92          setZipCode(zipcode);
93          setCity(city);
94          setStreet(street);
95          setNumber(number);
96      }
97  
98      /**
99       * clone of this instance
100      * 
101      * you will get a (deep) clone of this element
102      * 
103      * @see java.lang.Object#clone()
104      */
105     @Override
106     public MCRMetaAddress clone() {
107         MCRMetaAddress clone = (MCRMetaAddress) super.clone();
108 
109         clone.country = this.country;
110         clone.state = this.state;
111         clone.zipCode = this.zipCode;
112         clone.city = this.city;
113         clone.street = this.street;
114         clone.number = this.number;
115 
116         return clone;
117     }
118 
119     /**
120      * This method creates a XML stream for all data in this class, defined by
121      * the MyCoRe XML MCRMetaAddress definition for the given subtag.
122      * 
123      * @exception MCRException
124      *                if the content of this class is not valid
125      * @return a JDOM Element with the XML MCRMetaAddress part
126      */
127     @Override
128     public Element createXML() throws MCRException {
129         final Element elm = super.createXML();
130         if (getCountry() != null) {
131             elm.addContent(new Element("country").addContent(getCountry()));
132         }
133         if (getState() != null) {
134             elm.addContent(new Element("state").addContent(getState()));
135         }
136         if (getZipCode() != null) {
137             elm.addContent(new Element("zipcode").addContent(getZipCode()));
138         }
139         if (getCity() != null) {
140             elm.addContent(new Element("city").addContent(getCity()));
141         }
142         if (getStreet() != null) {
143             elm.addContent(new Element("street").addContent(getStreet()));
144         }
145         if (getNumber() != null) {
146             elm.addContent(new Element("number").addContent(getNumber()));
147         }
148 
149         return elm;
150     }
151 
152     /**
153      * Creates the JSON representation. Extends the {@link MCRMetaDefault#createJSON()} method
154      * with the following data.
155      * 
156      * <pre>
157      *   {
158      *     "country": "Deutschland",
159      *     "state": "Thüringen",
160      *     "zipcode": "07743",
161      *     "city": "Jena",
162      *     "street": "Bibliothekspl.",
163      *     "number": "2"
164      *   }
165      * </pre>
166      * 
167      */
168     @Override
169     public JsonObject createJSON() {
170         JsonObject obj = super.createJSON();
171         if (getCountry() != null) {
172             obj.addProperty("country", getCountry());
173         }
174         if (getState() != null) {
175             obj.addProperty("state", getState());
176         }
177         if (getZipCode() != null) {
178             obj.addProperty("zipcode", getZipCode());
179         }
180         if (getCity() != null) {
181             obj.addProperty("city", getCity());
182         }
183         if (getStreet() != null) {
184             obj.addProperty("street", getStreet());
185         }
186         if (getNumber() != null) {
187             obj.addProperty("number", getNumber());
188         }
189         return obj;
190     }
191 
192     /**
193      * This method put debug data to the logger (for the debug mode).
194      */
195     @Override
196     public void debug() {
197         if (LOGGER.isDebugEnabled()) {
198             super.debugDefault();
199             LOGGER.debug("Country            = {}", country);
200             LOGGER.debug("State              = {}", state);
201             LOGGER.debug("Zipcode            = {}", zipCode);
202             LOGGER.debug("City               = {}", city);
203             LOGGER.debug("Street             = {}", street);
204             LOGGER.debug("Number             = {}", number);
205             LOGGER.debug(" ");
206         }
207     }
208 
209     /**
210      * Check the equivalence between this instance and the given object.
211      * 
212      * @param obj the MCRMetaAddress object
213      * @return true if its equal
214      */
215     @Override
216     public boolean equals(Object obj) {
217         if (!super.equals(obj)) {
218             return false;
219         }
220         final MCRMetaAddress other = (MCRMetaAddress) obj;
221         return Objects.equals(this.country, other.country) && Objects.equals(this.state, other.state)
222             && Objects.equals(this.zipCode, other.zipCode) && Objects.equals(this.city, other.city)
223             && Objects.equals(this.street, other.street) && Objects.equals(this.number, other.number);
224     }
225 
226     /**
227      * @return the city
228      */
229     public String getCity() {
230         return city;
231     }
232 
233     /**
234      * @return the country
235      */
236     public String getCountry() {
237         return country;
238     }
239 
240     /**
241      * @return the number
242      */
243     public String getNumber() {
244         return number;
245     }
246 
247     /**
248      * @return the state
249      */
250     public String getState() {
251         return state;
252     }
253 
254     /**
255      * @return the street
256      */
257     public String getStreet() {
258         return street;
259     }
260 
261     /**
262      * @return the zipCode
263      */
264     public String getZipCode() {
265         return zipCode;
266     }
267 
268     /**
269      * Validates this MCRMetaAddress. This method throws an exception if:
270      * <ul>
271      * <li>the subtag is not null or empty</li>
272      * <li>the lang value was supported</li>
273      * <li>the inherited value is lower than zero</li>
274      * <li>all of country, state, zip, city, street and number is empty</li>
275      * </ul>
276      * 
277      * @throws MCRException the MCRMetaAddress is invalid
278      */
279     public void validate() throws MCRException {
280         super.validate();
281         if (getCountry() == null && getState() == null && getZipCode() == null && getCity() == null
282             && getStreet() == null && getNumber() == null) {
283             throw new MCRException(getSubTag() + ": address is empty");
284         }
285     }
286 
287     /**
288      * @param city the city to set
289      */
290     public void setCity(final String city) {
291         this.city = city;
292     }
293 
294     /**
295      * @param country the country to set
296      */
297     public void setCountry(final String country) {
298         this.country = country;
299     }
300 
301     /**
302      * This method reads the XML input stream part from a DOM part for the
303      * metadata of the document.
304      * 
305      * @param element
306      *            a relevant JDOM element for the metadata
307      */
308     @Override
309     public void setFromDOM(final Element element) {
310         super.setFromDOM(element);
311         country = element.getChildTextTrim("country");
312         state = element.getChildTextTrim("state");
313         zipCode = element.getChildTextTrim("zipcode");
314         city = element.getChildTextTrim("city");
315         street = element.getChildTextTrim("street");
316         number = element.getChildTextTrim("number");
317     }
318 
319     /**
320      * @param number the number to set
321      */
322     public void setNumber(final String number) {
323         this.number = number;
324     }
325 
326     /**
327      * @param state the state to set
328      */
329     public void setState(final String state) {
330         this.state = state;
331     }
332 
333     /**
334      * @param street the street to set
335      */
336     public void setStreet(final String street) {
337         this.street = street;
338     }
339 
340     /**
341      * @param zipCode the zipCode to set
342      */
343     public void setZipCode(final String zipCode) {
344         this.zipCode = zipCode;
345     }
346 }