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.solr.common.SolrInputDocument;
025
026import org.ametys.cms.content.indexing.solr.SolrFieldNames;
027import org.ametys.cms.contenttype.MetadataType;
028import org.ametys.cms.repository.Content;
029import org.ametys.cms.repository.comment.Comment;
030import org.ametys.cms.repository.comment.CommentableContent;
031import org.ametys.cms.search.SearchField;
032import org.ametys.cms.search.model.SystemProperty;
033import org.ametys.cms.search.query.CommentQuery;
034import org.ametys.cms.search.query.CommentQuery.CommentInclusion;
035import org.ametys.cms.search.query.Query;
036import org.ametys.cms.search.query.Query.Operator;
037import org.ametys.cms.search.solr.field.CommentsSearchField;
038import org.ametys.cms.search.solr.schema.FieldDefinition;
039import org.ametys.cms.search.solr.schema.SchemaDefinition;
040import org.ametys.runtime.i18n.I18nizableText;
041
042/**
043 * {@link SystemProperty} which represents the comments of a content.
044 */
045public class CommentsSystemProperty extends AbstractSystemProperty
046{
047
048    @Override
049    public MetadataType getType()
050    {
051        return MetadataType.BOOLEAN;
052    }
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 getWidget()
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 String getField()
104//    {
105//        return SolrFieldNames.CONTENT_COMMENTS;
106//    }
107    
108    @Override
109    public SearchField getSearchField()
110    {
111        return new CommentsSearchField();
112    }
113    
114    @Override
115    public Collection<SchemaDefinition> getSchemaDefinitions()
116    {
117        Collection<SchemaDefinition> definitions = super.getSchemaDefinitions();
118        
119        definitions.add(new FieldDefinition(SolrFieldNames.CONTENT_COMMENTS_VALIDATED, "boolean", false, false));
120        definitions.add(new FieldDefinition(SolrFieldNames.CONTENT_COMMENTS_NONVALIDATED, "boolean", false, false));
121        
122        return definitions;
123    }
124    
125    @Override
126    public void index(Content content, SolrInputDocument document)
127    {
128        super.index(content, document);
129        
130        boolean hasComment = (boolean) getValue(content);
131        if (hasComment)
132        {
133            List<Comment> comments = ((CommentableContent) content).getComments(true, true);
134            document.addField(SolrFieldNames.CONTENT_COMMENTS_VALIDATED, comments.stream().anyMatch(c -> c.isValidated()));
135            document.addField(SolrFieldNames.CONTENT_COMMENTS_NONVALIDATED, comments.stream().anyMatch(c -> !c.isValidated()));
136        }
137    }
138    
139    @Override
140    public Object getValue(Content content)
141    {
142        if (content instanceof CommentableContent)
143        {
144            List<Comment> comments = ((CommentableContent) content).getComments(true, true);
145            return Boolean.valueOf(!comments.isEmpty());
146        }
147        
148        return Boolean.valueOf(false);
149    }
150    
151    public Object getJsonValue(Content content, boolean full)
152    {
153        // FIXME pourquoi ne pas retourner getRawValue directement ?
154        Map<String, Object> infos = new LinkedHashMap<>();
155        infos.put("comments", getValue(content));
156        return infos;
157    }   
158    
159    @Override
160    public EnumeratorDefinition getEnumeratorDefinition(Configuration configuration)
161    {
162        Map<String, I18nizableText> entries = new LinkedHashMap<>();
163        entries.put("with-comments", new I18nizableText("plugin.cms", "PLUGINS_CMS_CONTENT_COMMENTS_SEARCH_ALL"));
164        entries.put("with-validated-comments", new I18nizableText("plugin.cms", "PLUGINS_CMS_CONTENT_COMMENTS_SEARCH_VALIDATED"));
165        entries.put("with-nonvalidated-comments", new I18nizableText("plugin.cms", "PLUGINS_CMS_CONTENT_COMMENTS_SEARCH_NOTVALIDATED"));
166        
167        return new EnumeratorDefinition(entries);
168    }
169
170}