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