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.data.type.indexing.IndexableDataContext;
027import org.ametys.cms.repository.Content;
028import org.ametys.cms.search.SearchField;
029import org.ametys.cms.search.model.SystemProperty;
030import org.ametys.cms.search.query.ContentIdQuery;
031import org.ametys.cms.search.query.Query;
032import org.ametys.cms.search.query.Query.Operator;
033import org.ametys.cms.search.solr.field.ContentIdSearchField;
034import org.ametys.runtime.model.type.DataContext;
035import org.ametys.runtime.model.type.ModelItemTypeConstants;
036
037/**
038 * {@link SystemProperty} which represents the current id of a content.
039 */
040public class ContentIdSystemProperty extends AbstractSystemProperty<String, Content>
041{
042    @Override
043    public void indexValue(SolrInputDocument document, Content ametysObject, IndexableDataContext context)
044    {
045        // Identifier is already indexed by default
046    }
047    
048    @Override
049    public boolean isMultiple()
050    {
051        return false;
052    }
053    
054    @Override
055    public boolean isSortable()
056    {
057        return true;
058    }
059    
060    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
061    {
062        if (StringUtils.isEmpty((String) value))
063        {
064            return new ContentIdQuery(Operator.EXISTS);
065        }
066        
067        return new ContentIdQuery(operator, (String) value);
068    }
069    
070    @Override
071    public SearchField getSearchField()
072    {
073        return new ContentIdSearchField();
074    }
075    
076    @Override
077    public Object getValue(Content content)
078    {
079        return content.getId();
080    }
081    
082    public Object valueToJSON(Content content, DataContext context)
083    {
084        return getValue(content);
085    }
086    
087    public void valueToSAX(ContentHandler contentHandler, Content content, DataContext context) throws SAXException
088    {
089        XMLUtils.createElement(contentHandler, getName(), (String) getValue(content));
090    }
091    
092    @Override
093    protected String _getTypeId()
094    {
095        return ModelItemTypeConstants.STRING_TYPE_ID;
096    }
097}