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