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