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