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.model.impl;
017
018import java.util.Collection;
019import java.util.Map;
020
021import org.xml.sax.ContentHandler;
022import org.xml.sax.SAXException;
023
024import org.ametys.cms.data.ContentValue;
025import org.ametys.cms.model.ContentElementDefinition;
026import org.ametys.cms.search.model.SearchModelCriterionDefinition;
027import org.ametys.core.util.XMLUtils;
028import org.ametys.runtime.model.DefinitionContext;
029import org.ametys.runtime.model.ModelItem;
030
031/**
032 * Static implementation for {@link SearchModelCriterionDefinition} searching on a model item of type content.
033 */
034public class StaticContentReferencingSearchModelCriterionDefinition extends StaticReferencingSearchModelCriterionDefinition<ContentValue> implements ContentElementDefinition
035{
036    @Override
037    public ContentElementDefinition getReference()
038    {
039        return (ContentElementDefinition) super.getReference();
040    }
041
042    public String getContentTypeId()
043    {
044        return getReference().getContentTypeId();
045    }
046
047    public void setContentTypeId(String contentTypeId)
048    {
049        throw new UnsupportedOperationException("The criterion '" + getPath() + "' references an element of type content. The content type id can not be set to the criterion. Set it directly to the referenced element");
050    }
051
052    public Collection< ? extends ModelItem> getModelItems()
053    {
054        return getReference().getModelItems();
055    }
056    
057    @Override
058    protected Map<String, Object> _toJSON(DefinitionContext context)
059    {
060        Map<String, Object> result = super._toJSON(context);
061        result.put("contentType", getContentTypeId());
062        return result;
063    }
064    
065    @Override
066    public void toSAX(ContentHandler contentHandler, DefinitionContext context) throws SAXException
067    {
068        super.toSAX(contentHandler, context);
069        XMLUtils.createElementIfNotNull(contentHandler, "contentType", getContentTypeId());
070    }
071}