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        Operator allowedOperator = _allowedOperator(operator);
065        return new CollectionsQuery(allowedOperator, synchronizableContentsCollectionIds);
066    }
067    
068    // Just to avoid an exception if a not-allowed operator is passed (for instance, SEARCH, etc.)
069    // The problem is that currently, we cannot restrain the proposed operator to the UI,
070    // and the operator for STRING type are plentiful, whereas CollectionsQuery only supports EQ and NE
071    private Operator _allowedOperator(Operator requestedOperator)
072    {
073        return requestedOperator == Operator.NE
074                ? requestedOperator
075                : Operator.EQ;
076    }
077
078    @Override
079    public SearchField getSearchField()
080    {
081        return new CollectionSearchField();
082    }
083
084    @Override
085    public Object getValue(Content content)
086    {
087        if (content instanceof JCRAmetysObject)
088        {
089            try
090            {
091                Value[] values = ((JCRAmetysObject) content).getNode().getProperty(SynchronizableContentsCollection.COLLECTION_ID_PROPERTY).getValues();
092                return Arrays.stream(values).map(LambdaUtils.wrap(Value::getString)).toArray(String[]::new);
093            }
094            catch (RepositoryException e)
095            {
096                // The property does not exists
097                return new String[0];
098            }
099        }
100        return new String[0];
101    }
102}