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.ArrayList; 019import java.util.Collection; 020import java.util.LinkedHashMap; 021import java.util.List; 022import java.util.Map; 023 024import org.apache.avalon.framework.configuration.Configuration; 025import org.apache.solr.common.SolrInputDocument; 026 027import org.ametys.cms.content.indexing.solr.SolrFieldNames; 028import org.ametys.cms.contenttype.MetadataType; 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.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(true, true); 072 } 073 else if ("with-validated-comments".equals(value)) 074 { 075 return new CommentQuery(true, false); 076 } 077 else // if ("with-nonvalidated-comments".equals(value)) 078 { 079 return new CommentQuery(false, true); 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.cms.content.EditContentsGrid.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 List<SchemaDefinition> definitions = new ArrayList<>(); 118 119 definitions.add(new FieldDefinition(SolrFieldNames.CONTENT_COMMENTS, "boolean", false, false)); 120 definitions.add(new FieldDefinition(SolrFieldNames.CONTENT_COMMENTS_VALIDATED, "boolean", false, false)); 121 definitions.add(new FieldDefinition(SolrFieldNames.CONTENT_COMMENTS_NONVALIDATED, "boolean", false, false)); 122 123 return definitions; 124 } 125 126 @Override 127 public void index(Content content, SolrInputDocument document) 128 { 129 if (content instanceof CommentableContent) 130 { 131 List<Comment> comments = ((CommentableContent) content).getComments(true, true); 132 133 document.addField(SolrFieldNames.CONTENT_COMMENTS, !comments.isEmpty()); 134 135 document.addField(SolrFieldNames.CONTENT_COMMENTS_VALIDATED, comments.stream().anyMatch(c -> c.isValidated())); 136 document.addField(SolrFieldNames.CONTENT_COMMENTS_NONVALIDATED, comments.stream().anyMatch(c -> !c.isValidated())); 137 } 138 else 139 { 140 document.addField(SolrFieldNames.CONTENT_COMMENTS, false); 141 } 142 } 143 144 @Override 145 public Object getValue(Content content) 146 { 147 boolean hasComments = false; 148 149 if (content instanceof CommentableContent) 150 { 151 List<Comment> comments = ((CommentableContent) content).getComments(true, true); 152 153 hasComments = !comments.isEmpty(); 154 } 155 156 return hasComments; 157 } 158 159 @Override 160 public Object getFullValue(Content content) 161 { 162 Map<String, Object> infos = new LinkedHashMap<>(); 163 164 if (content instanceof CommentableContent) 165 { 166 boolean hasComment = !((CommentableContent) content).getComments(true, true).isEmpty(); 167 infos.put("comments", hasComment); 168 } 169 170 return infos; 171 } 172 173 @Override 174 public EnumeratorDefinition getEnumeratorDefinition(Collection<String> contentTypes, Configuration configuration) 175 { 176 Map<String, I18nizableText> entries = new LinkedHashMap<>(); 177 entries.put("with-comments", new I18nizableText("plugin.cms", "PLUGINS_CMS_CONTENT_COMMENTS_SEARCH_ALL")); 178 entries.put("with-validated-comments", new I18nizableText("plugin.cms", "PLUGINS_CMS_CONTENT_COMMENTS_SEARCH_VALIDATED")); 179 entries.put("with-nonvalidated-comments", new I18nizableText("plugin.cms", "PLUGINS_CMS_CONTENT_COMMENTS_SEARCH_NOTVALIDATED")); 180 181 return new EnumeratorDefinition(entries); 182 } 183 184}