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    /**
053     * Constructor
054     * @param node The JCR node
055     * @param parentPath The parent path
056     * @param factory the ametys object factory
057     */
058    public Container(Node node, String parentPath, ContainerFactory factory)
059    {
060        super(node, parentPath, factory);
061    }
062    
063    // --------------------------------------------------------------------------------------//
064    // GETTERS AND SETTERS
065    // --------------------------------------------------------------------------------------//
066    /**
067     * Get the ECTS credits
068     * @return the ECTS credits
069     * @throws AmetysRepositoryException if an error occurred
070     */
071    public double getEcts() throws AmetysRepositoryException
072    {
073        return getValue(ECTS, false, 0D);
074    }
075
076    /**
077     * Get the description
078     * @return the description or null if not set
079     */
080    public RichText getDescription()
081    {
082        return getValue(DESCRIPTION);
083    }
084    
085    /**
086     * Get the period
087     * @return the period
088     * @throws AmetysRepositoryException if an error occurs
089     */
090    public String getPeriod() throws AmetysRepositoryException
091    {
092        return ContentDataHelper.getContentIdFromContentData(this, PERIOD);
093    }
094
095    /**
096     * Get the nature
097     * @return the nature
098     * @throws AmetysRepositoryException if an error occurs
099     */
100    public String getNature() throws AmetysRepositoryException
101    {
102        return ContentDataHelper.getContentIdFromContentData(this, NATURE);
103    }
104    
105    // --------------------------------------------------------------------------------------//
106    // Methods of CourseListContainer
107    // --------------------------------------------------------------------------------------//
108    @Override
109    public List<CourseList> getCourseLists()
110    {
111        return Arrays.stream(getValue(CHILD_PROGRAM_PARTS, false, new ContentValue[0]))
112                .map(ContentValue::getContentIfExists)
113                .flatMap(Optional::stream)
114                // This cast is not checked because an exception must be thrown if the retrieved content is not a program part
115                // TODO: change this behavior to throw our own exception and not a CassCastException
116                .map(ProgramPart.class::cast)
117                // Program parts that are not course lists are simply ignored
118                .filter(CourseList.class::isInstance)
119                .map(CourseList.class::cast)
120                .collect(Collectors.toList());
121    }
122
123    @Override
124    public boolean containsCourseList(String clId)
125    {
126        return ArrayUtils.contains(ContentDataHelper.getContentIdsArrayFromMultipleContentData(this, CHILD_PROGRAM_PARTS), clId);
127    }
128
129    @Override
130    public boolean hasCourseLists()
131    {
132        return !getCourseLists().isEmpty();
133    }
134    
135    // --------------------------------------------------------------------------------------//
136    // CDMfr
137    // --------------------------------------------------------------------------------------//
138    @Override
139    protected String getCDMType()
140    {
141        return "PR";
142    }
143}