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.content.external.ExternalizableMetadataHelper;
024import org.ametys.cms.content.external.ExternalizableMetadataProvider.ExternalizableMetadataStatus;
025import org.ametys.cms.data.RichText;
026import org.ametys.cms.repository.ModifiableDefaultContent;
027import org.ametys.odf.cdmfr.CDMEntity;
028import org.ametys.plugins.repository.AmetysRepositoryException;
029
030/**
031 * Class representing a {@link Person}
032 */
033public class Person extends ModifiableDefaultContent<PersonFactory> implements CDMEntity
034{
035    /** ldap uid */
036    public static final String LDAP_UID = "ldapUid";
037    
038    /** Mandatory Identifier to generate the CDM-fr id */
039    public static final String LOGIN = "login";
040
041    /** Name attribute. */
042    public static final String NAME = "lastName";
043
044    /** Given name attribute. */
045    public static final String GIVEN_NAME = "givenName";
046    
047    /** Academic title attribute. */
048    public static final String PERSON_TITLE = "personTitle";
049
050    /** ROLE LABEL attribute. */
051    public static final String ROLE = "role";
052
053    /** Additionnal informations attribute. */
054    public static final String ADDITIONAL_INFORMATIONS = "additionalInformations";
055
056    /**
057     * Constructor
058     * @param node The JCR node
059     * @param parentPath The parent path
060     * @param factory the factory
061     */
062    public Person(Node node, String parentPath, PersonFactory factory)
063    {
064        super(node, parentPath, factory);
065    }
066
067    // --------------------------------------------------------------------------------------//
068    //
069    // GETTERS AND SETTERS
070    //
071    // --------------------------------------------------------------------------------------//
072
073    /**
074     * Get the login of the person
075     * This identifier will be the CDM-fr id of the person
076     * @return the login or null
077     */
078    public String getLogin()
079    {
080        return getValue(Person.LOGIN, false, StringUtils.EMPTY);
081    }
082    /**
083     * Set the login of the person
084     * This identifier will be the CDM-fr id of the person
085     * @param login the login to set
086     * @throws AmetysRepositoryException if an error occurred
087     */
088    public void setLogin(String login) throws AmetysRepositoryException
089    {
090        if (_getFactory()._getSynchronizedMetadata(this).contains(LOGIN))
091        {
092            ExternalizableMetadataHelper.setLocalMetadata(getMetadataHolder(), LOGIN, login, ExternalizableMetadataStatus.LOCAL);
093        }
094        else
095        {
096            ExternalizableMetadataHelper.setMetadata(getMetadataHolder(), LOGIN, login);
097        }
098    }
099    
100    /**
101     * Get the lastname of the person
102     * @return the Name or null
103     */
104    public String getLastName()
105    {
106        return getValue(NAME, false, StringUtils.EMPTY);
107    }
108    /**
109     * Set the lastname of the person
110     * @param name the Name to set
111     * @throws AmetysRepositoryException if an error occurred
112     */
113    public void setLastName(String name) throws AmetysRepositoryException
114    {
115        if (_getFactory()._getSynchronizedMetadata(this).contains(NAME))
116        {
117            ExternalizableMetadataHelper.setLocalMetadata(getMetadataHolder(), NAME, name, ExternalizableMetadataStatus.LOCAL);
118        }
119        else
120        {
121            ExternalizableMetadataHelper.setMetadata(getMetadataHolder(), NAME, name);
122        }
123    }
124    
125    /**
126     * Get the given name of the person
127     * @return the given name or null
128     */
129    public String getGivenName()
130    {
131        return getValue(GIVEN_NAME, false, StringUtils.EMPTY);
132    }
133
134    /**
135     * Set the given name of the person
136     * @param name the Name to set
137     * @throws AmetysRepositoryException if an error occurred
138     */
139    public void setGivenName(String name) throws AmetysRepositoryException
140    {
141        if (_getFactory()._getSynchronizedMetadata(this).contains(GIVEN_NAME))
142        {
143            ExternalizableMetadataHelper.setLocalMetadata(getMetadataHolder(), GIVEN_NAME, name, ExternalizableMetadataStatus.LOCAL);
144        }
145        else
146        {
147            ExternalizableMetadataHelper.setMetadata(getMetadataHolder(), GIVEN_NAME, name);
148        }
149    }
150    
151    /**
152     * Get the contact data informations
153     * @return ContactData
154     */
155    public ContactData getContactData()
156    {
157        ContactData contact = new ContactData();
158        contact.setAddress(getValue(ContactData.ADDRESS, false, StringUtils.EMPTY));
159        contact.setAdditionalAddress(getValue(ContactData.ADDITIONAL_ADDRESS, false, StringUtils.EMPTY));
160        contact.setZipCode(getValue(ContactData.ZIP_CODE, false, StringUtils.EMPTY));
161        contact.setTown(getValue(ContactData.TOWN, false, StringUtils.EMPTY));
162        contact.setPhone(getValue(ContactData.PHONE, false, StringUtils.EMPTY));
163        contact.setFax(getValue(ContactData.FAX, false, StringUtils.EMPTY));
164        contact.setMail(getValue(ContactData.MAIL, false, StringUtils.EMPTY));
165        contact.setWebLinkLabel(getValue(ContactData.WEB_LINK_LABEL, false, StringUtils.EMPTY));
166        contact.setWebLinkUrl(getValue(ContactData.WEB_LINK_URL, false, StringUtils.EMPTY));
167        return contact;
168    }
169
170    /**
171     * Get the title posessed by the person
172     * @return the person title
173     */
174    public String getPersonTitle()
175    {
176        return getValue(PERSON_TITLE, false, StringUtils.EMPTY);
177    }
178
179    /**
180     * Set the title posessed by the person
181     * @param name the title to set
182     * @throws AmetysRepositoryException if an error occurred
183     */
184    public void setPersonTitle(String name) throws AmetysRepositoryException
185    {
186        if (_getFactory()._getSynchronizedMetadata(this).contains(PERSON_TITLE))
187        {
188            ExternalizableMetadataHelper.setLocalMetadata(getMetadataHolder(), PERSON_TITLE, name, ExternalizableMetadataStatus.LOCAL);
189        }
190        else
191        {
192            ExternalizableMetadataHelper.setMetadata(getMetadataHolder(), PERSON_TITLE, name);
193        }
194    }
195    
196    /**
197     * Get the role informations, a person may have different roles in different contexts, e.g. lecturer.
198     * @return role
199     */
200    public String[] getRole()
201    {
202        return getValue(ROLE, false, ArrayUtils.EMPTY_STRING_ARRAY);
203    }
204    
205    
206    /**
207     * Return the additionnals informations
208     * @return the description
209     */
210    public RichText getAdditionalInformations()
211    {
212        return getValue(Person.ADDITIONAL_INFORMATIONS);
213    }
214    
215    // --------------------------------------------------------------------------------------//
216    // CDM-fr
217    // --------------------------------------------------------------------------------------//
218    @Override
219    public String getCDMId() 
220    {
221        String cdmCode = getCdmCode();
222        if (StringUtils.isEmpty(cdmCode))
223        {
224            String login = getLogin();
225            return "FRUAI" + _getFactory()._getRootOrgUnitRNE() + "PE" + (StringUtils.isNotEmpty(login) ? login.toUpperCase() : getName().toUpperCase());
226        }
227        return cdmCode;
228    }
229    
230    @Override
231    public String getCdmCode()
232    {
233        return getValue(CDM_CODE, false, StringUtils.EMPTY);
234    }
235    
236    @Override
237    public void setCdmCode(String cdmCode) 
238    {
239        setValue(CDM_CODE, cdmCode);
240    }
241}