001/*
002 *  Copyright 2019 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.cms.content.type;
017
018import java.util.Arrays;
019import java.util.Collections;
020import java.util.List;
021import java.util.stream.Collectors;
022
023import org.ametys.cms.content.references.OutgoingReferences;
024import org.ametys.cms.content.references.Referencer;
025import org.ametys.cms.data.ContentValue;
026import org.ametys.cms.data.type.impl.ContentRepositoryElementType;
027import org.ametys.cms.repository.Content;
028
029/**
030 * Class for content type of elements used by contents
031 */
032public class ContentContentElementType extends ContentRepositoryElementType implements Referencer
033{
034    /** Constant for the content type for outgoing references */
035    public static final String OUTGOING_REFERENCE_TYPE = "content";
036    
037    public OutgoingReferences getOutgoingReferences(Object value)
038    {
039        if (value != null)
040        {
041            if (value instanceof String)
042            {
043                OutgoingReferences references = new OutgoingReferences();
044                List<String> contentIds = Collections.singletonList((String) value);
045                references.put(OUTGOING_REFERENCE_TYPE, contentIds);
046                return references;
047            }
048            else if (value instanceof String[])
049            {
050                OutgoingReferences references = new OutgoingReferences();
051                List<String> contentIds = Arrays.asList((String[]) value);
052                references.put(OUTGOING_REFERENCE_TYPE, contentIds);
053                return references;
054            }
055            else if (value instanceof Content)
056            {
057                OutgoingReferences references = new OutgoingReferences();
058                List<String> contentIds = Collections.singletonList(((Content) value).getId());
059                references.put(OUTGOING_REFERENCE_TYPE, contentIds);
060                return references;
061            }
062            else if (value instanceof Content[])
063            {
064                OutgoingReferences references = new OutgoingReferences();
065                List<String> contentIds = Arrays.stream((Content[]) value)
066                        .map(Content::getId)
067                        .collect(Collectors.toList());
068                references.put(OUTGOING_REFERENCE_TYPE, contentIds);
069                return references;
070            }
071            else if (value instanceof ContentValue)
072            {
073                OutgoingReferences references = new OutgoingReferences();
074                List<String> contentIds = Collections.singletonList(((ContentValue) value).getContentId());
075                references.put(OUTGOING_REFERENCE_TYPE, contentIds);
076                return references;
077            }
078            else if (value instanceof ContentValue[])
079            {
080                OutgoingReferences references = new OutgoingReferences();
081                List<String> contentIds = Arrays.stream((ContentValue[]) value)
082                        .map(ContentValue::getContentId)
083                        .collect(Collectors.toList());
084                references.put(OUTGOING_REFERENCE_TYPE, contentIds);
085                return references;
086            }
087            else
088            {
089                throw new IllegalArgumentException("Try to get outgoing references of the non content value '" + value + "'");
090            }
091        }
092        else
093        {
094            return OutgoingReferences.EMPTY;
095        }
096    }
097}