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.apache.avalon.framework.configuration.Configuration;
024import org.apache.avalon.framework.configuration.ConfigurationException;
025
026import org.ametys.cms.contenttype.MetadataType;
027import org.ametys.cms.search.query.OrQuery;
028import org.ametys.cms.search.query.Query;
029import org.ametys.cms.search.query.Query.Operator;
030import org.ametys.cms.search.ui.model.SearchUICriterion;
031import org.ametys.cms.search.ui.model.impl.AbstractCustomSearchUICriterion;
032import org.ametys.runtime.i18n.I18nizableText;
033import org.ametys.web.search.query.PageQuery;
034
035/**
036 * Custom {@link SearchUICriterion} searching a specific page and its sub-tree.
037 */
038public class PageSearchUICriterion extends AbstractCustomSearchUICriterion
039{
040    
041    @Override
042    public void configure(Configuration configuration) throws ConfigurationException
043    {
044        super.configure(configuration);
045        
046        setMultiple(configuration.getAttributeAsBoolean("multiple", false));
047    }
048    
049    @Override
050    public MetadataType getType()
051    {
052        return MetadataType.STRING;
053    }
054    
055    @Override
056    public Query getQuery(Object value, Operator customOperator, Map<String, Object> allValues, String language, Map<String, Object> contextualParameters)
057    {
058        if (value instanceof List<?>)
059        {
060            List<Query> queries = new ArrayList<>();
061            
062            for (Object pageId : (List<?>) value)
063            {
064                queries.add(new PageQuery(pageId.toString(), true));
065            }
066            
067            return new OrQuery(queries);
068        }
069        else
070        {
071            String pageId = value.toString();
072            return new PageQuery(pageId, true);
073        }
074    }
075    
076    @Override
077    public String getWidget()
078    {
079        return "edition.select-page";
080    }
081    
082    @Override
083    public Map<String, I18nizableText> getWidgetParameters()
084    {
085        Map<String, I18nizableText> params = new HashMap<>();
086        
087        params.put("siteContext", new I18nizableText("current"));
088        params.put("sitemapContext", new I18nizableText("current"));
089        // TODO Find a system to provide a simple "site" property in the search criterion definition.
090        // The search criterion definition in the model file declares "site" and the search model provides the real ID?
091        params.put("sitesField", new I18nizableText("property-site-eq"));
092        
093        return params;
094    }
095    
096    @Override
097    public String getFieldId()
098    {
099        return getId();
100    }
101    
102    @Override
103    public boolean isSortable()
104    {
105        // Not destined to be displayed.
106        return false;
107    }
108    
109    @Override
110    public Operator getOperator()
111    {
112        return Operator.EQ;
113    }
114
115}