001/*
002 *  Copyright 2015 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.cms.search.systemprop;
017
018import java.util.Collection;
019import java.util.LinkedHashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.configuration.Configuration;
024import org.apache.avalon.framework.configuration.ConfigurationException;
025import org.apache.solr.common.SolrInputDocument;
026
027import org.ametys.cms.content.indexing.solr.SolrFieldNames;
028import org.ametys.cms.data.type.indexing.IndexableDataContext;
029import org.ametys.cms.repository.Content;
030import org.ametys.cms.repository.comment.Comment;
031import org.ametys.cms.repository.comment.CommentableContent;
032import org.ametys.cms.search.SearchField;
033import org.ametys.cms.search.model.SystemProperty;
034import org.ametys.cms.search.query.CommentQuery;
035import org.ametys.cms.search.query.CommentQuery.CommentInclusion;
036import org.ametys.cms.search.query.Query;
037import org.ametys.cms.search.query.Query.Operator;
038import org.ametys.cms.search.solr.field.CommentsSearchField;
039import org.ametys.cms.search.solr.schema.FieldDefinition;
040import org.ametys.cms.search.solr.schema.SchemaDefinition;
041import org.ametys.runtime.i18n.I18nizableText;
042import org.ametys.runtime.model.Enumerator;
043import org.ametys.runtime.model.StaticEnumerator;
044import org.ametys.runtime.model.type.DataContext;
045import org.ametys.runtime.model.type.ModelItemTypeConstants;
046import org.ametys.runtime.plugin.component.ThreadSafeComponentManager;
047
048/**
049 * {@link SystemProperty} which represents the comments of a content.
050 */
051public class CommentsSystemProperty extends AbstractSystemProperty<Boolean, Content>
052{
053    @Override
054    public boolean isMultiple()
055    {
056        return false;
057    }
058    
059    @Override
060    public boolean isSortable()
061    {
062        return true;
063    }
064    
065    @Override
066    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
067    {
068        if ("with-comments".equals(value))
069        {
070            return new CommentQuery(CommentInclusion.VALIDATED_AND_NON_VALIDATED, operator);
071        }
072        else if ("with-validated-comments".equals(value))
073        {
074            return new CommentQuery(CommentInclusion.VALIDATED, operator);
075        }
076        else // if ("with-nonvalidated-comments".equals(value))
077        {
078            return new CommentQuery(CommentInclusion.NON_VALIDATED, operator);
079        }
080    }
081    
082    @Override
083    public String getCriterionWidget()
084    {
085        // Force standard combobox as the default widget, as the property type is boolean.
086        return "edition.combobox";
087    }
088    
089    @Override
090    public String getRenderer()
091    {
092        return "Ametys.plugins.cms.search.SearchGridHelper.renderBooleanIcon";
093    }
094    
095    @Override
096    public Integer getColumnWidth()
097    {
098        return 40;
099    }
100    
101    @Override
102    public SearchField getSearchField()
103    {
104        return new CommentsSearchField();
105    }
106    
107    @Override
108    public Collection<SchemaDefinition> getSchemaDefinitions()
109    {
110        Collection<SchemaDefinition> definitions = super.getSchemaDefinitions();
111        
112        definitions.add(new FieldDefinition(SolrFieldNames.CONTENT_COMMENTS_VALIDATED, "boolean", false, false));
113        definitions.add(new FieldDefinition(SolrFieldNames.CONTENT_COMMENTS_NONVALIDATED, "boolean", false, false));
114        
115        return definitions;
116    }
117    
118    @Override
119    public void indexValue(SolrInputDocument document, Content content, IndexableDataContext context)
120    {
121        super.indexValue(document, content, context);
122        
123        boolean hasComment = (boolean) getValue(content);
124        if (hasComment)
125        {
126            List<Comment> comments = ((CommentableContent) content).getComments(true, true);
127            document.addField(SolrFieldNames.CONTENT_COMMENTS_VALIDATED, comments.stream().anyMatch(c -> c.isValidated()));
128            document.addField(SolrFieldNames.CONTENT_COMMENTS_NONVALIDATED, comments.stream().anyMatch(c -> !c.isValidated()));
129        }
130    }
131    
132    @Override
133    public Object getValue(Content content)
134    {
135        if (content instanceof CommentableContent commentableContent)
136        {
137            List<Comment> comments = commentableContent.getComments(true, true);
138            return Boolean.valueOf(!comments.isEmpty());
139        }
140        
141        return Boolean.valueOf(false);
142    }
143    
144    @Override
145    public Object valueToJSON(Content content, DataContext context)
146    {
147        // FIXME pourquoi ne pas retourner getRawValue directement ?
148        Map<String, Object> infos = new LinkedHashMap<>();
149        infos.put("comments", getValue(content));
150        return infos;
151    }
152    
153    public Enumerator<String> getCriterionEnumerator(Configuration configuration, ThreadSafeComponentManager<Enumerator> enumeratorManager) throws ConfigurationException
154    {
155        StaticEnumerator<String> enumerator = new StaticEnumerator<>();
156        enumerator.add(new I18nizableText("plugin.cms", "PLUGINS_CMS_CONTENT_COMMENTS_SEARCH_ALL"), "with-comments");
157        enumerator.add(new I18nizableText("plugin.cms", "PLUGINS_CMS_CONTENT_COMMENTS_SEARCH_VALIDATED"), "with-validated-comments");
158        enumerator.add(new I18nizableText("plugin.cms", "PLUGINS_CMS_CONTENT_COMMENTS_SEARCH_NOTVALIDATED"), "with-nonvalidated-comments");
159        return enumerator;
160    }
161    
162    @Override
163    protected String _getTypeId()
164    {
165        return ModelItemTypeConstants.BOOLEAN_TYPE_ID;
166    }
167}