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