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