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.content.code.DisplayCodeProperty; 031import org.ametys.odf.course.Course; 032import org.ametys.plugins.repository.AmetysRepositoryException; 033 034/** 035 * Class representing a {@link CoursePart} 036 */ 037public class CoursePart extends ModifiableDefaultContent<CoursePartFactory> 038{ 039 /** The attribute holding the catalog */ 040 public static final String CATALOG = "catalog"; 041 042 /** The attribute holding the code */ 043 public static final String CODE = "code"; 044 045 /** Constants for attribute 'nature' */ 046 public static final String NATURE = "nature"; 047 048 /** Constants for attribute 'nbHours' */ 049 public static final String NB_HOURS = "nbHours"; 050 051 /** Name of attribute for course holder */ 052 public static final String COURSE_HOLDER = "courseHolder"; 053 054 /** Name of attribute for parent courses */ 055 public static final String PARENT_COURSES = "courses"; 056 057 /** 058 * Constructor. 059 * @param node the JCR Node. 060 * @param parentPath the parent path 061 * @param factory the corresponding factory. 062 */ 063 public CoursePart(Node node, String parentPath, CoursePartFactory factory) 064 { 065 super(node, parentPath, factory); 066 } 067 068 /** 069 * Get the catalog. 070 * @return the catalog 071 */ 072 public String getCatalog() 073 { 074 return getValue(CATALOG); 075 } 076 077 /** 078 * Set the catalog. 079 * @param catalog The catalog 080 * @throws AmetysRepositoryException if an error occurs 081 */ 082 public void setCatalog(String catalog) throws AmetysRepositoryException 083 { 084 setValue(CATALOG, catalog); 085 } 086 087 /** 088 * Get the code. 089 * @return the code 090 */ 091 public String getCode() 092 { 093 return getValue(CODE, false, StringUtils.EMPTY); 094 } 095 096 /** 097 * Set the code. 098 * @param code The code 099 * @throws AmetysRepositoryException if an error occurs 100 */ 101 public void setCode(String code) throws AmetysRepositoryException 102 { 103 setValue(CODE, code); 104 } 105 106 /** 107 * Get the code to display 108 * @return the code to display 109 */ 110 public String getDisplayCode() 111 { 112 return getValue(DisplayCodeProperty.PROPERTY_NAME, false, StringUtils.EMPTY); 113 } 114 115 /** 116 * Get the nature. 117 * @return the nature 118 */ 119 public String getNature() 120 { 121 return ContentDataHelper.getContentIdFromContentData(this, NATURE); 122 } 123 124 /** 125 * Get the number of hours 126 * @return the number of hours 127 */ 128 public double getNumberOfHours() 129 { 130 return getValue(NB_HOURS, false, 0D); 131 } 132 133 /** 134 * Get the course holder 135 * @return the course holder 136 */ 137 public Course getCourseHolder() 138 { 139 return Optional.ofNullable((ContentValue) getValue(COURSE_HOLDER)) 140 .flatMap(ContentValue::getContentIfExists) 141 .map(Course.class::cast) 142 .orElse(null); 143 } 144 145 /** 146 * Get the parent courses. 147 * @return The {@link List} of parent {@link Course}s 148 */ 149 public List<Course> getCourses() 150 { 151 return Arrays.stream(getValue(PARENT_COURSES, false, new ContentValue[0])) 152 .map(ContentValue::getContentIfExists) 153 .flatMap(Optional::stream) 154 .map(Course.class::cast) 155 .collect(Collectors.toList()); 156 } 157}