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.web.search.systemprop;
017
018import java.util.ArrayList;
019import java.util.Collection;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023
024import org.apache.avalon.framework.configuration.Configuration;
025
026import org.ametys.cms.contenttype.MetadataType;
027import org.ametys.cms.repository.Content;
028import org.ametys.cms.search.SearchField;
029import org.ametys.cms.search.model.SystemProperty;
030import org.ametys.cms.search.query.OrQuery;
031import org.ametys.cms.search.query.Query;
032import org.ametys.cms.search.query.Query.Operator;
033import org.ametys.cms.search.systemprop.AbstractSystemProperty;
034import org.ametys.runtime.i18n.I18nizableText;
035import org.ametys.web.repository.content.WebContent;
036import org.ametys.web.repository.page.Page;
037import org.ametys.web.search.query.ContentPageQuery;
038import org.ametys.web.search.query.PageQuery;
039import org.ametys.web.search.solr.field.PageIdsSearchField;
040
041/**
042 * {@link SystemProperty} which represents the pages of a Content.
043 */
044public class PagesSystemProperty extends AbstractSystemProperty
045{
046    @Override
047    public MetadataType getType()
048    {
049        return MetadataType.STRING;
050    }
051    
052    @Override
053    public boolean isMultiple()
054    {
055        return true;
056    }
057    
058    @Override
059    public boolean isSortable()
060    {
061        return false;
062    }
063    
064    @Override
065    public boolean isDisplayable()
066    {
067        return false;
068    }
069    
070    @Override
071    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
072    {
073        List<Query> queries = new ArrayList<>();
074        
075        String[] pageIds = parseStringArray(value);
076        for (String pageId : pageIds)
077        {
078            queries.add(new ContentPageQuery(new PageQuery(pageId, true)));
079        }
080        
081        return new OrQuery(queries);
082    }
083    
084    @Override
085    public String getWidget()
086    {
087        return "edition.select-page";
088    }
089    
090    @Override
091    public Map<String, I18nizableText> getWidgetParameters(Configuration configuration)
092    {
093        Map<String, I18nizableText> params = new HashMap<>();
094        
095        params.put("siteContext", new I18nizableText("current"));
096        params.put("sitemapContext", new I18nizableText("current"));
097        // TODO Find a system to provide a simple "site" property in the search criterion definition.
098        // The search criterion definition in the model file declares "site" and the search model provides the real ID?
099        params.put("sitesField", new I18nizableText("property-site-eq"));
100        
101        return params;
102    }
103    
104    @Override
105    public SearchField getSearchField()
106    {
107        return new PageIdsSearchField();
108    }
109    
110    @Override
111    public Object getValue(Content content)
112    {
113        List<String> ids = new ArrayList<>();
114        
115        if (content instanceof WebContent)
116        {
117            Collection<Page> pages = ((WebContent) content).getReferencingPages();
118            for (Page page : pages)
119            {
120                ids.add(page.getId());
121            }
122        }
123        
124        return ids.toArray(new String[ids.size()]);
125    }
126
127}