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.web.search.systemprop; 017 018import java.util.HashMap; 019import java.util.LinkedHashMap; 020import java.util.Map; 021 022import org.apache.avalon.framework.service.ServiceException; 023import org.apache.avalon.framework.service.ServiceManager; 024 025import org.ametys.cms.data.type.indexing.IndexableElementType; 026import org.ametys.cms.model.CMSDataContext; 027import org.ametys.cms.repository.Content; 028import org.ametys.cms.search.model.CriterionDefinitionAwareElementDefinition; 029import org.ametys.cms.search.model.CriterionDefinitionHelper; 030import org.ametys.cms.search.model.SystemProperty; 031import org.ametys.cms.search.query.Query; 032import org.ametys.cms.search.query.Query.Operator; 033import org.ametys.cms.search.systemprop.AbstractSystemProperty; 034import org.ametys.cms.search.systemprop.IndexationAwareSystemProperty; 035import org.ametys.runtime.model.type.DataContext; 036import org.ametys.runtime.model.type.ModelItemTypeConstants; 037import org.ametys.web.repository.content.SharedContent; 038import org.ametys.web.repository.content.WebContent; 039import org.ametys.web.repository.site.Site; 040import org.ametys.web.search.query.SharedQuery; 041 042/** 043 * {@link SystemProperty} which represents the shared status of a Content. 044 */ 045public class SharedSystemProperty extends AbstractSystemProperty<Boolean, Content> implements CriterionDefinitionAwareElementDefinition<Boolean>, IndexationAwareSystemProperty<Boolean, Content> 046{ 047 /** Solr field name. */ 048 public static final String SOLR_FIELD_NAME = "shared"; 049 050 /** The criterion definition helper */ 051 protected CriterionDefinitionHelper _criterionDefinitionHelper; 052 053 @Override 054 public void service(ServiceManager manager) throws ServiceException 055 { 056 super.service(manager); 057 _criterionDefinitionHelper = (CriterionDefinitionHelper) manager.lookup(CriterionDefinitionHelper.ROLE); 058 } 059 060 @Override 061 public String getConverter() 062 { 063 return "Ametys.plugins.web.search.ContentSearchTool.convertSharedContent"; 064 } 065 066 @Override 067 public String getRenderer() 068 { 069 return "Ametys.plugins.web.search.ContentSearchTool.renderSharedContent"; 070 } 071 072 @Override 073 public Integer getColumnWidth() 074 { 075 return 60; 076 } 077 078 @Override 079 public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters) 080 { 081 if (value == null) 082 { 083 return new SharedQuery(); 084 } 085 return new SharedQuery((Boolean) value, operator); 086 } 087 088 public String getSolrFieldName() 089 { 090 return SOLR_FIELD_NAME; 091 } 092 093 public String getSolrSortFieldName() 094 { 095 return SOLR_FIELD_NAME + "_sort"; 096 } 097 098 public String getSolrFacetFieldName() 099 { 100 return SOLR_FIELD_NAME; 101 } 102 103 @Override 104 public Object getValue(Content content) 105 { 106 return content instanceof SharedContent; 107 } 108 109 @Override 110 public Object valueToJSON(Content content, DataContext context) 111 { 112 boolean value = (boolean) getValue(content); 113 Map<String, Object> infos = new LinkedHashMap<>(); 114 115 infos.put("isShared", value); 116 117 if (value) 118 { 119 Content sharedContent = ((SharedContent) content).getInitialContent(); 120 if (sharedContent != null && sharedContent instanceof WebContent) 121 { 122 Map<String, String> orginalSite = new HashMap<>(); 123 orginalSite.put("name", ((WebContent) sharedContent).getSiteName()); 124 125 Site site = ((WebContent) sharedContent).getSite(); 126 if (site != null) 127 { 128 orginalSite.put("title", site.getTitle()); 129 } 130 131 infos.put("originalSite", orginalSite); 132 } 133 } 134 135 return infos; 136 } 137 138 @Override 139 protected String getTypeId() 140 { 141 return ModelItemTypeConstants.BOOLEAN_TYPE_ID; 142 } 143 144 public IndexableElementType getDefaultCriterionType() 145 { 146 CMSDataContext context = CMSDataContext.newInstance() 147 .withModelItem(this); 148 149 String typeId = getType().getDefaultCriterionTypeId(context); 150 return _criterionDefinitionHelper.getCriterionDefinitionType(typeId); 151 } 152}