001/*
002 *  Copyright 2016 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.plugins.contentio.synchronize.search.systemprop;
017
018import java.util.Arrays;
019import java.util.Map;
020
021import javax.jcr.RepositoryException;
022import javax.jcr.Value;
023
024import org.ametys.cms.contenttype.MetadataType;
025import org.ametys.cms.repository.Content;
026import org.ametys.cms.search.SearchField;
027import org.ametys.cms.search.model.SystemProperty;
028import org.ametys.cms.search.query.Query;
029import org.ametys.cms.search.query.Query.Operator;
030import org.ametys.cms.search.systemprop.AbstractSystemProperty;
031import org.ametys.core.util.LambdaUtils;
032import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollection;
033import org.ametys.plugins.contentio.synchronize.search.field.CollectionSearchField;
034import org.ametys.plugins.contentio.synchronize.search.query.CollectionsQuery;
035import org.ametys.plugins.repository.jcr.JCRAmetysObject;
036
037/**
038 * {@link SystemProperty} which represents the synchronizable contents collections of a content.
039 */
040public class CollectionsSystemProperty extends AbstractSystemProperty
041{
042    @Override
043    public MetadataType getType()
044    {
045        return MetadataType.STRING;
046    }
047
048    @Override
049    public boolean isMultiple()
050    {
051        return true;
052    }
053
054    @Override
055    public boolean isSortable()
056    {
057        return false;
058    }
059
060    @Override
061    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
062    {
063        String[] synchronizableContentsCollectionIds = parseStringArray(value);
064        return new CollectionsQuery(operator, synchronizableContentsCollectionIds);
065    }
066
067    @Override
068    public SearchField getSearchField()
069    {
070        return new CollectionSearchField();
071    }
072
073    @Override
074    public Object getValue(Content content)
075    {
076        if (content instanceof JCRAmetysObject)
077        {
078            try
079            {
080                Value[] values = ((JCRAmetysObject) content).getNode().getProperty(SynchronizableContentsCollection.COLLECTION_ID_PROPERTY).getValues();
081                return Arrays.stream(values).map(LambdaUtils.wrap(Value::getString)).toArray(String[]::new);
082            }
083            catch (RepositoryException e)
084            {
085                // The property does not exists
086                return new String[0];
087            }
088        }
089        return new String[0];
090    }
091}