001/* 002 * Copyright 2024 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.Map; 019 020import org.apache.avalon.framework.service.ServiceException; 021import org.apache.avalon.framework.service.ServiceManager; 022import org.apache.cocoon.xml.XMLUtils; 023import org.apache.commons.lang3.StringUtils; 024import org.apache.solr.common.SolrInputDocument; 025import org.xml.sax.ContentHandler; 026import org.xml.sax.SAXException; 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.query.ContentIdQuery; 035import org.ametys.cms.search.query.Query; 036import org.ametys.cms.search.query.Query.Operator; 037import org.ametys.runtime.model.type.DataContext; 038import org.ametys.runtime.model.type.ModelItemTypeConstants; 039 040/** 041 * {@link SystemProperty} which represents the current id of a content. 042 */ 043public class ContentIdSystemProperty extends AbstractSystemProperty<String, Content> implements CriterionDefinitionAwareElementDefinition<String>, IndexationAwareSystemProperty<String, Content> 044{ 045 /** Solr field name. */ 046 public static final String SOLR_FIELD_NAME = "id"; 047 048 /** The criterion definition helper */ 049 protected CriterionDefinitionHelper _criterionDefinitionHelper; 050 051 @Override 052 public void service(ServiceManager manager) throws ServiceException 053 { 054 super.service(manager); 055 _criterionDefinitionHelper = (CriterionDefinitionHelper) manager.lookup(CriterionDefinitionHelper.ROLE); 056 } 057 058 @Override 059 public void indexValue(SolrInputDocument document, Content ametysObject, CMSDataContext context) 060 { 061 // Identifier is already indexed by default 062 } 063 064 public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters) 065 { 066 if (StringUtils.isEmpty((String) value)) 067 { 068 return new ContentIdQuery(Operator.EXISTS); 069 } 070 071 return new ContentIdQuery(operator, (String) value); 072 } 073 074 public String getSolrFieldName() 075 { 076 return SOLR_FIELD_NAME; 077 } 078 079 public String getSolrSortFieldName() 080 { 081 return SOLR_FIELD_NAME; 082 } 083 084 public String getSolrFacetFieldName() 085 { 086 return SOLR_FIELD_NAME + "_dv"; 087 } 088 089 @Override 090 public Object getValue(Content content) 091 { 092 return content.getId(); 093 } 094 095 public Object valueToJSON(Content content, DataContext context) 096 { 097 return getValue(content); 098 } 099 100 public void valueToSAX(ContentHandler contentHandler, Content content, DataContext context) throws SAXException 101 { 102 XMLUtils.createElement(contentHandler, getName(), (String) getValue(content)); 103 } 104 105 @Override 106 protected String getTypeId() 107 { 108 return ModelItemTypeConstants.STRING_TYPE_ID; 109 } 110 111 public IndexableElementType getDefaultCriterionType() 112 { 113 CMSDataContext context = CMSDataContext.newInstance() 114 .withModelItem(this); 115 116 String typeId = getType().getDefaultCriterionTypeId(context); 117 return _criterionDefinitionHelper.getCriterionDefinitionType(typeId); 118 } 119}