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