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.Map;
019
020import org.ametys.cms.contenttype.MetadataType;
021import org.ametys.cms.repository.Content;
022import org.ametys.cms.search.SearchField;
023import org.ametys.cms.search.model.SystemProperty;
024import org.ametys.cms.search.query.Query;
025import org.ametys.cms.search.query.Query.Operator;
026import org.ametys.cms.search.systemprop.AbstractSystemProperty;
027import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollection;
028import org.ametys.plugins.contentio.synchronize.search.field.CollectionSearchField;
029import org.ametys.plugins.contentio.synchronize.search.query.CollectionsQuery;
030
031/**
032 * {@link SystemProperty} which represents the synchronizable contents collections of a content.
033 */
034public class CollectionsSystemProperty extends AbstractSystemProperty
035{
036    @Override
037    public MetadataType getType()
038    {
039        return MetadataType.STRING;
040    }
041
042    @Override
043    public boolean isMultiple()
044    {
045        return true;
046    }
047
048    @Override
049    public boolean isSortable()
050    {
051        return false;
052    }
053
054    @Override
055    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
056    {
057        String[] synchronizableContentsCollectionIds = parseStringArray(value);
058        Operator allowedOperator = _allowedOperator(operator);
059        return new CollectionsQuery(allowedOperator, synchronizableContentsCollectionIds);
060    }
061    
062    // Just to avoid an exception if a not-allowed operator is passed (for instance, SEARCH, etc.)
063    // The problem is that currently, we cannot restrain the proposed operator to the UI,
064    // and the operator for STRING type are plentiful, whereas CollectionsQuery only supports EQ and NE
065    private Operator _allowedOperator(Operator requestedOperator)
066    {
067        return requestedOperator == Operator.NE
068                ? requestedOperator
069                : Operator.EQ;
070    }
071
072    @Override
073    public SearchField getSearchField()
074    {
075        return new CollectionSearchField();
076    }
077
078    @Override
079    public Object getValue(Content content)
080    {
081        return content.getInternalDataHolder()
082                      .getValue(SynchronizableContentsCollection.COLLECTION_ID_DATA_NAME, new String[0]);
083    }
084}