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.ArrayList;
019import java.util.Arrays;
020import java.util.Collections;
021import java.util.List;
022
023import javax.jcr.Node;
024
025import org.apache.commons.lang.StringUtils;
026
027import org.ametys.cms.content.external.ExternalizableMetadataHelper;
028import org.ametys.cms.content.external.ExternalizableMetadataProvider.ExternalizableMetadataStatus;
029import org.ametys.cms.repository.ModifiableDefaultContent;
030import org.ametys.odf.cdmfr.CDMEntity;
031import org.ametys.plugins.repository.AmetysRepositoryException;
032import org.ametys.plugins.repository.UnknownAmetysObjectException;
033import org.ametys.plugins.repository.metadata.RichText;
034import org.ametys.plugins.repository.metadata.UnknownMetadataException;
035
036/**
037 * OrgUnit java object
038 */
039public class OrgUnit extends ModifiableDefaultContent<OrgUnitFactory> implements CDMEntity
040{
041    /** code property. */
042    public static final String METADATA_CODE = "code";
043    /** prefix for the code property */
044    public static final String CODE_PREFIX = "orgunit-";
045    /** code RNE property. */
046    public static final String METADATA_CODE_UAI = "codeUAI";
047    /** acronym property. */
048    public static final String METADATA_ACRONYM = "acronym";
049    /** description property. */
050    public static final String METADATA_DESCRIPTION = "description";
051    /** admission description property. */
052    public static final String METADATA_ADMISSION_INFO = "admissionInfo";
053    /** regulations property. */
054    public static final String METADATA_REGULATIONS = "regulations";
055    /** expenses property. */
056    public static final String METADATA_EXPENSES = "expenses";
057    /** universal adjustement property. */
058    public static final String METADATA_UNIVERSAL_ADJUSTMENT = "universalAdjustment";
059    /** student facilities property. */
060    public static final String METADATA_STUDENT_FACILITIES = "studentFacilities";
061    /** additional data property. */
062    public static final String METADATA_ADDITIONNAL_INFOS = "additionalInfos";
063    /** web link property. */
064    public static final String METADATA_WEB_LINK_LABEL = "webLinkLabel";
065    /** web link  url property. */
066    public static final String METADATA_WEB_LINK_URL = "webLinkUrl";
067    /** Metadata holding the parent orgunit */
068    public static final String METADATA_PARENT_ORGUNIT = "parentOrgUnit";
069    /** Metadata holding the contact */
070    public static final String METADATA_CONTACTS = "contactsReferences";
071    /** Metadata holding the child orgunits */
072    public static final String METADATA_CHILD_ORGUNITS = "childOrgUnits";
073    /** OrgUnit type */
074    public static final String METADATA_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     * Remove reference from local and remote metadata
089     * @param metadataName The metadata name
090     * @param value The value of reference to remove 
091     */
092    public void removeReference (String metadataName, String value)
093    {
094        String[] references = getMetadataHolder().getStringArray(metadataName, new String[0]);
095        List<String> refList = new ArrayList<>(Arrays.asList(references));
096        if (refList.contains(value))
097        {
098            refList.remove(value);
099        }
100        getMetadataHolder().setMetadata(metadataName, refList.toArray(new String[refList.size()]));
101        
102        String[] altReferences = getMetadataHolder().getStringArray(metadataName + ExternalizableMetadataHelper.ALTERNATIVE_SUFFIX, new String[0]);
103        List<String> remoteList = new ArrayList<>(Arrays.asList(altReferences));
104        if (remoteList.contains(value))
105        {
106            remoteList.remove(value);
107        }
108        getMetadataHolder().setMetadata(metadataName + ExternalizableMetadataHelper.ALTERNATIVE_SUFFIX, remoteList.toArray(new String[remoteList.size()]));
109    }
110    
111    // --------------------------------------------------------------------------------------//
112    //
113    // SUB ORGUNITS
114    //
115    // --------------------------------------------------------------------------------------//
116
117    /**
118     * Return a List of orgUnits IDs up to date, Each ID is checked to
119     * remove deleted elements
120     * @return List&lt;String&gt;
121     */
122    public List<String> getSubOrgUnits()
123    {
124        String[] orgunits = getMetadataHolder().getStringArray(METADATA_CHILD_ORGUNITS, new String[0]);
125        return new ArrayList<>(Arrays.asList(orgunits));
126    }
127
128    /**
129     * Add a sub orgnit
130     * @param id the sub orgunit id to add
131     */
132    public void addSubOrgUnit (String id)
133    {
134        String[] orgunits = new String[0];
135        try
136        {
137            orgunits = ExternalizableMetadataHelper.getStringArray(getMetadataHolder(), METADATA_CHILD_ORGUNITS, ExternalizableMetadataStatus.LOCAL);
138        }
139        catch (UnknownMetadataException e)
140        {
141            // Nothing
142        }
143        
144        List<String> orgunitsList = new ArrayList<>(Arrays.asList(orgunits));
145        orgunitsList.add(id);
146        
147        ExternalizableMetadataHelper.setLocalMetadata(getMetadataHolder(), METADATA_CHILD_ORGUNITS, orgunitsList.toArray(new String[orgunitsList.size()]), ExternalizableMetadataStatus.LOCAL);
148    }
149    
150    /**
151     * Delete a sub orgnit
152     * @param id the sub orgunit id to delete
153     */
154    public void removeSubOrgUnit (String id)
155    {
156        String[] orgunits = new String[0];
157        try
158        {
159            orgunits = ExternalizableMetadataHelper.getStringArray(getMetadataHolder(), METADATA_CHILD_ORGUNITS, ExternalizableMetadataStatus.LOCAL);
160        }
161        catch (UnknownMetadataException e)
162        {
163            // Nothing
164        }
165        
166        List<String> orgunitsList = new ArrayList<>(Arrays.asList(orgunits));
167        if (orgunitsList.contains(id))
168        {
169            orgunitsList.remove(id);
170        }
171        
172        ExternalizableMetadataHelper.setLocalMetadata(getMetadataHolder(), METADATA_CHILD_ORGUNITS, orgunitsList.toArray(new String[orgunitsList.size()]), ExternalizableMetadataStatus.LOCAL);
173    }
174    
175    /**
176     * Get the id of parent {@link OrgUnit}
177     * @return the id of parent {@link OrgUnit} or null;
178     */
179    public OrgUnit getParentOrgUnit()
180    {
181        try
182        {
183            String parentId = getMetadataHolder().getString(METADATA_PARENT_ORGUNIT, null);
184            if (StringUtils.isNotEmpty(parentId))
185            {
186                return _getFactory()._getResolver().resolveById(parentId);
187            }
188        }
189        catch (UnknownAmetysObjectException e)
190        {
191            // Nothing
192        }
193        
194        return null;
195    }
196    
197    // --------------------------------------------------------------------------------------//
198    //
199    // CONTACTS
200    //
201    // --------------------------------------------------------------------------------------//
202
203    /**
204     * Return a List of contact IDs
205     * @return a list of uuid
206     */
207    public List<String> getContacts()
208    {
209        String[] contacts = getMetadataHolder().getStringArray(METADATA_CONTACTS, new String[0]);
210        return new ArrayList<>(Arrays.asList(contacts));
211    }
212    
213    /**
214     * Return a List of local contact IDs
215     * @return a list of uuid
216     */
217    public List<String> getLocalContacts()
218    {
219        try
220        {
221            String[] contacts = ExternalizableMetadataHelper.getStringArray(getMetadataHolder(), METADATA_CONTACTS, ExternalizableMetadataStatus.LOCAL);
222            return new ArrayList<>(Arrays.asList(contacts));
223        }
224        catch (UnknownMetadataException e)
225        {
226            return Collections.EMPTY_LIST;
227        }
228    }
229    
230    /**
231     * Return a List of remote contact IDs
232     * @return a list of uuid
233     */
234    public List<String> getRemoteContacts()
235    {
236        try
237        {
238            String[] contacts = ExternalizableMetadataHelper.getStringArray(getMetadataHolder(), METADATA_CONTACTS, ExternalizableMetadataStatus.EXTERNAL);
239            return new ArrayList<>(Arrays.asList(contacts));
240        }
241        catch (UnknownMetadataException e)
242        {
243            return Collections.EMPTY_LIST;
244        }
245    }
246    
247    
248    // --------------------------------------------------------------------------------------//
249    //
250    // GETTERS AND SETTERS
251    //
252    // --------------------------------------------------------------------------------------//
253
254    /**
255     * Get the code
256     * @return The code
257     * @throws AmetysRepositoryException if failed to get metadata
258     */
259    public String getCode() throws AmetysRepositoryException
260    {
261        return getMetadataHolder().getString(OrgUnit.METADATA_CODE, "");
262    }
263
264    /**
265     * Set the code of the {@link OrgUnit}
266     * @param code The code
267     * @throws AmetysRepositoryException if failed to set metadata
268     */
269    public void setCode(String code) throws AmetysRepositoryException
270    {
271        if (_getFactory()._getSynchronizedMetadata(this).contains(OrgUnit.METADATA_CODE))
272        {
273            ExternalizableMetadataHelper.setLocalMetadata(getMetadataHolder(), OrgUnit.METADATA_CODE, code, ExternalizableMetadataStatus.LOCAL);
274        }
275        else
276        {
277            ExternalizableMetadataHelper.setMetadata(getMetadataHolder(), OrgUnit.METADATA_CODE, code);
278        }
279    }
280
281    /**
282     * Get the UAI code
283     * @return the UAI code
284     * @throws AmetysRepositoryException if failed to get metadata
285     */
286    public String getUAICode() throws AmetysRepositoryException
287    {
288        return getMetadataHolder().getString(OrgUnit.METADATA_CODE_UAI, "");
289    }
290    
291    /**
292     * Get the orgunit type
293     * @return the orgunit type
294     * @throws AmetysRepositoryException if failed to get metadata
295     */
296    public String getType() throws AmetysRepositoryException
297    {
298        return getMetadataHolder().getString(OrgUnit.METADATA_TYPE, "");
299    }
300
301    /**
302     * Set the UAI code
303     * @param rne the code
304     * @throws AmetysRepositoryException if failed to set metadata
305     */
306    public void setUAICode(String rne) throws AmetysRepositoryException
307    {
308        if (_getFactory()._getSynchronizedMetadata(this).contains(OrgUnit.METADATA_CODE_UAI))
309        {
310            ExternalizableMetadataHelper.setLocalMetadata(getMetadataHolder(), OrgUnit.METADATA_CODE_UAI, rne, ExternalizableMetadataStatus.LOCAL);
311        }
312        else
313        {
314            ExternalizableMetadataHelper.setMetadata(getMetadataHolder(), OrgUnit.METADATA_CODE_UAI, rne);
315        }
316    }
317    
318    /**
319     * Return the metadata code
320     * 
321     * @return the acronym
322     */
323    public String getAcronym()
324    {
325        return getMetadataHolder().getString(OrgUnit.METADATA_ACRONYM, "");
326    }
327
328    /**
329     * Return the description
330     * @return the description
331     */
332    public RichText getDescription()
333    {
334        try
335        {
336            return getMetadataHolder().getRichText(OrgUnit.METADATA_DESCRIPTION);
337        }
338        catch (UnknownMetadataException e)
339        {
340            return null;
341        }
342    }
343
344    /**
345     * Return the metadata admission_info
346     * @return the admission_info
347     */
348    public RichText getAdmissionInfo()
349    {
350        try
351        {
352            return getMetadataHolder().getRichText(OrgUnit.METADATA_ADMISSION_INFO);
353        }
354        catch (UnknownMetadataException e)
355        {
356            return null;
357        }
358    }
359
360    /**
361     * Return the regulations
362     * @return the regulations
363     */
364    public RichText getRegulations()
365    {
366        try
367        {
368            return getMetadataHolder().getRichText(OrgUnit.METADATA_REGULATIONS);
369        }
370        catch (UnknownMetadataException e)
371        {
372            return null;
373        }
374    }
375
376    /**
377     * Return the metadata Expenses
378     * @return the Expenses
379     */
380    public RichText getExpenses()
381    {
382        try
383        {
384            return getMetadataHolder().getRichText(OrgUnit.METADATA_EXPENSES);
385        }
386        catch (UnknownMetadataException e)
387        {
388            return null;
389        }
390    }
391
392    /**
393     * Return the metadata universalAdjustment
394     * @return the universalAdjustment
395     */
396    public RichText getUniversalAdjustment()
397    {
398        try
399        {
400            return getMetadataHolder().getRichText(OrgUnit.METADATA_UNIVERSAL_ADJUSTMENT);
401        }
402        catch (UnknownMetadataException e)
403        {
404            return null;
405        }
406    }
407
408    /**
409     * Return the metadata student_facilities
410     * 
411     * @return the student_facilities
412     */
413    public RichText getStudentFacilities()
414    {
415        try
416        {
417            return getMetadataHolder().getRichText(OrgUnit.METADATA_STUDENT_FACILITIES);
418        }
419        catch (UnknownMetadataException e)
420        {
421            return null;
422        }
423    }
424
425    /**
426     * Return the metadata additionnal_infos
427     * 
428     * @return the additionnal_infos
429     */
430    public RichText getAdditionnalInfos()
431    {
432        try
433        {
434            return getMetadataHolder().getRichText(OrgUnit.METADATA_ADDITIONNAL_INFOS);
435        }
436        catch (UnknownMetadataException e)
437        {
438            return null;
439        }
440    }
441
442    /**
443     * Return the metadata web_link
444     * 
445     * @return the web_link
446     */
447    public String getWebLinkLabel()
448    {
449        return getMetadataHolder().getString(OrgUnit.METADATA_WEB_LINK_LABEL, "");
450    }
451
452    /**
453     * Get the web link URL
454     * @return the web link URL or null
455     */
456    public String getWebLinkURL()
457    {
458        return getMetadataHolder().getString(METADATA_WEB_LINK_URL, "");
459    }
460
461    // --------------------------------------------------------------------------------------//
462    // CDM-fr
463    // --------------------------------------------------------------------------------------//
464    @Override
465    public String getCDMId() 
466    {
467        String cdmCode = getCdmCode();
468        if (StringUtils.isEmpty(cdmCode))
469        {
470            return "FRUAI" + _getFactory()._getRootOrgUnitUAI() + "OU" + getUAICode();
471        }
472        return cdmCode;
473    }
474    
475    @Override
476    public String getCdmCode()
477    {
478        return getMetadataHolder().getString(CDM_CODE, "");
479    }
480    
481    @Override
482    public void setCdmCode(String cdmCode) 
483    {
484        getMetadataHolder().setMetadata(CDM_CODE, cdmCode);
485    }
486    
487}