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.cocoon.xml.XMLUtils; 021import org.apache.commons.lang3.StringUtils; 022import org.apache.solr.common.SolrInputDocument; 023import org.xml.sax.ContentHandler; 024import org.xml.sax.SAXException; 025 026import org.ametys.cms.model.CMSDataContext; 027import org.ametys.cms.repository.Content; 028import org.ametys.cms.search.model.SystemProperty; 029import org.ametys.cms.search.query.ContentIdQuery; 030import org.ametys.cms.search.query.Query; 031import org.ametys.cms.search.query.Query.Operator; 032import org.ametys.runtime.model.type.DataContext; 033import org.ametys.runtime.model.type.ModelItemTypeConstants; 034 035/** 036 * {@link SystemProperty} which represents the current id of a content. 037 */ 038public class ContentIdSystemProperty extends AbstractIndexableSystemProperty<String, String, Content> 039{ 040 /** Solr field name. */ 041 public static final String SOLR_FIELD_NAME = "id"; 042 043 @Override 044 public void indexValue(SolrInputDocument document, Content ametysObject, CMSDataContext context) 045 { 046 // Identifier is already indexed by default 047 } 048 049 @Override 050 public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters) 051 { 052 if (StringUtils.isEmpty((String) value)) 053 { 054 return new ContentIdQuery(Operator.EXISTS); 055 } 056 057 return new ContentIdQuery(operator, (String) value); 058 } 059 060 @Override 061 public String getSolrFieldName() 062 { 063 return SOLR_FIELD_NAME; 064 } 065 066 @Override 067 public String getSolrSortFieldName() 068 { 069 return SOLR_FIELD_NAME; 070 } 071 072 @Override 073 public String getSolrFacetFieldName() 074 { 075 return SOLR_FIELD_NAME + "_dv"; 076 } 077 078 @Override 079 public Object getValue(Content content) 080 { 081 return content.getId(); 082 } 083 084 public Object valueToJSON(Content content, DataContext context) 085 { 086 return getValue(content); 087 } 088 089 public void valueToSAX(ContentHandler contentHandler, Content content, DataContext context) throws SAXException 090 { 091 XMLUtils.createElement(contentHandler, getName(), (String) getValue(content)); 092 } 093 094 @Override 095 protected String getTypeId() 096 { 097 return ModelItemTypeConstants.STRING_TYPE_ID; 098 } 099}