001/*
002 *  Copyright 2010 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.odf.person;
017
018import javax.jcr.Node;
019
020import org.apache.commons.lang3.ArrayUtils;
021import org.apache.commons.lang3.StringUtils;
022
023import org.ametys.cms.data.RichText;
024import org.ametys.cms.repository.ModifiableDefaultContent;
025import org.ametys.odf.cdmfr.CDMEntity;
026
027/**
028 * Class representing a {@link Person}
029 */
030public class Person extends ModifiableDefaultContent<PersonFactory> implements CDMEntity
031{
032    /** ldap uid */
033    public static final String LDAP_UID = "ldapUid";
034    
035    /** Mandatory Identifier to generate the CDM-fr id */
036    public static final String LOGIN = "login";
037
038    /** Name attribute. */
039    public static final String NAME = "lastName";
040
041    /** Given name attribute. */
042    public static final String GIVEN_NAME = "givenName";
043    
044    /** Academic title attribute. */
045    public static final String PERSON_TITLE = "personTitle";
046
047    /** ROLE LABEL attribute. */
048    public static final String ROLE = "role";
049
050    /** Additionnal informations attribute. */
051    public static final String ADDITIONAL_INFORMATIONS = "additionalInformations";
052
053    /**
054     * Constructor
055     * @param node The JCR node
056     * @param parentPath The parent path
057     * @param factory the factory
058     */
059    public Person(Node node, String parentPath, PersonFactory factory)
060    {
061        super(node, parentPath, factory);
062    }
063
064    // --------------------------------------------------------------------------------------//
065    //
066    // GETTERS AND SETTERS
067    //
068    // --------------------------------------------------------------------------------------//
069
070    /**
071     * Get the login of the person
072     * This identifier will be the CDM-fr id of the person
073     * @return the login or null
074     */
075    public String getLogin()
076    {
077        return getValue(Person.LOGIN, false, StringUtils.EMPTY);
078    }
079    
080    /**
081     * Get the lastname of the person
082     * @return the Name or null
083     */
084    public String getLastName()
085    {
086        return getValue(NAME, false, StringUtils.EMPTY);
087    }
088    
089    /**
090     * Get the given name of the person
091     * @return the given name or null
092     */
093    public String getGivenName()
094    {
095        return getValue(GIVEN_NAME, false, StringUtils.EMPTY);
096    }
097
098    /**
099     * Get the contact data informations
100     * @return ContactData
101     */
102    public ContactData getContactData()
103    {
104        ContactData contact = new ContactData();
105        contact.setAddress(getValue(ContactData.ADDRESS, false, StringUtils.EMPTY));
106        contact.setAdditionalAddress(getValue(ContactData.ADDITIONAL_ADDRESS, false, StringUtils.EMPTY));
107        contact.setZipCode(getValue(ContactData.ZIP_CODE, false, StringUtils.EMPTY));
108        contact.setTown(getValue(ContactData.TOWN, false, StringUtils.EMPTY));
109        contact.setPhone(getValue(ContactData.PHONE, false, StringUtils.EMPTY));
110        contact.setFax(getValue(ContactData.FAX, false, StringUtils.EMPTY));
111        contact.setMail(getValue(ContactData.MAIL, false, StringUtils.EMPTY));
112        contact.setWebLinkLabel(getValue(ContactData.WEB_LINK_LABEL, false, StringUtils.EMPTY));
113        contact.setWebLinkUrl(getValue(ContactData.WEB_LINK_URL, false, StringUtils.EMPTY));
114        return contact;
115    }
116
117    /**
118     * Get the title posessed by the person
119     * @return the person title
120     */
121    public String getPersonTitle()
122    {
123        return getValue(PERSON_TITLE, false, StringUtils.EMPTY);
124    }
125
126    /**
127     * Get the role informations, a person may have different roles in different contexts, e.g. lecturer.
128     * @return role
129     */
130    public String[] getRole()
131    {
132        return getValue(ROLE, false, ArrayUtils.EMPTY_STRING_ARRAY);
133    }
134    
135    
136    /**
137     * Return the additionnals informations
138     * @return the description
139     */
140    public RichText getAdditionalInformations()
141    {
142        return getValue(Person.ADDITIONAL_INFORMATIONS);
143    }
144    
145    // --------------------------------------------------------------------------------------//
146    // CDM-fr
147    // --------------------------------------------------------------------------------------//
148    @Override
149    public String getCDMId() 
150    {
151        String cdmCode = getCdmCode();
152        if (StringUtils.isEmpty(cdmCode))
153        {
154            String login = getLogin();
155            return "FRUAI" + _getFactory()._getRootOrgUnitRNE() + "PE" + (StringUtils.isNotEmpty(login) ? login.toUpperCase() : getName().toUpperCase());
156        }
157        return cdmCode;
158    }
159    
160    @Override
161    public String getCdmCode()
162    {
163        return getValue(CDM_CODE, false, StringUtils.EMPTY);
164    }
165    
166    @Override
167    public void setCdmCode(String cdmCode) 
168    {
169        setValue(CDM_CODE, cdmCode);
170    }
171}