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.Arrays;
019import java.util.List;
020import java.util.Optional;
021import java.util.stream.Collectors;
022
023import javax.jcr.Node;
024
025import org.apache.commons.lang3.ArrayUtils;
026
027import org.ametys.cms.data.ContentDataHelper;
028import org.ametys.cms.data.ContentValue;
029import org.ametys.cms.data.RichText;
030import org.ametys.odf.courselist.CourseList;
031import org.ametys.odf.courselist.CourseListContainer;
032import org.ametys.plugins.repository.AmetysRepositoryException;
033
034/**
035 * Container java object
036 */
037public class Container extends AbstractTraversableProgramPart<ContainerFactory> implements CourseListContainer
038{
039
040    /** Constants for ects attribute */
041    public static final String ECTS = "ects";
042
043    /** Constants for description attribute */
044    public static final String DESCRIPTION = "description";
045
046    /** Constants for period attribute */
047    public static final String PERIOD = "period";
048
049    /** Constants for nature attribute* */
050    public static final String NATURE = "nature";
051    
052    /** Constants for orgunit attribute */
053    public static final String ORG_UNIT = "orgUnit";
054
055    /**
056     * Constructor
057     * @param node The JCR node
058     * @param parentPath The parent path
059     * @param factory the ametys object factory
060     */
061    public Container(Node node, String parentPath, ContainerFactory factory)
062    {
063        super(node, parentPath, factory);
064    }
065    
066    // --------------------------------------------------------------------------------------//
067    // GETTERS AND SETTERS
068    // --------------------------------------------------------------------------------------//
069    /**
070     * Get the ECTS credits
071     * @return the ECTS credits 
072     * @throws AmetysRepositoryException if an error occurred
073     */
074    public double getEcts() throws AmetysRepositoryException
075    {
076        return getValue(ECTS, false, 0D);
077    }
078
079    /**
080     * Get the description
081     * @return the description or null if not set
082     */
083    public RichText getDescription()
084    {
085        return getValue(DESCRIPTION);
086    }
087    
088    /**
089     * Get the period
090     * @return the period
091     * @throws AmetysRepositoryException if an error occurs
092     */
093    public String getPeriod() throws AmetysRepositoryException
094    {
095        return ContentDataHelper.getContentIdFromContentData(this, PERIOD);
096    }
097
098    /**
099     * Get the nature
100     * @return the nature
101     * @throws AmetysRepositoryException if an error occurs
102     */
103    public String getNature() throws AmetysRepositoryException
104    {
105        return ContentDataHelper.getContentIdFromContentData(this, NATURE);
106    }
107    
108    // --------------------------------------------------------------------------------------//
109    // Methods of CourseListContainer
110    // --------------------------------------------------------------------------------------//
111    @Override
112    public List<CourseList> getCourseLists()
113    {
114        return Arrays.stream(getValue(CHILD_PROGRAM_PARTS, false, new ContentValue[0]))
115                .map(ContentValue::getContentIfExists)
116                .filter(Optional::isPresent)
117                .map(Optional::get)
118                // This cast is not checked because an exception must be thrown if the retrieved content is not a program part 
119                // TODO: change this behavior to throw our own exception and not a CassCastException
120                .map(ProgramPart.class::cast)
121                // Program parts that are not course lists are simply ignored
122                .filter(CourseList.class::isInstance)
123                .map(CourseList.class::cast)
124                .collect(Collectors.toList());
125    }
126
127    @Override
128    public boolean containsCourseList(String clId)
129    {
130        return ArrayUtils.contains(ContentDataHelper.getContentIdsArrayFromMultipleContentData(this, CHILD_PROGRAM_PARTS), clId);
131    }
132
133    @Override
134    public boolean hasCourseLists()
135    {
136        return !getCourseLists().isEmpty();
137    }
138    
139    // --------------------------------------------------------------------------------------//
140    // CDMfr
141    // --------------------------------------------------------------------------------------//
142    @Override
143    protected String getCDMType()
144    {
145        return "PR";
146    }
147    
148    // --------------------------------------------------------------------------------------//
149    // ORGUNITS
150    // --------------------------------------------------------------------------------------//
151    /**
152     * Return the id of orgunit content binded to this container, or <code>null</code> if there is not.
153     * @return the id of orgunit content binded to this container, or <code>null</code> if there is not.
154     */
155    public String getOrgUnit()
156    {
157        return ContentDataHelper.getContentIdFromContentData(this, ORG_UNIT, null);
158    }
159}