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.orgunit;
017
018import java.util.List;
019import java.util.Optional;
020
021import javax.jcr.Node;
022
023import org.apache.commons.lang3.StringUtils;
024
025import org.ametys.cms.data.ContentDataHelper;
026import org.ametys.cms.data.ContentValue;
027import org.ametys.cms.data.RichText;
028import org.ametys.cms.repository.ModifiableDefaultContent;
029import org.ametys.odf.cdmfr.CDMEntity;
030import org.ametys.odf.content.code.DisplayCodeProperty;
031import org.ametys.plugins.repository.AmetysRepositoryException;
032import org.ametys.plugins.repository.data.external.ExternalizableDataProvider.ExternalizableDataStatus;
033import org.ametys.plugins.repository.data.holder.values.ValueContext;
034
035/**
036 * OrgUnit java object
037 */
038public class OrgUnit extends ModifiableDefaultContent<OrgUnitFactory> implements CDMEntity
039{
040    /** prefix for the code attribute */
041    public static final String CODE_PREFIX = "orgunit-";
042    /** code RNE attribute. */
043    public static final String CODE_UAI = "codeUAI";
044    /** SIRET attribute. */
045    public static final String SIRET = "siret";
046    /** Activity number attribute. */
047    public static final String ACTIVITY_NUMBER = "activityNumber";
048    /** acronym attribute. */
049    public static final String ACRONYM = "acronym";
050    /** description attribute. */
051    public static final String DESCRIPTION = "description";
052    /** admission description attribute. */
053    public static final String ADMISSION_INFO = "admissionInfo";
054    /** regulations attribute. */
055    public static final String REGULATIONS = "regulations";
056    /** expenses attribute. */
057    public static final String EXPENSES = "expenses";
058    /** universal adjustement attribute. */
059    public static final String UNIVERSAL_ADJUSTMENT = "universalAdjustment";
060    /** student facilities attribute. */
061    public static final String STUDENT_FACILITIES = "studentFacilities";
062    /** additional data attribute. */
063    public static final String ADDITIONNAL_INFOS = "additionalInfos";
064    /** web link attribute. */
065    public static final String WEB_LINK_LABEL = "webLinkLabel";
066    /** web link  url attribute. */
067    public static final String WEB_LINK_URL = "webLinkUrl";
068    /** attribute holding the parent orgunit */
069    public static final String PARENT_ORGUNIT = "parentOrgUnit";
070    /** attribute holding the contact */
071    public static final String CONTACTS = "contactsReferences";
072    /** attribute holding the child orgunits */
073    public static final String CHILD_ORGUNITS = "childOrgUnits";
074    /** OrgUnit type */
075    public static final String TYPE = "type";
076
077    /**
078     * Constructor
079     * @param node The JCR node
080     * @param parentPath The parent path
081     * @param factory The factory
082     */
083    public OrgUnit(Node node, String parentPath, OrgUnitFactory factory)
084    {
085        super(node, parentPath, factory);
086    }
087
088    // --------------------------------------------------------------------------------------//
089    //
090    // SUB ORGUNITS
091    //
092    // --------------------------------------------------------------------------------------//
093
094    /**
095     * Return a List of orgUnits IDs up to date, Each ID is checked to
096     * remove deleted elements
097     * @return List&lt;String&gt;
098     */
099    public List<String> getSubOrgUnits()
100    {
101        return ContentDataHelper.getContentIdsListFromMultipleContentData(this, CHILD_ORGUNITS);
102    }
103
104    /**
105     * Get the id of parent {@link OrgUnit}
106     * @return the id of parent {@link OrgUnit} or null;
107     */
108    public OrgUnit getParentOrgUnit()
109    {
110        return Optional.ofNullable((ContentValue) getValue(PARENT_ORGUNIT))
111                .flatMap(ContentValue::getContentIfExists)
112                .map(OrgUnit.class::cast)
113                .orElse(null);
114    }
115    
116    // --------------------------------------------------------------------------------------//
117    //
118    // CONTACTS
119    //
120    // --------------------------------------------------------------------------------------//
121
122    /**
123     * Return a List of contact IDs
124     * @return a list of uuid
125     */
126    public List<String> getContacts()
127    {
128        return ContentDataHelper.getContentIdsListFromMultipleContentData(this, CONTACTS);
129    }
130    
131    /**
132     * Return a List of local contact IDs
133     * @return a list of uuid
134     */
135    public List<String> getLocalContacts()
136    {
137        ValueContext localValueContext = ValueContext.newInstance().withStatus(ExternalizableDataStatus.LOCAL);
138        return ContentDataHelper.getContentIdsListFromMultipleContentData(this, CONTACTS, localValueContext);
139    }
140    
141    /**
142     * Return a List of remote contact IDs
143     * @return a list of uuid
144     */
145    public List<String> getRemoteContacts()
146    {
147        ValueContext externalValueContext = ValueContext.newInstance().withStatus(ExternalizableDataStatus.EXTERNAL);
148        return ContentDataHelper.getContentIdsListFromMultipleContentData(this, CONTACTS, externalValueContext);
149    }
150    
151    
152    // --------------------------------------------------------------------------------------//
153    //
154    // GETTERS AND SETTERS
155    //
156    // --------------------------------------------------------------------------------------//
157    /**
158     * Get the code to display
159     * @return the code to display
160     */
161    public String getDisplayCode()
162    {
163        return getValue(DisplayCodeProperty.PROPERTY_NAME, false, StringUtils.EMPTY);
164    }
165    
166    /**
167     * Get the UAI code
168     * @return the UAI code
169     * @throws AmetysRepositoryException if failed to get metadata
170     */
171    public String getUAICode() throws AmetysRepositoryException
172    {
173        return getValue(CODE_UAI, false, StringUtils.EMPTY);
174    }
175    
176    /**
177     * Get the SIRET
178     * @return the SIRET
179     * @throws AmetysRepositoryException if failed to get metadata
180     */
181    public String getSIRET() throws AmetysRepositoryException
182    {
183        return getValue(SIRET, false, StringUtils.EMPTY);
184    }
185    
186    /**
187     * Get the orgunit type
188     * @return the orgunit type
189     * @throws AmetysRepositoryException if failed to get metadata
190     */
191    public String getType() throws AmetysRepositoryException
192    {
193        return ContentDataHelper.getContentIdFromContentData(this, TYPE);
194    }
195    
196    /**
197     * Return the metadata code
198     * 
199     * @return the acronym
200     */
201    public String getAcronym()
202    {
203        return getValue(OrgUnit.ACRONYM, false, StringUtils.EMPTY);
204    }
205
206    /**
207     * Return the description
208     * @return the description
209     */
210    public RichText getDescription()
211    {
212        return getValue(OrgUnit.DESCRIPTION);
213    }
214
215    /**
216     * Return the metadata admission_info
217     * @return the admission_info
218     */
219    public RichText getAdmissionInfo()
220    {
221        return getValue(OrgUnit.ADMISSION_INFO);
222    }
223
224    /**
225     * Return the regulations
226     * @return the regulations
227     */
228    public RichText getRegulations()
229    {
230        return getValue(OrgUnit.REGULATIONS);
231    }
232
233    /**
234     * Return the metadata Expenses
235     * @return the Expenses
236     */
237    public RichText getExpenses()
238    {
239        return getValue(OrgUnit.EXPENSES);
240    }
241
242    /**
243     * Return the metadata universalAdjustment
244     * @return the universalAdjustment
245     */
246    public RichText getUniversalAdjustment()
247    {
248        return getValue(OrgUnit.UNIVERSAL_ADJUSTMENT);
249    }
250
251    /**
252     * Return the metadata student_facilities
253     * 
254     * @return the student_facilities
255     */
256    public RichText getStudentFacilities()
257    {
258        return getValue(OrgUnit.STUDENT_FACILITIES);
259    }
260
261    /**
262     * Return the metadata additionnal_infos
263     * 
264     * @return the additionnal_infos
265     */
266    public RichText getAdditionnalInfos()
267    {
268        return getValue(OrgUnit.ADDITIONNAL_INFOS);
269    }
270
271    /**
272     * Return the metadata web_link
273     * 
274     * @return the web_link
275     */
276    public String getWebLinkLabel()
277    {
278        return getValue(OrgUnit.WEB_LINK_LABEL, false, StringUtils.EMPTY);
279    }
280
281    /**
282     * Get the web link URL
283     * @return the web link URL or null
284     */
285    public String getWebLinkURL()
286    {
287        return getValue(WEB_LINK_URL, false, StringUtils.EMPTY);
288    }
289
290    // --------------------------------------------------------------------------------------//
291    // CDM-fr
292    // --------------------------------------------------------------------------------------//
293    @Override
294    public String getCDMId() 
295    {
296        String cdmCode = getCdmCode();
297        if (StringUtils.isEmpty(cdmCode))
298        {
299            return "FRUAI" + _getFactory()._getRootOrgUnitUAI() + "OU" + getUAICode();
300        }
301        return cdmCode;
302    }
303    
304    @Override
305    public String getCdmCode()
306    {
307        return getValue(CDM_CODE, false, StringUtils.EMPTY);
308    }
309    
310    @Override
311    public void setCdmCode(String cdmCode) 
312    {
313        setValue(CDM_CODE, cdmCode);
314    }
315    
316}