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.jdom2.Element;
24  import org.mycore.common.MCRException;
25  import org.mycore.common.MCRUtils;
26  
27  import com.google.gson.JsonObject;
28  
29  /**
30   * This class implements all methods for handling with the
31   * MCRMetaInstitutionName part of a metadata object. The MCRMetaInstitutionName
32   * class represents a name of an institution or corporation.
33   * 
34   * @author J. Kupferschmidt
35   * @version $Revision$ $Date$
36   */
37  public final class MCRMetaInstitutionName extends MCRMetaDefault {
38      // data
39      private String fullname;
40  
41      private String nickname;
42  
43      private String property;
44  
45      /**
46       * This is the constructor. <br>
47       * The language element was set to <b>en </b>. All other elemnts are set to
48       * an empty string.
49       */
50      public MCRMetaInstitutionName() {
51          super();
52          fullname = "";
53          nickname = "";
54          property = "";
55      }
56  
57      /**
58       * This is the constructor. <br>
59       * The language element was set. If the value of <em>lang</em> is
60       * null, empty or false <b>en </b> was set. The subtag element was set to
61       * the value of <em>subtag</em>. If the value of <em>subtag</em>
62       * is null or empty an exception was throwed. The type element was set to
63       * the value of <em>type</em>, if it is null, an empty string was set
64       * to the type element. The fullname, nickname  and property element
65       * was set to the value of <em>set_...</em>, if they are null,
66       * an empty string was set to this element.
67       * @param subtag      the name of the subtag
68       * @param lang    the default language
69       * @param type        the optional type string
70       * @param inherted    a value &gt;= 0
71       * @param fullname    the full name
72       * @param nickname    the nickname
73       * @param property       the property title
74       *
75       * @exception MCRException if the parameter values are invalid
76       */
77      public MCRMetaInstitutionName(String subtag, String lang, String type, int inherted,
78          String fullname, String nickname, String property) throws MCRException {
79          super(subtag, lang, type, inherted);
80          this.fullname = "";
81          this.nickname = "";
82          this.property = "";
83          set(fullname, nickname, property);
84      }
85  
86      /**
87       * This methode set all name componets.
88       * 
89       * @param fullname
90       *            the full name
91       * @param nickname
92       *            the nickname
93       * @param property
94       *            the property title
95       */
96      public void set(String fullname, String nickname, String property) {
97          if (fullname == null || nickname == null || property == null) {
98              throw new MCRException("One parameter is null.");
99          }
100 
101         this.fullname = fullname.trim();
102         this.nickname = nickname.trim();
103         this.property = property.trim();
104     }
105 
106     /**
107      * This method get the name text element.
108      * 
109      * @return the fullname
110      */
111     public String getFullName() {
112         return fullname;
113     }
114 
115     /**
116      * This method get the nickname text element.
117      * 
118      * @return the nickname
119      */
120     public String getNickname() {
121         return nickname;
122     }
123 
124     /**
125      * This method get the property text element.
126      * 
127      * @return the property
128      */
129     public String getProperty() {
130         return property;
131     }
132 
133     /**
134      * This method reads the XML input stream part from a DOM part for the
135      * metadata of the document.
136      * 
137      * @param element
138      *            a relevant DOM element for the metadata
139      */
140     @Override
141     public void setFromDOM(Element element) {
142         super.setFromDOM(element);
143         fullname = element.getChildTextTrim("fullname");
144 
145         if (fullname == null) {
146             fullname = "";
147         }
148 
149         nickname = element.getChildTextTrim("nickname");
150 
151         if (nickname == null) {
152             nickname = "";
153         }
154 
155         property = element.getChildTextTrim("property");
156 
157         if (property == null) {
158             property = "";
159         }
160     }
161 
162     /**
163      * This method creates a XML stream for all data in this class, defined by
164      * the MyCoRe XML MCRMetaInstitutionName definition for the given subtag.
165      * 
166      * @exception MCRException
167      *                if the content of this class is not valid
168      * @return a JDOM Element with the XML MCRMetaInstitutionName part
169      */
170     @Override
171     public Element createXML() throws MCRException {
172         Element elm = super.createXML();
173         elm.addContent(new Element("fullname").addContent(fullname));
174 
175         nickname = nickname.trim();
176         if (nickname.length() != 0) {
177             elm.addContent(new Element("nickname").addContent(nickname));
178         }
179 
180         property = property.trim();
181         if (property.length() != 0) {
182             elm.addContent(new Element("property").addContent(property));
183         }
184 
185         return elm;
186     }
187 
188     /**
189      * Creates the JSON representation. Extends the {@link MCRMetaDefault#createJSON()} method
190      * with the following data.
191      * 
192      * <pre>
193      *   {
194      *     fullname: "library of congress",
195      *     nickname: "LOC",
196      *     property: "USA"
197      *   }
198      * </pre>
199      */
200     @Override
201     public JsonObject createJSON() {
202         JsonObject obj = super.createJSON();
203         obj.addProperty("fullname", fullname);
204         if (nickname != null) {
205             obj.addProperty("nickname", nickname);
206         }
207         if (property != null) {
208             obj.addProperty("property", property);
209         }
210         return obj;
211     }
212 
213     /**
214      * Validates this MCRMetaInstitutionName. This method throws an exception if:
215      * <ul>
216      * <li>the subtag is not null or empty</li>
217      * <li>the lang value was supported</li>
218      * <li>the inherited value is lower than zero</li>
219      * <li>the trimmed fullname is null or empty</li>
220      * </ul>
221      * 
222      * @throws MCRException the MCRMetaInstitutionName is invalid
223      */
224     public void validate() throws MCRException {
225         super.validate();
226         fullname = MCRUtils.filterTrimmedNotEmpty(fullname)
227             .orElseThrow(() -> new MCRException(getSubTag() + ": fullname is null or empty"));
228     }
229 
230     /**
231      * clone of this instance
232      * 
233      * you will get a (deep) clone of this element
234      * 
235      * @see java.lang.Object#clone()
236      */
237     @Override
238     public MCRMetaInstitutionName clone() {
239         MCRMetaInstitutionName clone = (MCRMetaInstitutionName) super.clone();
240 
241         clone.fullname = this.fullname;
242         clone.nickname = this.nickname;
243         clone.property = this.property;
244 
245         return clone;
246     }
247 
248     @Override
249     public boolean equals(Object obj) {
250         if (!super.equals(obj)) {
251             return false;
252         }
253         final MCRMetaInstitutionName other = (MCRMetaInstitutionName) obj;
254         return Objects.equals(fullname, other.fullname) && Objects.equals(nickname, other.nickname)
255             && Objects.equals(property, other.property);
256     }
257 
258 }