001    /*
002     * 
003     * $Revision: 14106 $ $Date: 2008-10-09 11:30:08 +0200 (Do, 09 Okt 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.user;
025    
026    import org.apache.log4j.Logger;
027    
028    /**
029     * Instances of this class store contact information of MyCoRe users.
030     * MCRUserContact is part of the MyCoRe user component and must not be used
031     * directly by other components. MCRUserContact is aggregated by MCRUser and all
032     * user objects are managed by the user manager (the instance of the singleton
033     * MCRUserMgr).
034     * 
035     * @see org.mycore.user.MCRUserMgr
036     * @see org.mycore.user.MCRUser
037     * 
038     * @author Detlev Degenhardt
039     * @author Jens Kupferschmidt
040     * @author Thomas Scheffler (yagee)
041     * @version $Revision: 14106 $ $Date: 2008-10-09 11:30:08 +0200 (Do, 09 Okt 2008) $
042     */
043    public class MCRUserContact {
044        private static Logger logger = Logger.getLogger(MCRUserContact.class.getName());
045    
046        /** The maximum length of the salutation string */
047        public final static int salutation_len = 24;
048    
049        /** The maximum length of the firstname string */
050        public final static int firstname_len = 64;
051    
052        /** The maximum length of the lastname string */
053        public final static int lastname_len = 32;
054    
055        /** The maximum length of the street string */
056        public final static int street_len = 64;
057    
058        /** The maximum length of the city string */
059        public final static int city_len = 32;
060    
061        /** The maximum length of the postalcode string */
062        public final static int postalcode_len = 32;
063    
064        /** The maximum length of the country string */
065        public final static int country_len = 32;
066    
067        /** The maximum length of the state string */
068        public final static int state_len = 32;
069    
070        /** The maximum length of the institution string */
071        public final static int institution_len = 64;
072    
073        /** The maximum length of the faculty string */
074        public final static int faculty_len = 64;
075    
076        /** The maximum length of the department string */
077        public final static int department_len = 64;
078    
079        /** The maximum length of the institute string */
080        public final static int institute_len = 64;
081    
082        /** The maximum length of the telephone string */
083        public final static int telephone_len = 32;
084    
085        /** The maximum length of the fax string */
086        public final static int fax_len = 32;
087    
088        /** The maximum length of the email string */
089        public final static int email_len = 64;
090    
091        /** The maximum length of the cellphone string */
092        public final static int cellphone_len = 32;
093    
094        private String salutation = "";
095    
096        private String firstname = "";
097    
098        private String lastname = "";
099    
100        private String street = "";
101    
102        private String city = "";
103    
104        private String postalcode = "";
105    
106        private String country = "";
107    
108        private String state = "";
109    
110        private String institution = "";
111    
112        private String faculty = "";
113    
114        private String department = "";
115    
116        private String institute = "";
117    
118        private String telephone = "";
119    
120        private String fax = "";
121    
122        private String email = "";
123    
124        private String cellphone = "";
125    
126        /**
127         * Constructor for an empty object.
128         */
129        public MCRUserContact() {
130            init();
131        }
132    
133        /**
134         * Constructor. All address and contact attributes are passed as Strings
135         */
136        public MCRUserContact(String salutation, String firstname, String lastname, String street, String city, String postalcode, String country, String state, String institution, String faculty, String department, String institute, String telephone, String fax, String email, String cellphone) {
137            init();
138            this.salutation = MCRUserObject.trim(salutation, salutation_len);
139            this.firstname = MCRUserObject.trim(firstname, firstname_len);
140            this.lastname = MCRUserObject.trim(lastname, lastname_len);
141            this.street = MCRUserObject.trim(street, street_len);
142            this.city = MCRUserObject.trim(city, city_len);
143            this.postalcode = MCRUserObject.trim(postalcode, postalcode_len);
144            this.country = MCRUserObject.trim(country, country_len);
145            this.state = MCRUserObject.trim(state, state_len);
146            this.institution = MCRUserObject.trim(institution, institution_len);
147            this.faculty = MCRUserObject.trim(faculty, faculty_len);
148            this.department = MCRUserObject.trim(department, department_len);
149            this.institute = MCRUserObject.trim(institute, institute_len);
150            this.telephone = MCRUserObject.trim(telephone, telephone_len);
151            this.fax = MCRUserObject.trim(fax, fax_len);
152            this.email = MCRUserObject.trim(email, email_len);
153            this.cellphone = MCRUserObject.trim(cellphone, cellphone_len);
154        }
155    
156        public Object clone() {
157            return new MCRUserContact(this.salutation, this.firstname, this.lastname, this.street, this.city, this.postalcode, this.country, this.state, this.institution, this.faculty, this.department, this.institute, this.telephone, this.fax, this.email, this.cellphone);
158        }
159    
160        /**
161         * Constructor. All address and contact attributes are provided as a JDOM
162         * Element.
163         * 
164         * @param elm
165         *            the JDOM Element
166         */
167        public MCRUserContact(org.jdom.Element elm) {
168            init();
169            setFromJDOMElement(elm);
170        }
171    
172        /**
173         * All address and contact attributes are passed as a JDOM Element.
174         * 
175         * @param elm
176         *            the JDOM Element
177         */
178        public final void setFromJDOMElement(org.jdom.Element elm) {
179            if (elm == null) {
180                return;
181            }
182    
183            this.salutation = MCRUserObject.trim(elm.getChildTextTrim("contact.salutation"), salutation_len);
184            this.firstname = MCRUserObject.trim(elm.getChildTextTrim("contact.firstname"), firstname_len);
185            this.lastname = MCRUserObject.trim(elm.getChildTextTrim("contact.lastname"), lastname_len);
186            this.street = MCRUserObject.trim(elm.getChildTextTrim("contact.street"), street_len);
187            this.city = MCRUserObject.trim(elm.getChildTextTrim("contact.city"), city_len);
188            this.postalcode = MCRUserObject.trim(elm.getChildTextTrim("contact.postalcode"), postalcode_len);
189            this.country = MCRUserObject.trim(elm.getChildTextTrim("contact.country"), country_len);
190            this.state = MCRUserObject.trim(elm.getChildTextTrim("contact.state"), state_len);
191            this.institution = MCRUserObject.trim(elm.getChildTextTrim("contact.institution"), institution_len);
192            this.faculty = MCRUserObject.trim(elm.getChildTextTrim("contact.faculty"), faculty_len);
193            this.department = MCRUserObject.trim(elm.getChildTextTrim("contact.department"), department_len);
194            this.institute = MCRUserObject.trim(elm.getChildTextTrim("contact.institute"), institute_len);
195            this.telephone = MCRUserObject.trim(elm.getChildTextTrim("contact.telephone"), telephone_len);
196            this.fax = MCRUserObject.trim(elm.getChildTextTrim("contact.fax"), fax_len);
197            this.email = MCRUserObject.trim(elm.getChildTextTrim("contact.email"), email_len);
198            this.cellphone = MCRUserObject.trim(elm.getChildTextTrim("contact.cellphone"), cellphone_len);
199        }
200    
201        /**
202         * This method initializes this object with empty attributes.
203         */
204        private final void init() {
205            salutation = "";
206            firstname = "";
207            lastname = "";
208            street = "";
209            city = "";
210            postalcode = "";
211            country = "";
212            state = "";
213            institution = "";
214            faculty = "";
215            department = "";
216            institute = "";
217            telephone = "";
218            fax = "";
219            email = "";
220            cellphone = "";
221        }
222    
223        /**
224         * The following methods simply return the attributes...
225         */
226        public final String getSalutation() {
227            return salutation;
228        }
229    
230        public final String getFirstName() {
231            return firstname;
232        }
233    
234        public final String getLastName() {
235            return lastname;
236        }
237    
238        public final String getStreet() {
239            return street;
240        }
241    
242        public final String getCity() {
243            return city;
244        }
245    
246        public final String getPostalCode() {
247            return postalcode;
248        }
249    
250        public final String getCountry() {
251            return country;
252        }
253    
254        public final String getState() {
255            return state;
256        }
257    
258        public final String getInstitution() {
259            return institution;
260        }
261    
262        public final String getFaculty() {
263            return faculty;
264        }
265    
266        public final String getDepartment() {
267            return department;
268        }
269    
270        public final String getInstitute() {
271            return institute;
272        }
273    
274        public final String getTelephone() {
275            return telephone;
276        }
277    
278        public final String getFax() {
279            return fax;
280        }
281    
282        public final String getEmail() {
283            return email;
284        }
285    
286        public final String getCellphone() {
287            return cellphone;
288        }
289    
290        public final void setSalutation(String v) {
291            this.salutation = v;
292        }
293    
294        public final void setFirstName(String v) {
295            this.firstname = v;
296        }
297    
298        public final void setLastName(String v) {
299            this.lastname = v;
300        }
301    
302        public final void setStreet(String v) {
303            this.street = v;
304        }
305    
306        public final void setCity(String v) {
307            this.city = v;
308        }
309    
310        public final void setPostalCode(String v) {
311            this.postalcode = v;
312        }
313    
314        public final void setCountry(String v) {
315            this.country = v;
316        }
317    
318        public final void setState(String v) {
319            this.state = v;
320        }
321    
322        public final void setInstitution(String v) {
323            this.institution = v;
324        }
325    
326        public final void setFaculty(String v) {
327            this.faculty = v;
328        }
329    
330        public final void setDepartment(String v) {
331            this.department = v;
332        }
333    
334        public final void setInstitute(String v) {
335            this.institute = v;
336        }
337    
338        public final void setTelephone(String v) {
339            this.telephone = v;
340        }
341    
342        public final void setFax(String v) {
343            this.fax = v;
344        }
345    
346        public final void setEmail(String v) {
347            this.email = v;
348        }
349    
350        public final void setCellphone(String v) {
351            this.cellphone = v;
352        }
353    
354        /**
355         * This method returns the user contact information as a JDOM Element. This
356         * output is used by the corresponding user object to create a full XML
357         * representation of the user.
358         * 
359         * @return JDOM Element including data fields of this class
360         */
361        public final org.jdom.Element toJDOMElement() {
362            org.jdom.Element address = new org.jdom.Element("user.contact");
363            org.jdom.Element Salutation = new org.jdom.Element("contact.salutation").setText(salutation);
364            org.jdom.Element Firstname = new org.jdom.Element("contact.firstname").setText(firstname);
365            org.jdom.Element Lastname = new org.jdom.Element("contact.lastname").setText(lastname);
366            org.jdom.Element Street = new org.jdom.Element("contact.street").setText(street);
367            org.jdom.Element City = new org.jdom.Element("contact.city").setText(city);
368            org.jdom.Element Postalcode = new org.jdom.Element("contact.postalcode").setText(postalcode);
369            org.jdom.Element Country = new org.jdom.Element("contact.country").setText(country);
370            org.jdom.Element State = new org.jdom.Element("contact.state").setText(state);
371            org.jdom.Element Institution = new org.jdom.Element("contact.institution").setText(institution);
372            org.jdom.Element Faculty = new org.jdom.Element("contact.faculty").setText(faculty);
373            org.jdom.Element Department = new org.jdom.Element("contact.department").setText(department);
374            org.jdom.Element Institute = new org.jdom.Element("contact.institute").setText(institute);
375            org.jdom.Element Telephone = new org.jdom.Element("contact.telephone").setText(telephone);
376            org.jdom.Element Fax = new org.jdom.Element("contact.fax").setText(fax);
377            org.jdom.Element Email = new org.jdom.Element("contact.email").setText(email);
378            org.jdom.Element Cellphone = new org.jdom.Element("contact.cellphone").setText(cellphone);
379    
380            // Aggregate address element
381            address.addContent(Salutation).addContent(Firstname).addContent(Lastname).addContent(Street).addContent(City).addContent(Postalcode).addContent(Country).addContent(State).addContent(Institution).addContent(Faculty).addContent(Department).addContent(Institute).addContent(Telephone).addContent(Fax).addContent(Email).addContent(Cellphone);
382    
383            return address;
384        }
385    
386        /**
387         * This method prints the attributes of this object when in debug mode.
388         */
389        public final void debug() {
390            logger.debug("Salutation      : " + salutation);
391            logger.debug("Firstname       : " + firstname);
392            logger.debug("Lastname        : " + lastname);
393            logger.debug("Street          : " + street);
394            logger.debug("City            : " + city);
395            logger.debug("Postalcode      : " + postalcode);
396            logger.debug("Country         : " + country);
397            logger.debug("State           : " + state);
398            logger.debug("Institution     : " + institution);
399            logger.debug("Faculty         : " + faculty);
400            logger.debug("Department      : " + department);
401            logger.debug("Institute       : " + institute);
402            logger.debug("Telephone       : " + telephone);
403            logger.debug("Fax             : " + fax);
404            logger.debug("Email           : " + email);
405            logger.debug("Cellphone       : " + cellphone);
406        }
407    
408        public boolean equals(Object obj) {
409            if (!(obj instanceof MCRUserContact)){
410                return false;
411            }
412            MCRUserContact uc=(MCRUserContact)obj;
413            if (this==uc){
414                return true;
415            }
416            if (this.hashCode()!=this.hashCode()){
417                //acording to the hashCode() contract
418                return false;
419            }
420            return fastEquals(uc);
421        }
422        
423        private boolean fastEquals(MCRUserContact uc){
424            return ((this.cellphone==uc.cellphone) &&
425                    ((this.city==uc.city) || (this.city.equals(uc.city))) && 
426                    ((this.country==uc.country) || (this.country.equals(uc.country))) && 
427                    ((this.department==uc.department) || (this.department.equals(uc.department))) && 
428                    ((this.email==uc.email) || (this.email.equals(uc.email))) && 
429                    ((this.faculty==uc.faculty) || (this.faculty.equals(uc.faculty))) && 
430                    ((this.fax==uc.fax) || (this.fax.equals(uc.fax))) && 
431                    ((this.firstname==uc.firstname) || (this.firstname.equals(uc.firstname))) && 
432                    ((this.institute==uc.institute) || (this.institute.equals(uc.institute))) && 
433                    ((this.institution==uc.institution) || (this.institution.equals(uc.institution))) && 
434                    ((this.lastname==uc.lastname) || (this.lastname.equals(uc.lastname))) && 
435                    ((this.postalcode==uc.postalcode) || (this.postalcode.equals(uc.postalcode))) && 
436                    ((this.salutation==uc.salutation) || (this.salutation.equals(uc.salutation))) && 
437                    ((this.state==uc.state) || (this.state.equals(uc.state))) && 
438                    ((this.street==uc.street) || (this.street.equals(uc.street))) && 
439                    ((this.telephone==uc.telephone) || (this.telephone.equals(uc.telephone)))
440                   );
441        }
442    
443        public int hashCode() {
444            int result=17;
445            result = 37*result+this.cellphone.hashCode();
446            result = 37*result+this.city.hashCode();
447            result = 37*result+this.department.hashCode();
448            result = 37*result+this.email.hashCode();
449            result = 37*result+this.faculty.hashCode();
450            result = 37*result+this.fax.hashCode();
451            result = 37*result+this.firstname.hashCode();
452            result = 37*result+this.institute.hashCode();
453            result = 37*result+this.institution.hashCode();
454            result = 37*result+this.lastname.hashCode();
455            result = 37*result+this.postalcode.hashCode();
456            result = 37*result+this.salutation.hashCode();
457            result = 37*result+this.state.hashCode();
458            result = 37*result+this.street.hashCode();
459            result = 37*result+this.telephone.hashCode();
460            return result;
461        }
462    }