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