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.program;
017
018import java.util.ArrayList;
019import java.util.List;
020
021import javax.jcr.Node;
022
023import org.apache.commons.lang3.ArrayUtils;
024import org.apache.commons.lang3.StringUtils;
025
026import org.ametys.odf.courselist.CourseList;
027import org.ametys.odf.courselist.CourseListContainer;
028import org.ametys.plugins.repository.AmetysRepositoryException;
029import org.ametys.plugins.repository.UnknownAmetysObjectException;
030import org.ametys.plugins.repository.metadata.RichText;
031import org.ametys.plugins.repository.metadata.UnknownMetadataException;
032
033/**
034 * Container java object
035 */
036public class Container extends AbstractTraversableProgramPart<ContainerFactory> implements CourseListContainer
037{
038
039    /** Constants for code metadata */
040    public static final String CODE = "code";
041    
042    /** Constants for ects metadata */
043    public static final String ECTS = "ects";
044
045    /** Constants for description metadata */
046    public static final String DESCRIPTION = "description";
047
048    /** Constants for period metadata */
049    public static final String PERIOD = "period";
050
051    /** Constants for nature metadata* */
052    public static final String NATURE = "nature";
053
054    /**
055     * Constructor
056     * @param node The JCR node
057     * @param parentPath The parent path
058     * @param factory the ametys object factory
059     */
060    public Container(Node node, String parentPath, ContainerFactory factory)
061    {
062        super(node, parentPath, factory);
063    }
064    
065    // --------------------------------------------------------------------------------------//
066    // GETTERS AND SETTERS
067    // --------------------------------------------------------------------------------------//
068    /**
069     * Get the ECTS credits
070     * @return the ECTS credits 
071     * @throws AmetysRepositoryException if an error occurred
072     */
073    public double getEcts() throws AmetysRepositoryException
074    {
075        return getMetadataHolder().getDouble(ECTS, 0);
076    }
077
078    /**
079     * Get the description
080     * @return the description or null if not set
081     */
082    public RichText getDescription()
083    {
084        try
085        {
086            return getMetadataHolder().getRichText(DESCRIPTION);
087        }
088        catch (UnknownMetadataException e)
089        {
090            return null;
091        }
092    }
093    
094    /**
095     * Get the period
096     * @return the period
097     * @throws AmetysRepositoryException if an error occurs
098     */
099    public String getPeriod() throws AmetysRepositoryException
100    {
101        return getMetadataHolder().getString(PERIOD, StringUtils.EMPTY);
102    }
103
104    /**
105     * Get the nature
106     * @return the nature
107     * @throws AmetysRepositoryException if an error occurs
108     */
109    public String getNature() throws AmetysRepositoryException
110    {
111        return getMetadataHolder().getString(NATURE, StringUtils.EMPTY);
112    }
113    
114    // --------------------------------------------------------------------------------------//
115    // Methods of CourseListContainer
116    // --------------------------------------------------------------------------------------//
117    @Override
118    public List<CourseList> getCourseLists()
119    {
120        List<CourseList> children = new ArrayList<>();
121        
122        String[] childIds = getMetadataHolder().getStringArray(METADATA_PARENT_PROGRAM_PARTS, new String[0]);
123        for (String id : childIds)
124        {
125            try
126            {
127                ProgramPart child = _getFactory()._getResolver().resolveById(id);
128                if (child instanceof CourseList)
129                {
130                    children.add((CourseList) child);
131                }
132            }
133            catch (UnknownAmetysObjectException e)
134            {
135                // Nothing
136            }
137        }
138        
139        return children;
140    }
141
142    @Override
143    public boolean containsCourseList(String clId)
144    {
145        return ArrayUtils.contains(getMetadataHolder().getStringArray(METADATA_PARENT_PROGRAM_PARTS, new String[0]), clId);
146    }
147
148    @Override
149    public boolean hasCourseLists()
150    {
151        return !getCourseLists().isEmpty();
152    }
153    
154    // --------------------------------------------------------------------------------------//
155    // CDMfr
156    // --------------------------------------------------------------------------------------//
157    @Override
158    protected String getCDMType()
159    {
160        return "PR";
161    }
162}