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.ElementDefinition; 030import org.ametys.runtime.model.ModelItem; 031 032/** 033 * Implementation for {@link SearchModelCriterionDefinition} searching on a model item of type content. 034 */ 035public class ContentReferencingSearchModelCriterionDefinition extends ReferencingSearchModelCriterionDefinition<ContentValue> implements ContentElementDefinition 036{ 037 /** 038 * Default constructor. 039 */ 040 public ContentReferencingSearchModelCriterionDefinition() 041 { 042 // Nothing to do 043 } 044 045 /** 046 * Constructor used to create a BO criterion definition on a referenced item of type content 047 * @param reference the item referenced by this criterion 048 * @param referencePath the path of the criterion's reference 049 */ 050 public ContentReferencingSearchModelCriterionDefinition(ElementDefinition reference, String referencePath) 051 { 052 super(reference, referencePath); 053 } 054 055 @Override 056 public ContentElementDefinition getReference() 057 { 058 return (ContentElementDefinition) super.getReference(); 059 } 060 061 public String getContentTypeId() 062 { 063 return getReference().getContentTypeId(); 064 } 065 066 public void setContentTypeId(String contentTypeId) 067 { 068 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"); 069 } 070 071 public Collection< ? extends ModelItem> getModelItems() 072 { 073 return getReference().getModelItems(); 074 } 075 076 @Override 077 protected Map<String, Object> _toJSON(DefinitionContext context) 078 { 079 Map<String, Object> result = super._toJSON(context); 080 result.put("contentType", getContentTypeId()); 081 return result; 082 } 083 084 @Override 085 public void toSAX(ContentHandler contentHandler, DefinitionContext context) throws SAXException 086 { 087 super.toSAX(contentHandler, context); 088 XMLUtils.createElementIfNotNull(contentHandler, "contentType", getContentTypeId()); 089 } 090}