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.SearchField;
028import org.ametys.cms.search.model.SystemProperty;
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.AbstractSystemProperty;
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;
040import org.ametys.web.search.solr.field.PageIdsSearchField;
041
042/**
043 * {@link SystemProperty} which represents the pages of a Content.
044 */
045public class PagesSystemProperty extends AbstractSystemProperty<String, Content>
046{
047    @Override
048    public boolean isMultiple()
049    {
050        return true;
051    }
052    
053    @Override
054    public boolean isSortable()
055    {
056        return false;
057    }
058    
059    @Override
060    public boolean isDisplayable()
061    {
062        return false;
063    }
064    
065    @Override
066    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
067    {
068        List<Query> queries = new ArrayList<>();
069        
070        if (operator == Operator.EXISTS)
071        {
072            return new ContentPageQuery(new PageQuery(true));
073        }
074        else
075        {
076            String[] pageIds = parseStringArray(value);
077            for (String pageId : pageIds)
078            {
079                queries.add(new ContentPageQuery(new PageQuery(pageId, true)));
080            }
081        }
082        
083        Query query = new OrQuery(queries);
084        
085        if (operator == Operator.NE)
086        {
087            query = new NotQuery(query);
088        }
089        return query;
090    }
091    
092    @Override
093    public String getCriterionWidget()
094    {
095        return "edition.select-page";
096    }
097    
098    public Map<String, I18nizableText> getCriterionWidgetParameters(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 search criterion definition.
105        // The search criterion definition in the model file declares "site" and the search model provides the real ID?
106        params.put("sitesField", new I18nizableText("property-site-eq"));
107        
108        return params;
109    }
110    
111    @Override
112    public SearchField getSearchField()
113    {
114        return new PageIdsSearchField();
115    }
116    
117    @Override
118    public Object getValue(Content content)
119    {
120        List<String> ids = new ArrayList<>();
121        
122        if (content instanceof WebContent webContent)
123        {
124            Collection<Page> pages = webContent.getReferencingPages();
125            for (Page page : pages)
126            {
127                ids.add(page.getId());
128            }
129        }
130        
131        return ids.toArray(new String[ids.size()]);
132    }
133
134    @Override
135    protected String _getTypeId()
136    {
137        return ModelItemTypeConstants.STRING_TYPE_ID;
138    }
139}