001/* 002 * Copyright 2016 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.ArrayList; 019import java.util.Collection; 020import java.util.HashMap; 021import java.util.List; 022import java.util.Map; 023 024import org.apache.avalon.framework.configuration.Configuration; 025import org.apache.avalon.framework.service.ServiceException; 026import org.apache.avalon.framework.service.ServiceManager; 027 028import org.ametys.cms.data.type.indexing.IndexableElementType; 029import org.ametys.cms.model.CMSDataContext; 030import org.ametys.cms.repository.Content; 031import org.ametys.cms.search.model.CriterionDefinitionAwareElementDefinition; 032import org.ametys.cms.search.model.CriterionDefinitionHelper; 033import org.ametys.cms.search.model.SystemProperty; 034import org.ametys.cms.search.model.impl.ReferencingSearchModelCriterionDefinition; 035import org.ametys.cms.search.query.NotQuery; 036import org.ametys.cms.search.query.OrQuery; 037import org.ametys.cms.search.query.Query; 038import org.ametys.cms.search.query.Query.Operator; 039import org.ametys.cms.search.systemprop.AbstractSystemProperty; 040import org.ametys.cms.search.systemprop.IndexationAwareSystemProperty; 041import org.ametys.runtime.i18n.I18nizableText; 042import org.ametys.runtime.model.type.ModelItemTypeConstants; 043import org.ametys.web.repository.content.WebContent; 044import org.ametys.web.repository.page.Page; 045import org.ametys.web.search.query.ContentPageQuery; 046import org.ametys.web.search.query.PageQuery; 047 048/** 049 * {@link SystemProperty} which represents the pages of a Content. 050 */ 051public class PagesSystemProperty extends AbstractSystemProperty<String, Content> implements CriterionDefinitionAwareElementDefinition<String>, IndexationAwareSystemProperty<String, Content> 052{ 053 /** System property identifier */ 054 public static final String SYSTEM_PROPERTY_ID = "pages"; 055 056 /** Solr field name. */ 057 public static final String SOLR_FIELD_NAME = "pageIds"; 058 059 /** The criterion definition helper */ 060 protected CriterionDefinitionHelper _criterionDefinitionHelper; 061 062 @Override 063 public void service(ServiceManager manager) throws ServiceException 064 { 065 super.service(manager); 066 _criterionDefinitionHelper = (CriterionDefinitionHelper) manager.lookup(CriterionDefinitionHelper.ROLE); 067 } 068 069 @Override 070 public boolean isMultiple() 071 { 072 return true; 073 } 074 075 @Override 076 public boolean isDisplayable() 077 { 078 return false; 079 } 080 081 @Override 082 public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters) 083 { 084 List<Query> queries = new ArrayList<>(); 085 086 if (operator == Operator.EXISTS) 087 { 088 return new ContentPageQuery(new PageQuery(true)); 089 } 090 else 091 { 092 String[] pageIds = parseStringArray(value); 093 for (String pageId : pageIds) 094 { 095 queries.add(new ContentPageQuery(new PageQuery(pageId, true))); 096 } 097 } 098 099 Query query = new OrQuery(queries); 100 101 if (operator == Operator.NE) 102 { 103 query = new NotQuery(query); 104 } 105 return query; 106 } 107 108 @Override 109 public String getDefaultCriterionWidget() 110 { 111 return "edition.select-page"; 112 } 113 114 @Override 115 public Map<String, I18nizableText> getDefaultCriterionWidgetParameters(Configuration configuration) 116 { 117 Map<String, I18nizableText> params = new HashMap<>(); 118 119 params.put("siteContext", new I18nizableText("current")); 120 params.put("sitemapContext", new I18nizableText("current")); 121 // TODO Find a system to provide a simple "site" property in the criterion definition. 122 // The criterion definition in the model file declares "site" and the search model provides the real ID? 123 params.put("sitesField", new I18nizableText(ReferencingSearchModelCriterionDefinition.CRITERION_DEFINITION_PREFIX + "site-eq")); 124 125 return params; 126 } 127 128 public String getSolrFieldName() 129 { 130 return SOLR_FIELD_NAME; 131 } 132 133 public String getSolrSortFieldName() 134 { 135 return null; 136 } 137 138 public String getSolrFacetFieldName() 139 { 140 return null; 141 } 142 143 @Override 144 public Object getValue(Content content) 145 { 146 List<String> ids = new ArrayList<>(); 147 148 if (content instanceof WebContent webContent) 149 { 150 Collection<Page> pages = webContent.getReferencingPages(); 151 for (Page page : pages) 152 { 153 ids.add(page.getId()); 154 } 155 } 156 157 return ids.toArray(new String[ids.size()]); 158 } 159 160 @Override 161 protected String getTypeId() 162 { 163 return ModelItemTypeConstants.STRING_TYPE_ID; 164 } 165 166 public IndexableElementType getDefaultCriterionType() 167 { 168 CMSDataContext context = CMSDataContext.newInstance() 169 .withModelItem(this); 170 171 String typeId = getType().getDefaultCriterionTypeId(context); 172 return _criterionDefinitionHelper.getCriterionDefinitionType(typeId); 173 } 174}