001/*
002 *  Copyright 2018 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.List;
020
021import javax.jcr.Node;
022
023import org.apache.commons.lang.ArrayUtils;
024
025import org.ametys.cms.repository.Content;
026import org.ametys.plugins.repository.UnknownAmetysObjectException;
027
028/**
029 * Common implementation of a {@link Content} which is part of a program, traversable
030 * @param <F> the actual type of factory.
031 */
032public abstract class AbstractTraversableProgramPart<F extends ProgramPartFactory> extends AbstractProgramPart<F> implements TraversableProgramPart
033{
034    /**
035     * Constructor
036     * @param node The JCR node
037     * @param parentPath The parent path
038     * @param factory The factory
039     */
040    public AbstractTraversableProgramPart(Node node, String parentPath, F factory)
041    {
042        super(node, parentPath, factory);
043    }
044    
045    @Override
046    public List<ProgramPart> getProgramPartChildren()
047    {
048        List<ProgramPart> children = new ArrayList<>();
049        
050        String[] childIds = getMetadataHolder().getStringArray(METADATA_CHILD_PROGRAM_PARTS, new String[0]);
051        for (String id : childIds)
052        {
053            try
054            {
055                ProgramPart child = _getFactory()._getResolver().resolveById(id);
056                children.add(child);
057            }
058            catch (UnknownAmetysObjectException e)
059            {
060                // Nothing
061            }
062        }
063        
064        return children;
065    }
066    
067    @Override
068    public boolean containsProgramPart(String contentId)
069    {
070        return ArrayUtils.contains(getMetadataHolder().getStringArray(METADATA_CHILD_PROGRAM_PARTS, new String[0]), contentId);
071    }
072    
073    @Override
074    public boolean hasProgramPartChildren()
075    {
076        return getMetadataHolder().getStringArray(METADATA_CHILD_PROGRAM_PARTS, new String[0]).length > 0;
077    }
078    
079}