001    /*
002     * 
003     * $Revision: 14003 $ $Date: 2008-09-16 11:45:07 +0200 (Di, 16 Sep 2008) $
004     *
005     * This file is part of ***  M y C o R e  ***
006     * See http://www.mycore.de/ for details.
007     *
008     * This program is free software; you can use it, redistribute it
009     * and / or modify it under the terms of the GNU General Public License
010     * (GPL) as published by the Free Software Foundation; either version 2
011     * of the License or (at your option) any later version.
012     *
013     * This program is distributed in the hope that it will be useful, but
014     * WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program, in a file called gpl.txt or license.txt.
020     * If not, write to the Free Software Foundation Inc.,
021     * 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
022     */
023    
024    package org.mycore.datamodel.metadata;
025    
026    import org.jdom.Namespace;
027    import org.mycore.common.MCRException;
028    
029    /**
030     * This class implements all methods for handling with the MCRMetaPersonName
031     * part of a metadata object. The MCRMetaPersonName class represents a natural
032     * person specified by a list of names.
033     * 
034     * @author J. Vogler
035     * @version $Revision: 14003 $ $Date: 2008-09-16 11:45:07 +0200 (Di, 16 Sep 2008) $
036     */
037    final public class MCRMetaPersonName extends MCRMetaDefault {
038        // MetaPerson data
039        private String firstname;
040    
041        private String callname;
042    
043        private String surname;
044    
045        private String fullname;
046    
047        private String academic;
048    
049        private String peerage;
050    
051        private String prefix;
052    
053        /**
054         * This is the constructor. <br>
055         * The language element was set to <b>en </b>. All other elemnts are set to
056         * an empty string.
057         */
058        public MCRMetaPersonName() {
059            super();
060            firstname = "";
061            callname = "";
062            surname = "";
063            fullname = "";
064            academic = "";
065            peerage = "";
066            prefix = "";
067        }
068    
069        /**
070         * This is the constructor. <br>
071         * The language element was set. If the value of <em>default_lang</em> is
072         * null, empty or false <b>en </b> was set. The subtag element was set to
073         * the value of <em>set_subtag<em>. If the value of <em>set_subtag</em>
074         * is null or empty an exception was throwed. The type element was set to
075         * the value of <em>set_type<em>, if it is null, an empty string was set
076         * to the type element. The firstname, callname, surname, academic and
077         * peerage element was set to the value of <em>set_...<em>, if they are null,
078         * an empty string was set to this element.
079         *
080         * @param set_datapart     the global part of the elements like 'metadata'
081         *                         or 'service'
082         * @param set_subtag      the name of the subtag
083         * @param default_lang    the default language
084         * @param set_type        the optional type string
085         * @param set_inherted    a value >= 0
086         * @param set_firstname   the first name
087         * @param set_callname    the callname
088         * @param set_surname     the surname
089         * @param set_fullname    the fullname
090         * @param set_academic    the academic title
091         * @param set_peerage     the peerage title
092         * @param set_prefix      the prefix title
093         * @exception MCRException if the parameter values are invalid
094         */
095        public MCRMetaPersonName(String set_datapart, String set_subtag, String default_lang, String set_type, int set_inherted, String set_firstname, String set_callname, String set_surname, String set_fullname, String set_academic, String set_peerage, String set_prefix) throws MCRException {
096            super(set_datapart, set_subtag, default_lang, set_type, set_inherted);
097            firstname = "";
098            callname = "";
099            surname = "";
100            fullname = "";
101            academic = "";
102            peerage = "";
103            prefix = "";
104            set(set_firstname, set_callname, set_surname, set_fullname, set_academic, set_peerage, set_prefix);
105        }
106    
107        /**
108         * This methode set all name componets.
109         * 
110         * @param set_firstname
111         *            the first name
112         * @param set_callname
113         *            the callname
114         * @param set_surname
115         *            the surname
116         * @param set_fullname
117         *            the fullname
118         * @param set_academic
119         *            the academic title
120         * @param set_peerage
121         *            the peerage title
122         * @param set_prefix
123         *            the prefix title
124         */
125        public final void set(String set_firstname, String set_callname, String set_surname, String set_fullname, String set_academic, String set_peerage, String set_prefix) {
126            if ((set_firstname == null) || (set_callname == null) || (set_surname == null) || (set_fullname == null) || (set_academic == null) || (set_peerage == null) || (set_prefix == null)) {
127                throw new MCRException("One or more parameter(s) are null.");
128            }
129    
130            firstname = set_firstname.trim();
131            callname = set_callname.trim();
132            surname = set_surname.trim();
133            fullname = set_fullname.trim();
134            academic = set_academic.trim();
135            peerage = set_peerage.trim();
136            prefix = set_prefix.trim();
137        }
138    
139        /**
140         * This method get the firstname text element.
141         * 
142         * @return the firstname
143         */
144        public final String getFirstName() {
145            return firstname;
146        }
147    
148        /**
149         * This method get the callname text element.
150         * 
151         * @return the callname
152         */
153        public final String getCallName() {
154            return callname;
155        }
156    
157        /**
158         * This method get the surname text element.
159         * 
160         * @return the surname
161         */
162        public final String getSurName() {
163            return surname;
164        }
165    
166        /**
167         * This method get the fullname text element.
168         * 
169         * @return the fullname
170         */
171        public final String getFullName() {
172            return fullname;
173        }
174    
175        /**
176         * This method get the academic text element.
177         * 
178         * @return the academic
179         */
180        public final String getAcademic() {
181            return academic;
182        }
183    
184        /**
185         * This method get the peerage text element.
186         * 
187         * @return the peerage
188         */
189        public final String getPeerage() {
190            return peerage;
191        }
192    
193        /**
194         * This method get the prefix text element.
195         * 
196         * @return the prefix
197         */
198        public final String getPrefix() {
199            return prefix;
200        }
201    
202        /**
203         * This method reads the XML input stream part from a DOM part for the
204         * metadata of the document.
205         * 
206         * @param element
207         *            a relevant JDOM element for the metadata
208         */
209        public final void setFromDOM(org.jdom.Element element) {
210            super.setFromDOM(element);
211            firstname = element.getChildTextTrim("firstname");
212    
213            if (firstname == null) {
214                firstname = "";
215            }
216    
217            callname = element.getChildTextTrim("callname");
218    
219            if (callname == null) {
220                callname = "";
221            }
222    
223            surname = element.getChildTextTrim("surname");
224    
225            if (surname == null) {
226                surname = "";
227            }
228    
229            fullname = element.getChildTextTrim("fullname");
230    
231            if (fullname == null) {
232                fullname = "";
233            }
234    
235            academic = element.getChildTextTrim("academic");
236    
237            if (academic == null) {
238                academic = "";
239            }
240    
241            peerage = element.getChildTextTrim("peerage");
242    
243            if (peerage == null) {
244                peerage = "";
245            }
246    
247            prefix = element.getChildTextTrim("prefix");
248    
249            if (prefix == null) {
250                prefix = "";
251            }
252        }
253    
254        /**
255         * This method creates a XML stream for all data in this class, defined by
256         * the MyCoRe XML MCRMetaPersonName definition for the given subtag.
257         * 
258         * @exception MCRException
259         *                if the content of this class is not valid
260         * @return a JDOM Element with the XML MCRMetaPersonName part
261         */
262        public final org.jdom.Element createXML() throws MCRException {
263            if (!isValid()) {
264                debug();
265                throw new MCRException("The content of MCRMetaPersonName is not valid.");
266            }
267    
268            org.jdom.Element elm = new org.jdom.Element(subtag);
269            elm.setAttribute("lang", lang, Namespace.XML_NAMESPACE);
270            elm.setAttribute("inherited", Integer.toString(inherited));
271    
272            if ((type != null) && ((type = type.trim()).length() != 0)) {
273                elm.setAttribute("type", type);
274            }
275    
276            if ((firstname = firstname.trim()).length() != 0) {
277                elm.addContent(new org.jdom.Element("firstname").addContent(firstname));
278            }
279    
280            if ((callname = callname.trim()).length() != 0) {
281                elm.addContent(new org.jdom.Element("callname").addContent(callname));
282            }
283    
284            if ((fullname = fullname.trim()).length() != 0) {
285                elm.addContent(new org.jdom.Element("fullname").addContent(fullname));
286            }
287    
288            if ((surname = surname.trim()).length() != 0) {
289                elm.addContent(new org.jdom.Element("surname").addContent(surname));
290            }
291    
292            if ((academic = academic.trim()).length() != 0) {
293                elm.addContent(new org.jdom.Element("academic").addContent(academic));
294            }
295    
296            if ((peerage = peerage.trim()).length() != 0) {
297                elm.addContent(new org.jdom.Element("peerage").addContent(peerage));
298            }
299    
300            if ((prefix = prefix.trim()).length() != 0) {
301                elm.addContent(new org.jdom.Element("prefix").addContent(prefix));
302            }
303    
304            return elm;
305        }
306    
307        /**
308         * This method checks the validation of the content of this class. The
309         * method returns <em>false</em> if
310         * <ul>
311         * <li>the firstname is empty and
312         * <li>the surname is empty
313         * </ul>
314         * otherwise the method returns <em>true</em>.
315         * 
316         * @return a boolean value
317         */
318        public final boolean isValid() {
319            if ((firstname = firstname.trim()).length() == 0) {
320                firstname = callname;
321            }
322    
323            if ((callname = callname.trim()).length() == 0) {
324                callname = firstname;
325            }
326    
327            if (((firstname = firstname.trim()).length() == 0) && ((surname = surname.trim()).length() == 0)) {
328                return false;
329            }
330    
331            if ((fullname = fullname.trim()).length() == 0) {
332                StringBuffer sb = new StringBuffer(128);
333                sb.append(academic).append(' ').append(peerage).append(' ').append(firstname).append(' ').append(prefix).append(' ').append(surname);
334                fullname = sb.toString();
335            }
336    
337            return true;
338        }
339    
340        /**
341         * This method make a clone of this class.
342         */
343        public final Object clone() {
344            return new MCRMetaPersonName(datapart, subtag, lang, type, inherited, firstname, callname, surname, fullname, academic, peerage, prefix);
345        }
346    
347        /**
348         * This method put debug data to the logger (for the debug mode).
349         */
350        public final void debug() {
351            super.debugDefault();
352            LOGGER.debug("Firstname          = " + firstname);
353            LOGGER.debug("Callname           = " + callname);
354            LOGGER.debug("Surname            = " + surname);
355            LOGGER.debug("Fullname           = " + fullname);
356            LOGGER.debug("Academic           = " + academic);
357            LOGGER.debug("Peerage            = " + peerage);
358            LOGGER.debug("Prefix             = " + prefix);
359            LOGGER.debug("");
360        }
361    }