001/*
002 *  Copyright 2018 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.coursepart;
017
018import java.util.ArrayList;
019import java.util.HashSet;
020import java.util.List;
021import java.util.Set;
022
023import javax.jcr.Node;
024
025import org.apache.commons.lang3.StringUtils;
026
027import org.ametys.cms.repository.ModifiableDefaultContent;
028import org.ametys.odf.course.Course;
029import org.ametys.odf.program.Program;
030import org.ametys.plugins.repository.AmetysRepositoryException;
031import org.ametys.plugins.repository.UnknownAmetysObjectException;
032
033/**
034 * Class representing a {@link CoursePart}
035 */
036public class CoursePart extends ModifiableDefaultContent<CoursePartFactory>
037{
038    /** The metadata holding the catalog */
039    public static final String METADATA_CATALOG = "catalog";
040    
041    /** The metadata holding the code */
042    public static final String METADATA_CODE = "code";
043    
044    /** Constants for metadata 'nature' */
045    public static final String METADATA_NATURE = "nature";
046
047    /** Constants for metadata 'nbHours' */
048    public static final String METADATA_NB_HOURS = "nbHours";
049
050    /** Name of metadata for course holder */
051    public static final String METADATA_COURSE_HOLDER = "courseHolder";
052
053    /** Name of metadata for parent courses */
054    public static final String METADATA_PARENT_COURSES = "courses";
055    
056    /**
057     * Constructor.
058     * @param node the JCR Node.
059     * @param parentPath the parent path
060     * @param factory the corresponding factory.
061     */
062    public CoursePart(Node node, String parentPath, CoursePartFactory factory)
063    {
064        super(node, parentPath, factory);
065    }
066    
067    /**
068     * Get the catalog.
069     * @return the catalog
070     */
071    public String getCatalog()
072    {
073        return getMetadataHolder().getString(METADATA_CATALOG, null);
074    }
075    
076    /**
077     * Set the catalog.
078     * @param catalog The catalog
079     * @throws AmetysRepositoryException if an error occurs
080     */
081    public void setCatalog(String catalog) throws AmetysRepositoryException
082    {
083        getMetadataHolder().setMetadata(METADATA_CATALOG, catalog);
084    }
085
086    /**
087     * Get the code.
088     * @return the code
089     */
090    public String getCode()
091    {
092        return getMetadataHolder().getString(METADATA_CODE, StringUtils.EMPTY);
093    }
094
095    /**
096     * Set the code.
097     * @param code The code
098     * @throws AmetysRepositoryException if an error occurs
099     */
100    public void setCode(String code) throws AmetysRepositoryException
101    {
102        getMetadataHolder().setMetadata(METADATA_CODE, code);
103    }
104
105    /**
106     * Get the nature.
107     * @return the nature
108     */
109    public String getNature()
110    {
111        return getMetadataHolder().getString(METADATA_NATURE, StringUtils.EMPTY);
112    }
113
114    /**
115     * Get the number of hours
116     * @return the number of hours
117     */
118    public double getNumberOfHours()
119    {
120        return getMetadataHolder().getDouble(METADATA_NB_HOURS, 0);
121    }
122
123    /**
124     * Get the course holder
125     * @return the course holder
126     */
127    public Course getCourseHolder()
128    {
129        Course courseHolder = null;
130        try
131        {
132            String courseHolderId = getMetadataHolder().getString(METADATA_COURSE_HOLDER);
133            courseHolder = _getFactory()._getResolver().resolveById(courseHolderId);
134            
135        }
136        catch (AmetysRepositoryException e)
137        {
138            // Do nothing
139        }
140        return courseHolder;
141    }
142    
143    /**
144     * Get the parent courses.
145     * @return The {@link List} of parent {@link Course}s
146     */
147    public List<Course> getCourses()
148    {
149        List<Course> courses = new ArrayList<>();
150        
151        String[] coursePartIds = getMetadataHolder().getStringArray(METADATA_PARENT_COURSES, new String[0]);
152        for (String id : coursePartIds)
153        {
154            try
155            {
156                Course course = _getFactory()._getResolver().resolveById(id);
157                courses.add(course);
158            }
159            catch (UnknownAmetysObjectException e)
160            {
161                // Nothing
162            }
163        }
164        
165        return courses;
166    }
167    
168    /**
169     * Returns the programs holding this course part through the courses
170     * @return the root programs
171     */
172    public Set<Program> getRootPrograms()
173    {
174        Set<Program> programs = new HashSet<>();
175        for (Course course : getCourses())
176        {
177            programs.addAll(course.getRootPrograms());
178        }
179        return programs;
180    }
181}