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.NotQuery;
031import org.ametys.cms.search.query.OrQuery;
032import org.ametys.cms.search.query.Query;
033import org.ametys.cms.search.query.Query.Operator;
034import org.ametys.cms.search.systemprop.AbstractSystemProperty;
035import org.ametys.runtime.i18n.I18nizableText;
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
046{
047    @Override
048    public MetadataType getType()
049    {
050        return MetadataType.STRING;
051    }
052    
053    @Override
054    public boolean isMultiple()
055    {
056        return true;
057    }
058    
059    @Override
060    public boolean isSortable()
061    {
062        return false;
063    }
064    
065    @Override
066    public boolean isDisplayable()
067    {
068        return false;
069    }
070    
071    @Override
072    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
073    {
074        List<Query> queries = new ArrayList<>();
075        
076        if (operator == Operator.EXISTS)
077        {
078            return new ContentPageQuery(new PageQuery(true));
079        }
080        else
081        {
082            String[] pageIds = parseStringArray(value);
083            for (String pageId : pageIds)
084            {
085                queries.add(new ContentPageQuery(new PageQuery(pageId, true)));
086            }
087        }
088        
089        Query query = new OrQuery(queries);
090        
091        if (operator == Operator.NE)
092        {
093            query = new NotQuery(query);
094        }
095        return query;
096    }
097    
098    @Override
099    public String getWidget()
100    {
101        return "edition.select-page";
102    }
103    
104    @Override
105    public Map<String, I18nizableText> getWidgetParameters(Configuration configuration)
106    {
107        Map<String, I18nizableText> params = new HashMap<>();
108        
109        params.put("siteContext", new I18nizableText("current"));
110        params.put("sitemapContext", new I18nizableText("current"));
111        // TODO Find a system to provide a simple "site" property in the search criterion definition.
112        // The search criterion definition in the model file declares "site" and the search model provides the real ID?
113        params.put("sitesField", new I18nizableText("property-site-eq"));
114        
115        return params;
116    }
117    
118    @Override
119    public SearchField getSearchField()
120    {
121        return new PageIdsSearchField();
122    }
123    
124    @Override
125    public Object getValue(Content content)
126    {
127        List<String> ids = new ArrayList<>();
128        
129        if (content instanceof WebContent)
130        {
131            Collection<Page> pages = ((WebContent) content).getReferencingPages();
132            for (Page page : pages)
133            {
134                ids.add(page.getId());
135            }
136        }
137        
138        return ids.toArray(new String[ids.size()]);
139    }
140
141}