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.lang.StringUtils;
021
022import org.ametys.cms.content.external.ExternalizableMetadataHelper;
023import org.ametys.cms.content.external.ExternalizableMetadataProvider.ExternalizableMetadataStatus;
024import org.ametys.cms.repository.ModifiableDefaultContent;
025import org.ametys.odf.cdmfr.CDMEntity;
026import org.ametys.plugins.repository.AmetysRepositoryException;
027import org.ametys.plugins.repository.metadata.RichText;
028import org.ametys.plugins.repository.metadata.UnknownMetadataException;
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 property. */
042    public static final String NAME = "lastName";
043
044    /** Given name property. */
045    public static final String GIVEN_NAME = "givenName";
046    
047    /** Academic title property. */
048    public static final String PERSON_TITLE = "personTitle";
049
050    /** ROLE LABEL property. */
051    public static final String ROLE = "role";
052
053    /** Additionnal informations property. */
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 getMetadataHolder().getString(Person.LOGIN, "");
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 getMetadataHolder().getString(NAME, "");
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 getMetadataHolder().getString(GIVEN_NAME, "");
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(getMetadataHolder().getString(ContactData.ADDRESS, ""));
159        contact.setAdditionalAddress(getMetadataHolder().getString(ContactData.ADDITIONAL_ADDRESS, ""));
160        contact.setZipCode(getMetadataHolder().getString(ContactData.ZIP_CODE, ""));
161        contact.setTown(getMetadataHolder().getString(ContactData.TOWN, ""));
162        contact.setPhone(getMetadataHolder().getString(ContactData.PHONE, ""));
163        contact.setFax(getMetadataHolder().getString(ContactData.FAX, ""));
164        contact.setMail(getMetadataHolder().getString(ContactData.MAIL, ""));
165        contact.setWebLinkLabel(getMetadataHolder().getString(ContactData.WEB_LINK_LABEL, ""));
166        contact.setWebLinkUrl(getMetadataHolder().getString(ContactData.WEB_LINK_URL, ""));
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 getMetadataHolder().getString(PERSON_TITLE, "");
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 getMetadataHolder().getStringArray(ROLE, new String[0]);
203    }
204    
205    
206    /**
207     * Return the additionnals informations
208     * @return the description
209     */
210    public RichText getAdditionalInformations()
211    {
212        try
213        {
214            return getMetadataHolder().getRichText(Person.ADDITIONAL_INFORMATIONS);
215        }
216        catch (UnknownMetadataException e)
217        {
218            return null;
219        }
220    }
221    
222    // --------------------------------------------------------------------------------------//
223    // CDM-fr
224    // --------------------------------------------------------------------------------------//
225    @Override
226    public String getCDMId() 
227    {
228        String cdmCode = getCdmCode();
229        if (StringUtils.isEmpty(cdmCode))
230        {
231            String login = getLogin();
232            return "FRUAI" + _getFactory()._getRootOrgUnitRNE() + "PE" + (StringUtils.isNotEmpty(login) ? login.toUpperCase() : getName().toUpperCase());
233        }
234        return cdmCode;
235    }
236    
237    @Override
238    public String getCdmCode()
239    {
240        return getMetadataHolder().getString(CDM_CODE, "");
241    }
242    
243    @Override
244    public void setCdmCode(String cdmCode) 
245    {
246        getMetadataHolder().setMetadata(CDM_CODE, cdmCode);
247    }
248}