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.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.StringUtils;
026
027import org.ametys.cms.data.ContentDataHelper;
028import org.ametys.cms.data.ContentValue;
029import org.ametys.cms.repository.ModifiableDefaultContent;
030import org.ametys.odf.course.Course;
031import org.ametys.plugins.repository.AmetysRepositoryException;
032
033/**
034 * Class representing a {@link CoursePart}
035 */
036public class CoursePart extends ModifiableDefaultContent<CoursePartFactory>
037{
038    /** The attribute holding the catalog */
039    public static final String CATALOG = "catalog";
040    
041    /** The attribute holding the code */
042    public static final String CODE = "code";
043    
044    /** Constants for attribute 'nature' */
045    public static final String NATURE = "nature";
046
047    /** Constants for attribute 'nbHours' */
048    public static final String NB_HOURS = "nbHours";
049
050    /** Name of attribute for course holder */
051    public static final String COURSE_HOLDER = "courseHolder";
052
053    /** Name of attribute for parent courses */
054    public static final String 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 getValue(CATALOG);
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        setValue(CATALOG, catalog);
084    }
085
086    /**
087     * Get the code.
088     * @return the code
089     */
090    public String getCode()
091    {
092        return getValue(CODE, false, 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        setValue(CODE, code);
103    }
104
105    /**
106     * Get the nature.
107     * @return the nature
108     */
109    public String getNature()
110    {
111        return ContentDataHelper.getContentIdFromContentData(this,  NATURE);
112    }
113
114    /**
115     * Get the number of hours
116     * @return the number of hours
117     */
118    public double getNumberOfHours()
119    {
120        return getValue(NB_HOURS, false, 0D);
121    }
122
123    /**
124     * Get the course holder
125     * @return the course holder
126     */
127    public Course getCourseHolder()
128    {
129        return Optional.ofNullable((ContentValue) getValue(COURSE_HOLDER))
130                                .flatMap(ContentValue::getContentIfExists)
131                                .map(Course.class::cast)
132                                .orElse(null);
133    }
134    
135    /**
136     * Get the parent courses.
137     * @return The {@link List} of parent {@link Course}s
138     */
139    public List<Course> getCourses()
140    {
141        return Arrays.stream(getValue(PARENT_COURSES, false, new ContentValue[0]))
142                .map(ContentValue::getContentIfExists)
143                .filter(Optional::isPresent)
144                .map(Optional::get)
145                .map(Course.class::cast)
146                .collect(Collectors.toList());
147    }
148}