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.model.CMSDataContext;
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.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.schema.FieldDefinition;
038import org.ametys.cms.search.solr.schema.SchemaDefinition;
039import org.ametys.runtime.i18n.I18nizableText;
040import org.ametys.runtime.model.Enumerator;
041import org.ametys.runtime.model.StaticEnumerator;
042import org.ametys.runtime.model.type.DataContext;
043import org.ametys.runtime.model.type.ModelItemTypeConstants;
044import org.ametys.runtime.plugin.component.ThreadSafeComponentManager;
045
046/**
047 * {@link SystemProperty} which represents the comments of a content.
048 */
049public class CommentsSystemProperty extends AbstractIndexableSystemProperty<Boolean, String, Content>
050{
051    /** System property identifier */
052    public static final String SYSTEM_PROPERTY_ID = "comments";
053    
054    /** Solr field name. */
055    public static final String SOLR_FIELD_NAME = SYSTEM_PROPERTY_ID;
056    
057    @Override
058    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
059    {
060        if ("with-comments".equals(value))
061        {
062            return new CommentQuery(CommentInclusion.VALIDATED_AND_NON_VALIDATED, operator);
063        }
064        else if ("with-validated-comments".equals(value))
065        {
066            return new CommentQuery(CommentInclusion.VALIDATED, operator);
067        }
068        else // if ("with-nonvalidated-comments".equals(value))
069        {
070            return new CommentQuery(CommentInclusion.NON_VALIDATED, operator);
071        }
072    }
073    
074    @Override
075    public String getRenderer()
076    {
077        return "Ametys.grid.GridColumnHelper.renderBooleanIcon";
078    }
079    
080    @Override
081    public Integer getColumnWidth()
082    {
083        return 40;
084    }
085    
086    @Override
087    public String getSolrFieldName()
088    {
089        return SOLR_FIELD_NAME;
090    }
091    
092    @Override
093    public String getSolrSortFieldName()
094    {
095        return SOLR_FIELD_NAME;
096    }
097    
098    @Override
099    public String getSolrFacetFieldName()
100    {
101        // FIXME use the default implementation;
102        // This leads to Solr error during indexation : is field 'comments_dv' is already declared by someone else ?
103        return null;
104    }
105    
106    @Override
107    public Collection<SchemaDefinition> getSchemaDefinitions()
108    {
109        Collection<SchemaDefinition> definitions = super.getSchemaDefinitions();
110        
111        definitions.add(new FieldDefinition(SolrFieldNames.CONTENT_COMMENTS_VALIDATED, "boolean", false, false));
112        definitions.add(new FieldDefinition(SolrFieldNames.CONTENT_COMMENTS_NONVALIDATED, "boolean", false, false));
113        
114        return definitions;
115    }
116    
117    @Override
118    public void indexValue(SolrInputDocument document, Content content, CMSDataContext context)
119    {
120        super.indexValue(document, content, context);
121        
122        boolean hasComment = (boolean) getValue(content);
123        if (hasComment)
124        {
125            List<Comment> comments = ((CommentableContent) content).getComments(true, true);
126            document.addField(SolrFieldNames.CONTENT_COMMENTS_VALIDATED, comments.stream().anyMatch(c -> c.isValidated()));
127            document.addField(SolrFieldNames.CONTENT_COMMENTS_NONVALIDATED, comments.stream().anyMatch(c -> !c.isValidated()));
128        }
129        else
130        {
131            document.addField(SolrFieldNames.CONTENT_COMMENTS_VALIDATED, null);
132            document.addField(SolrFieldNames.CONTENT_COMMENTS_NONVALIDATED, null);
133        }
134    }
135    
136    @Override
137    public Object getValue(Content content)
138    {
139        if (content instanceof CommentableContent commentableContent)
140        {
141            List<Comment> comments = commentableContent.getComments(true, true);
142            return Boolean.valueOf(!comments.isEmpty());
143        }
144        
145        return Boolean.valueOf(false);
146    }
147    
148    @Override
149    public Object valueToJSON(Content content, DataContext context)
150    {
151        // FIXME pourquoi ne pas retourner getRawValue directement ?
152        Map<String, Object> infos = new LinkedHashMap<>();
153        infos.put("comments", getValue(content));
154        return infos;
155    }
156    
157    @Override
158    public Enumerator<String> getDefaultCriterionEnumerator(Configuration configuration, ThreadSafeComponentManager<Enumerator> enumeratorManager) throws ConfigurationException
159    {
160        StaticEnumerator<String> enumerator = new StaticEnumerator<>();
161        enumerator.add(new I18nizableText("plugin.cms", "PLUGINS_CMS_CONTENT_COMMENTS_SEARCH_ALL"), "with-comments");
162        enumerator.add(new I18nizableText("plugin.cms", "PLUGINS_CMS_CONTENT_COMMENTS_SEARCH_VALIDATED"), "with-validated-comments");
163        enumerator.add(new I18nizableText("plugin.cms", "PLUGINS_CMS_CONTENT_COMMENTS_SEARCH_NOTVALIDATED"), "with-nonvalidated-comments");
164        return enumerator;
165    }
166    
167    @Override
168    protected String getTypeId()
169    {
170        return ModelItemTypeConstants.BOOLEAN_TYPE_ID;
171    }
172    
173    @Override
174    protected String getCriterionTypeId()
175    {
176        return ModelItemTypeConstants.STRING_TYPE_ID;
177    }
178}