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.ArrayList;
019import java.util.HashSet;
020import java.util.List;
021import java.util.Set;
022
023import javax.jcr.Node;
024
025import org.apache.commons.lang.StringUtils;
026
027import org.ametys.cms.repository.Content;
028import org.ametys.cms.repository.ModifiableDefaultContent;
029import org.ametys.plugins.repository.AmetysRepositoryException;
030import org.ametys.plugins.repository.UnknownAmetysObjectException;
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        List<ProgramPart> children = new ArrayList<>();
053        
054        String[] childIds = getMetadataHolder().getStringArray(METADATA_PARENT_PROGRAM_PARTS, new String[0]);
055        for (String id : childIds)
056        {
057            try
058            {
059                ProgramPart child = _getFactory()._getResolver().resolveById(id);
060                children.add(child);
061            }
062            catch (UnknownAmetysObjectException e)
063            {
064                // Nothing
065            }
066        }
067        
068        return children;
069    }
070    
071    @Override
072    public Set<Program> getRootPrograms()
073    {
074        Set<Program> programs = new HashSet<>();
075        
076        List<ProgramPart> parents = getProgramPartParents();
077        for (ProgramPart parent : parents)
078        {
079            if (parent instanceof Program)
080            {
081                programs.add((Program) parent);
082            }
083            else
084            {
085                programs.addAll(parent.getRootPrograms());
086            }
087        }
088        
089        return programs;
090    }
091    
092    @Override
093    public String getCatalog()
094    {
095        return getMetadataHolder().getString(METADATA_CATALOG, null);
096    }
097
098    @Override
099    public void setCatalog(String catalog) throws AmetysRepositoryException
100    {
101        getMetadataHolder().setMetadata(METADATA_CATALOG, catalog);
102    }
103    
104    @Override
105    public String getCode()
106    {
107        return getMetadataHolder().getString(METADATA_CODE, "");
108    }
109    
110    @Override
111    public void setCode(String code) throws AmetysRepositoryException
112    {
113        getMetadataHolder().setMetadata(METADATA_CODE, code);
114    }
115    
116    // --------------------------------------------------------------------------------------//
117    // CDM-fr
118    // --------------------------------------------------------------------------------------//
119    @Override
120    public String getCDMId() 
121    {
122        String cdmCode = getCdmCode();
123        if (StringUtils.isEmpty(cdmCode))
124        {
125            return "FRUAI" + _getFactory()._getRootOrgUnitRNE() + getCDMType() + getCode();
126        }
127        return cdmCode;
128    }
129    
130    /**
131     * Get the type of this {@link ProgramPart} in CDM. This type compose the CDM identifier
132     * @return the type
133     */
134    protected abstract String getCDMType();
135    
136    @Override
137    public String getCdmCode()
138    {
139        return getMetadataHolder().getString(CDM_CODE, "");
140    }
141    
142    @Override
143    public void setCdmCode(String cdmCode) 
144    {
145        getMetadataHolder().setMetadata(CDM_CODE, cdmCode);
146    }
147}