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.LinkedHashMap; 019import java.util.Map; 020 021import org.apache.avalon.framework.configuration.Configuration; 022import org.apache.avalon.framework.configuration.ConfigurationException; 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025 026import org.ametys.cms.repository.Content; 027import org.ametys.cms.search.model.SystemProperty; 028import org.ametys.cms.search.query.Query; 029import org.ametys.cms.search.query.Query.Operator; 030import org.ametys.cms.search.systemprop.AbstractIndexableSystemProperty; 031import org.ametys.runtime.model.Enumerator; 032import org.ametys.runtime.model.type.DataContext; 033import org.ametys.runtime.model.type.ModelItemTypeConstants; 034import org.ametys.runtime.plugin.component.ThreadSafeComponentManager; 035import org.ametys.web.indexing.solr.SolrWebFieldNames; 036import org.ametys.web.repository.content.WebContent; 037import org.ametys.web.repository.site.Site; 038import org.ametys.web.search.misc.SiteQueryHelper; 039import org.ametys.web.site.SiteEnumerator; 040 041/** 042 * {@link SystemProperty} which represents the site of a Content. 043 */ 044public class SiteSystemProperty extends AbstractIndexableSystemProperty<String, String, Content> 045{ 046 /** Solr field name. */ 047 public static final String SOLR_FIELD_NAME = SolrWebFieldNames.SITE_NAME; 048 049 /** The site query helper */ 050 protected SiteQueryHelper _siteQueryHelper; 051 052 @Override 053 public void service(ServiceManager manager) throws ServiceException 054 { 055 super.service(manager); 056 _siteQueryHelper = (SiteQueryHelper) manager.lookup(SiteQueryHelper.ROLE); 057 } 058 059 @Override 060 public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters) 061 { 062 return _siteQueryHelper.getQuery(value, operator, getName(), contextualParameters); 063 } 064 065 @Override 066 public String getDefaultCriterionWidget() 067 { 068 return "edition.select-site"; 069 } 070 071 @Override 072 public String getRenderer() 073 { 074 return "Ametys.plugins.web.search.ContentSearchTool.renderSite"; 075 } 076 077 @Override 078 public String getConverter() 079 { 080 return "Ametys.plugins.web.search.ContentSearchTool.convertSite"; 081 } 082 083 @Override 084 public String getSolrFieldName() 085 { 086 return SOLR_FIELD_NAME; 087 } 088 089 @Override 090 public String getSolrSortFieldName() 091 { 092 return SOLR_FIELD_NAME + "_sort"; 093 } 094 095 @Override 096 public String getSolrFacetFieldName() 097 { 098 return SOLR_FIELD_NAME + "_dv"; 099 } 100 101 @Override 102 public Object getValue(Content content) 103 { 104 if (content instanceof WebContent webContent) 105 { 106 return webContent.getSiteName(); 107 } 108 109 return null; 110 } 111 112 @Override 113 public Object valueToJSON(Content content, DataContext context) 114 { 115 String value = (String) getValue(content); 116 if (value != null) 117 { 118 Map<String, Object> info = new LinkedHashMap<>(); 119 info.put("name", value); 120 121 Site site = ((WebContent) content).getSite(); 122 if (site != null) 123 { 124 info.put("title", site.getTitle()); 125 } 126 return info; 127 } 128 129 return value; 130 } 131 132 @Override 133 public Object getSortValue(Content content) 134 { 135 if (content instanceof WebContent webContent) 136 { 137 return webContent.getSite().getTitle(); 138 } 139 140 return null; 141 } 142 143 @Override 144 public Enumerator<String> getDefaultCriterionEnumerator(Configuration configuration, ThreadSafeComponentManager<Enumerator> enumeratorManager) throws ConfigurationException 145 { 146 String role = "enumerator"; 147 enumeratorManager.addComponent(getPluginName(), null, role, SiteEnumerator.class, configuration); 148 149 try 150 { 151 enumeratorManager.initialize(); 152 return enumeratorManager.lookup(role); 153 } 154 catch (Exception e) 155 { 156 throw new ConfigurationException("Unable to initialize the site enumerator for system property '" + getName() + "'.", configuration, e); 157 } 158 } 159 160 @Override 161 protected String getTypeId() 162 { 163 return ModelItemTypeConstants.STRING_TYPE_ID; 164 } 165}