001/* 002 * Copyright 2017 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.Collections; 020import java.util.List; 021import java.util.Optional; 022import java.util.stream.Collectors; 023 024import javax.jcr.Node; 025 026import org.apache.commons.lang.StringUtils; 027 028import org.ametys.cms.data.ContentDataHelper; 029import org.ametys.cms.data.ContentValue; 030import org.ametys.cms.repository.Content; 031import org.ametys.cms.repository.ModifiableDefaultContent; 032import org.ametys.plugins.repository.AmetysRepositoryException; 033import org.ametys.runtime.model.exception.UndefinedItemPathException; 034 035/** 036 * Common implementation of a {@link Content} which is part of a program 037 * @param <F> the actual type of factory. 038 */ 039public abstract class AbstractProgramPart<F extends ProgramPartFactory> extends ModifiableDefaultContent<F> implements ProgramPart 040{ 041 /** 042 * Constructor 043 * @param node The JCR node 044 * @param parentPath The parent path 045 * @param factory The factory 046 */ 047 public AbstractProgramPart(Node node, String parentPath, F factory) 048 { 049 super(node, parentPath, factory); 050 } 051 052 @Override 053 public List<ProgramPart> getProgramPartParents() 054 { 055 return Arrays.stream(getValue(PARENT_PROGRAM_PARTS, false, new ContentValue[0])) 056 .map(ContentValue::getContentIfExists) 057 .flatMap(Optional::stream) 058 .map(ProgramPart.class::cast) 059 .collect(Collectors.toList()); 060 } 061 062 @Override 063 public String getCatalog() 064 { 065 return getValue(CATALOG); 066 } 067 068 @Override 069 public void setCatalog(String catalog) throws AmetysRepositoryException 070 { 071 setValue(CATALOG, catalog); 072 } 073 074 @Override 075 public String getCode() 076 { 077 return getValue(CODE, false, StringUtils.EMPTY); 078 } 079 080 @Override 081 public void setCode(String code) throws AmetysRepositoryException 082 { 083 setValue(CODE, code); 084 } 085 086 public boolean isPublishable() 087 { 088 return getInternalDataHolder().getValue(PUBLISHABLE, true); 089 } 090 091 public void setPublishable(boolean isPublishable) 092 { 093 getInternalDataHolder().setValue(PUBLISHABLE, isPublishable); 094 } 095 096 public List<String> getOrgUnits() 097 { 098 try 099 { 100 return ContentDataHelper.getContentIdsListFromMultipleContentData(this, ORG_UNITS_REFERENCES); 101 } 102 catch (UndefinedItemPathException e) 103 { 104 return Collections.EMPTY_LIST; // this attribute is not part of model 105 } 106 } 107 108 // --------------------------------------------------------------------------------------// 109 // CDM-fr 110 // --------------------------------------------------------------------------------------// 111 @Override 112 public String getCDMId() 113 { 114 String cdmCode = getCdmCode(); 115 if (StringUtils.isEmpty(cdmCode)) 116 { 117 return "FRUAI" + _getFactory()._getRootOrgUnitRNE() + getCDMType() + getCode(); 118 } 119 return cdmCode; 120 } 121 122 /** 123 * Get the type of this {@link ProgramPart} in CDM. This type compose the CDM identifier 124 * @return the type 125 */ 126 protected abstract String getCDMType(); 127 128 @Override 129 public String getCdmCode() 130 { 131 return getValue(CDM_CODE, false, StringUtils.EMPTY); 132 } 133 134 @Override 135 public void setCdmCode(String cdmCode) 136 { 137 setValue(CDM_CODE, cdmCode); 138 } 139}