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.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.ArrayUtils;
026
027import org.ametys.cms.data.ContentDataHelper;
028import org.ametys.cms.data.ContentValue;
029import org.ametys.cms.repository.Content;
030
031/**
032 * Common implementation of a {@link Content} which is part of a program, traversable
033 * @param <F> the actual type of factory.
034 */
035public abstract class AbstractTraversableProgramPart<F extends ProgramPartFactory> extends AbstractProgramPart<F> implements TraversableProgramPart
036{
037    /**
038     * Constructor
039     * @param node The JCR node
040     * @param parentPath The parent path
041     * @param factory The factory
042     */
043    public AbstractTraversableProgramPart(Node node, String parentPath, F factory)
044    {
045        super(node, parentPath, factory);
046    }
047    
048    @Override
049    public List<ProgramPart> getProgramPartChildren()
050    {
051        return Arrays.stream(getValue(CHILD_PROGRAM_PARTS, false, new ContentValue[0]))
052                .map(ContentValue::getContentIfExists)
053                .flatMap(Optional::stream)
054                .map(ProgramPart.class::cast)
055                .collect(Collectors.toList());
056    }
057    
058    @Override
059    public boolean containsProgramPart(String contentId)
060    {
061        return ArrayUtils.contains(ContentDataHelper.getContentIdsArrayFromMultipleContentData(this, CHILD_PROGRAM_PARTS), contentId);
062    }
063    
064    @Override
065    public boolean hasProgramPartChildren()
066    {
067        return !ContentDataHelper.isMultipleContentDataEmpty(this, CHILD_PROGRAM_PARTS);
068    }
069    
070}