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.repository.Content;
027import org.ametys.cms.search.model.SystemProperty;
028import org.ametys.cms.search.model.impl.ReferencingSearchModelCriterionDefinition;
029import org.ametys.cms.search.query.NotQuery;
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.AbstractIndexableSystemProperty;
034import org.ametys.runtime.i18n.I18nizableText;
035import org.ametys.runtime.model.type.ModelItemTypeConstants;
036import org.ametys.web.repository.content.WebContent;
037import org.ametys.web.repository.page.Page;
038import org.ametys.web.search.query.ContentPageQuery;
039import org.ametys.web.search.query.PageQuery;
040
041/**
042 * {@link SystemProperty} which represents the pages of a Content.
043 */
044public class PagesSystemProperty extends AbstractIndexableSystemProperty<String, String, Content>
045{
046    /** System property identifier */
047    public static final String SYSTEM_PROPERTY_ID = "pages";
048
049    /** Solr field name. */
050    public static final String SOLR_FIELD_NAME = "pageIds";
051    
052    @Override
053    public boolean isMultiple()
054    {
055        return true;
056    }
057    
058    @Override
059    public boolean isDisplayable()
060    {
061        return false;
062    }
063    
064    @Override
065    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
066    {
067        List<Query> queries = new ArrayList<>();
068        
069        if (operator == Operator.EXISTS)
070        {
071            return new ContentPageQuery(new PageQuery(true));
072        }
073        else
074        {
075            String[] pageIds = parseStringArray(value);
076            for (String pageId : pageIds)
077            {
078                queries.add(new ContentPageQuery(new PageQuery(pageId, true)));
079            }
080        }
081        
082        Query query = new OrQuery(queries);
083        
084        if (operator == Operator.NE)
085        {
086            query = new NotQuery(query);
087        }
088        return query;
089    }
090    
091    @Override
092    public String getDefaultCriterionWidget()
093    {
094        return "edition.select-page";
095    }
096    
097    @Override
098    public Map<String, I18nizableText> getDefaultCriterionWidgetParameters(Configuration configuration)
099    {
100        Map<String, I18nizableText> params = new HashMap<>();
101        
102        params.put("siteContext", new I18nizableText("current"));
103        params.put("sitemapContext", new I18nizableText("current"));
104        // TODO Find a system to provide a simple "site" property in the criterion definition.
105        // The criterion definition in the model file declares "site" and the search model provides the real ID?
106        params.put("sitesField", new I18nizableText(ReferencingSearchModelCriterionDefinition.CRITERION_DEFINITION_PREFIX + "site-eq"));
107        
108        return params;
109    }
110    
111    @Override
112    public String getSolrFieldName()
113    {
114        return SOLR_FIELD_NAME;
115    }
116    
117    @Override
118    public String getSolrSortFieldName()
119    {
120        return null;
121    }
122    
123    @Override
124    public String getSolrFacetFieldName()
125    {
126        return null;
127    }
128    
129    @Override
130    public Object getValue(Content content)
131    {
132        List<String> ids = new ArrayList<>();
133        
134        if (content instanceof WebContent webContent)
135        {
136            Collection<Page> pages = webContent.getReferencingPages();
137            for (Page page : pages)
138            {
139                ids.add(page.getId());
140            }
141        }
142        
143        return ids.toArray(new String[ids.size()]);
144    }
145
146    @Override
147    protected String getTypeId()
148    {
149        return ModelItemTypeConstants.STRING_TYPE_ID;
150    }
151}