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