001/*
002 *  Copyright 2015 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.web.search.model.impl;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.ametys.cms.search.model.SearchModelCriterionDefinition;
024import org.ametys.cms.search.model.impl.AbstractStaticSearchModelCriterionDefinition;
025import org.ametys.cms.search.model.impl.ReferencingSearchModelCriterionDefinition;
026import org.ametys.cms.search.query.OrQuery;
027import org.ametys.cms.search.query.Query;
028import org.ametys.cms.search.query.Query.Operator;
029import org.ametys.runtime.i18n.I18nizableText;
030import org.ametys.runtime.model.type.ModelItemTypeConstants;
031import org.ametys.web.search.query.PageQuery;
032
033/**
034 * Custom {@link SearchModelCriterionDefinition} searching a specific page and its sub-tree.
035 */
036public class PageCriterionDefinition extends AbstractStaticSearchModelCriterionDefinition<String>
037{
038    @Override
039    protected String getTypeId()
040    {
041        return ModelItemTypeConstants.STRING_TYPE_ID;
042    }
043    
044    @Override
045    public Query getQuery(Object value, Operator customOperator, Map<String, Object> allValues, String language, Map<String, Object> contextualParameters)
046    {
047        if (_getCriterionDefinitionHelper().isQueryValueEmpty(value))
048        {
049            return null;
050        }
051        
052        if (value instanceof List<?>)
053        {
054            List<Query> queries = new ArrayList<>();
055            
056            for (Object pageId : (List<?>) value)
057            {
058                queries.add(new PageQuery(pageId.toString(), true));
059            }
060            
061            return new OrQuery(queries);
062        }
063        else
064        {
065            String pageId = value.toString();
066            return new PageQuery(pageId, true);
067        }
068    }
069    
070    @Override
071    public String getWidget()
072    {
073        return "edition.select-page";
074    }
075    
076    @Override
077    public Map<String, I18nizableText> getWidgetParameters()
078    {
079        Map<String, I18nizableText> params = new HashMap<>();
080        
081        params.put("siteContext", new I18nizableText("current"));
082        params.put("sitemapContext", new I18nizableText("current"));
083        // TODO Find a system to provide a simple "site" property in the criterion definition.
084        // The criterion definition in the model file declares "site" and the search model provides the real ID?
085        params.put("sitesField", new I18nizableText(ReferencingSearchModelCriterionDefinition.CRITERION_DEFINITION_PREFIX + "site-eq"));
086        
087        return params;
088    }
089    
090    @Override
091    public Operator getOperator()
092    {
093        return Operator.EQ;
094    }
095}