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  import java.util.function.BiConsumer;
23  
24  import org.apache.logging.log4j.LogManager;
25  import org.apache.logging.log4j.Logger;
26  import org.jdom2.Element;
27  import org.mycore.common.MCRException;
28  import org.mycore.common.MCRUtils;
29  
30  /**
31   * This class implements all methods for handling a name with the
32   * MCRMetaPersonName datamodel. The MCRMetaPersonName class represents a natural
33   * or legal person specified by a list of names parts.
34   * 
35   * @author J. Vogler
36   * @author J. Kupferschmidt
37   */
38  public final class MCRMetaPersonName extends MCRMetaDefault {
39  
40      private String firstname;
41  
42      private String callname;
43  
44      private String surname;
45  
46      private String fullname;
47  
48      private String academic;
49  
50      private String peerage;
51  
52      private String numeration;
53  
54      private String title;
55  
56      private String prefix;
57  
58      private String affix;
59  
60      private static final Logger LOGGER = LogManager.getLogger();
61  
62      /**
63       * This is the constructor. <br>
64       * The language element was set to <b>application default language</b>. All
65       * other elements will be set to an empty string.
66       */
67      public MCRMetaPersonName() {
68          super();
69          lang = DEFAULT_LANGUAGE;
70          firstname = "";
71          callname = "";
72          surname = "";
73          fullname = "";
74          academic = "";
75          peerage = "";
76          numeration = "";
77          title = "";
78          prefix = "";
79          affix = "";
80      }
81  
82      /**
83       * This is the constructor. <br>
84       * The method set all common fields of all MCRMetaXXX datamodel types. It
85       * set the language to the <b>application default language</b>. The type
86       * attribute will be set to an empty String.
87       * 
88       * @param subtag
89       *            the name of the subtag
90       * @param inherted
91       *            a value &gt;= 0
92       * 
93       * @exception MCRException
94       *                if the parameter values are invalid
95       */
96      public MCRMetaPersonName(String subtag, int inherted) throws MCRException {
97          super(subtag, DEFAULT_LANGUAGE, "", inherted);
98          type = "";
99          firstname = "";
100         callname = "";
101         surname = "";
102         fullname = "";
103         academic = "";
104         peerage = "";
105         numeration = "";
106         title = "";
107         prefix = "";
108         affix = "";
109     }
110 
111     /**
112      * This method get the first name text element.
113      * 
114      * @return the first name
115      */
116     public String getFirstName() {
117         return firstname;
118     }
119 
120     /**
121      * This method set the first name text element.
122      */
123     public void setFirstName(String firstname) {
124         if (firstname != null) {
125             this.firstname = firstname.trim();
126         } else {
127             this.firstname = "";
128         }
129     }
130 
131     /**
132      * This method get the call name text element.
133      * 
134      * @return the call name
135      */
136     public String getCallName() {
137         return callname;
138     }
139 
140     /**
141      * This method set the call name text element.
142      */
143     public void setCallName(String callname) {
144         if (callname != null) {
145             this.callname = callname.trim();
146         } else {
147             this.callname = "";
148         }
149     }
150 
151     /**
152      * This method get the surname text element.
153      * 
154      * @return the surname
155      */
156     public String getSurName() {
157         return surname;
158     }
159 
160     /**
161      * This method set the surname text element.
162      */
163     public void setSurName(String surname) {
164         if (surname != null) {
165             this.surname = surname.trim();
166         } else {
167             this.surname = "";
168         }
169     }
170 
171     /**
172      * This method get the full name text element.
173      * 
174      * @return the full name
175      */
176     public String getFullName() {
177         return fullname;
178     }
179 
180     /**
181      * This method set the full name text element.
182      */
183     public void setFullName(String fullname) {
184         if (fullname != null) {
185             this.fullname = fullname.trim();
186         } else {
187             this.fullname = "";
188         }
189     }
190 
191     /**
192      * This method get the academic text element.
193      * 
194      * @return the academic
195      */
196     public String getAcademic() {
197         return academic;
198     }
199 
200     /**
201      * This method set the academic text element.
202      */
203     public void setAcademic(String academic) {
204         if (academic != null) {
205             this.academic = academic.trim();
206         } else {
207             this.academic = "";
208         }
209     }
210 
211     /**
212      * This method get the peerage text element.
213      * 
214      * @return the peerage
215      */
216     public String getPeerage() {
217         return peerage;
218     }
219 
220     /**
221      * This method set the peerage text element.
222      */
223     public void setPeerage(String peerage) {
224         if (peerage != null) {
225             this.peerage = peerage.trim();
226         } else {
227             this.peerage = "";
228         }
229     }
230 
231     /**
232      * This method get the numeration text element.
233      * 
234      * @return the numeration
235      */
236     public String getNumeration() {
237         return numeration;
238     }
239 
240     /**
241      * This method set the numeration text element.
242      */
243     public void setNumeration(String numeration) {
244         if (numeration != null) {
245             this.numeration = numeration.trim();
246         } else {
247             this.numeration = "";
248         }
249     }
250 
251     /**
252      * This method get the title text element.
253      * 
254      * @return the title
255      */
256     public String getTitle() {
257         return title;
258     }
259 
260     /**
261      * This method set the title text element.
262      */
263     public void setTitle(String title) {
264         if (title != null) {
265             this.title = title.trim();
266         } else {
267             this.title = "";
268         }
269     }
270 
271     /**
272      * This method get the prefix text element.
273      * 
274      * @return the prefix
275      */
276     public String getPrefix() {
277         return prefix;
278     }
279 
280     /**
281      * This method set the prefix text element.
282      */
283     public void setPrefix(String prefix) {
284         if (prefix != null) {
285             this.prefix = prefix.trim();
286         } else {
287             this.prefix = "";
288         }
289     }
290 
291     /**
292      * This method get the affix text element.
293      * 
294      * @return the affix
295      */
296     public String getAffix() {
297         return affix;
298     }
299 
300     /**
301      * This method set the affix text element.
302      */
303     public void setAffix(String affix) {
304         if (affix != null) {
305             this.affix = affix.trim();
306         } else {
307             this.affix = "";
308         }
309     }
310 
311     /**
312      * This method reads the XML input stream part from a DOM part for the
313      * metadata of the document.
314      * 
315      * @param element
316      *            a relevant JDOM element for the metadata
317      */
318     @Override
319     public void setFromDOM(Element element) {
320         super.setFromDOM(element);
321         setFirstName(element.getChildTextTrim("firstname"));
322         setCallName(element.getChildTextTrim("callname"));
323         setSurName(element.getChildTextTrim("surname"));
324         setFullName(element.getChildTextTrim("fullname"));
325         setAcademic(element.getChildTextTrim("academic"));
326         setPeerage(element.getChildTextTrim("peerage"));
327         setNumeration(element.getChildTextTrim("numeration"));
328         setTitle(element.getChildTextTrim("title"));
329         setPrefix(element.getChildTextTrim("prefix"));
330         setAffix(element.getChildTextTrim("affix"));
331     }
332 
333     /**
334      * This method creates a XML stream for all data in this class, defined by
335      * the MyCoRe XML MCRMetaPersonName definition for the given subtag.
336      * 
337      * @exception MCRException
338      *                if the content of this class is not valid
339      * @return a JDOM Element with the XML MCRMetaPersonName part
340      */
341     @Override
342     public Element createXML() throws MCRException {
343         Element elm = super.createXML();
344         BiConsumer<String, String> addContent = (name, value) -> MCRUtils.filterTrimmedNotEmpty(value)
345             .ifPresent(trimmedValue -> elm.addContent(new Element(name).addContent(trimmedValue)));
346         addContent.accept("firstname", firstname);
347         addContent.accept("callname", callname);
348         addContent.accept("fullname", fullname);
349         addContent.accept("surname", surname);
350         addContent.accept("academic", academic);
351         addContent.accept("peerage", peerage);
352         addContent.accept("numeration", numeration);
353         addContent.accept("title", title);
354         addContent.accept("prefix", prefix);
355         addContent.accept("affix", affix);
356 
357         return elm;
358     }
359 
360     /**
361      * Validates this MCRMetaPersonName. This method throws an exception if:
362      * <ul>
363      * <li>the subtag is not null or empty</li>
364      * <li>the lang value was supported</li>
365      * <li>the inherited value is lower than zero</li>
366      * <li>the firstname, the callname or the fullname is null</li>
367      * </ul>
368      * 
369      * @throws MCRException the MCRMetaPersonName is invalid
370      */
371     public void validate() throws MCRException {
372         super.validate();
373         if (firstname == null || callname == null || fullname == null) {
374             throw new MCRException(getSubTag() + ": one of fullname, callname or firstname is null.");
375         }
376         firstname = firstname.trim();
377         if (firstname.length() == 0) {
378             firstname = callname;
379         }
380         callname = callname.trim();
381         if (callname.length() == 0) {
382             callname = firstname;
383         }
384         fullname = fullname.trim();
385         if (fullname.length() == 0) {
386             String sb = academic + ' ' + peerage + ' ' + firstname + ' ' + prefix + ' ' + surname;
387             fullname = sb.trim();
388             if (fullname.length() == 0) {
389                 throw new MCRException(getSubTag() + ": full name / first name or surname is empty");
390             }
391         }
392     }
393 
394     /**
395      * clone of this instance
396      * 
397      * you will get a (deep) clone of this element
398      * 
399      * @see java.lang.Object#clone()
400      */
401     @Override
402     public MCRMetaPersonName clone() {
403         MCRMetaPersonName clone = (MCRMetaPersonName) super.clone();
404 
405         clone.firstname = this.firstname;
406         clone.callname = this.callname;
407         clone.surname = this.surname;
408         clone.fullname = this.fullname;
409         clone.academic = this.academic;
410         clone.peerage = this.peerage;
411         clone.numeration = this.numeration;
412         clone.title = this.title;
413         clone.prefix = this.prefix;
414         clone.affix = this.affix;
415 
416         return clone;
417     }
418 
419     /**
420      * This method put debug data to the logger (for the debug mode).
421      */
422     @Override
423     public void debug() {
424         if (LOGGER.isDebugEnabled()) {
425             super.debugDefault();
426             LOGGER.debug("First name         = {}", firstname);
427             LOGGER.debug("Call name          = {}", callname);
428             LOGGER.debug("Surname            = {}", surname);
429             LOGGER.debug("Full name          = {}", fullname);
430             LOGGER.debug("Academic           = {}", academic);
431             LOGGER.debug("Peerage            = {}", peerage);
432             LOGGER.debug("Numeration         = {}", numeration);
433             LOGGER.debug("Title              = {}", title);
434             LOGGER.debug("Prefix             = {}", prefix);
435             LOGGER.debug("Affix              = {}", affix);
436             LOGGER.debug("");
437         }
438     }
439 
440     /**
441      * This method compares this instance with a MCRMetaPersonName object
442      */
443     @Override
444     public boolean equals(Object obj) {
445         if (!super.equals(obj)) {
446             return false;
447         }
448         final MCRMetaPersonName other = (MCRMetaPersonName) obj;
449         return Objects.equals(this.firstname, other.firstname) && Objects.equals(this.callname, other.callname) &&
450             Objects.equals(this.surname, other.surname) && Objects.equals(this.fullname, other.fullname) &&
451             Objects.equals(this.academic, other.academic) && Objects.equals(this.peerage, other.peerage) &&
452             Objects.equals(this.numeration, other.numeration) && Objects.equals(this.title, other.title) &&
453             Objects.equals(this.prefix, other.prefix) && Objects.equals(this.affix, other.affix);
454     }
455 }