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