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.Collections; 020import java.util.Map; 021 022import org.apache.avalon.framework.service.ServiceException; 023import org.xml.sax.ContentHandler; 024import org.xml.sax.SAXException; 025 026import org.ametys.cms.contenttype.ContentType; 027import org.ametys.cms.contenttype.ContentTypeExtensionPoint; 028import org.ametys.cms.contenttype.ContentTypesHelper; 029import org.ametys.cms.data.ContentValue; 030import org.ametys.cms.data.type.ModelItemTypeConstants; 031import org.ametys.cms.data.type.ModelItemTypeExtensionPoint; 032import org.ametys.cms.data.type.indexing.IndexableElementType; 033import org.ametys.cms.model.ContentElementDefinition; 034import org.ametys.cms.search.model.SearchModelCriterionDefinition; 035import org.ametys.core.util.XMLUtils; 036import org.ametys.runtime.model.DefinitionContext; 037import org.ametys.runtime.model.ModelItem; 038 039/** 040 * Implementation of {@link SearchModelCriterionDefinition} for data of type content. 041 */ 042public class ContentSearchModelCriterionDefinition extends DefaultSearchModelCriterionDefinition<ContentValue> implements ContentElementDefinition 043{ 044 /** The criterion's content type identifier. */ 045 protected String _contentTypeId; 046 047 private ContentTypesHelper _contentTypesHelper; 048 private ContentTypeExtensionPoint _contentTypeExtensionPoint; 049 private ModelItemTypeExtensionPoint _criterionTypeExtensionPoint; 050 051 public String getContentTypeId() 052 { 053 return _contentTypeId; 054 } 055 056 public void setContentTypeId(String contentTypeId) 057 { 058 _contentTypeId = contentTypeId; 059 } 060 061 public Collection< ? extends ModelItem> getModelItems() 062 { 063 if (_contentTypeId != null && _getContentTypeExtensionPoint().hasExtension(_contentTypeId)) 064 { 065 ContentType contentType = _getContentTypeExtensionPoint().getExtension(_contentTypeId); 066 return contentType.getModelItems(); 067 } 068 else 069 { 070 return Collections.singleton(_getContentTypesHelper().getTitleAttributeDefinition()); 071 } 072 } 073 074 @Override 075 protected Map<String, Object> _toJSON(DefinitionContext context) 076 { 077 Map<String, Object> result = super._toJSON(context); 078 result.put("contentType", getContentTypeId()); 079 return result; 080 } 081 082 @Override 083 public void toSAX(ContentHandler contentHandler, DefinitionContext context) throws SAXException 084 { 085 super.toSAX(contentHandler, context); 086 XMLUtils.createElementIfNotNull(contentHandler, "contentType", getContentTypeId()); 087 } 088 089 @Override 090 public void copyTo(SearchModelCriterionDefinition<ContentValue> criterion, Map<String, Object> contextualParameters) 091 { 092 super.copyTo(criterion, contextualParameters); 093 094 if (criterion instanceof ContentElementDefinition contentCriterion) 095 { 096 contentCriterion.setContentTypeId(getContentTypeId()); 097 } 098 } 099 100 @SuppressWarnings("unchecked") 101 @Override 102 public IndexableElementType<ContentValue> getType() 103 { 104 String typeId = ModelItemTypeConstants.CONTENT_ELEMENT_TYPE_ID; 105 return (IndexableElementType<ContentValue>) _getCriterionTypeExtensionPoint().getExtension(typeId); 106 } 107 108 /** 109 * Retrieves the {@link ContentTypesHelper} 110 * @return the {@link ContentTypesHelper} 111 */ 112 protected ContentTypesHelper _getContentTypesHelper() 113 { 114 if (_contentTypesHelper == null) 115 { 116 try 117 { 118 _contentTypesHelper = (ContentTypesHelper) __serviceManager.lookup(ContentTypesHelper.ROLE); 119 } 120 catch (ServiceException e) 121 { 122 throw new RuntimeException("Unable to lookup after the content types helper component", e); 123 } 124 } 125 126 return _contentTypesHelper; 127 } 128 129 /** 130 * Retrieves the {@link ContentTypeExtensionPoint} 131 * @return the {@link ContentTypeExtensionPoint} 132 */ 133 protected ContentTypeExtensionPoint _getContentTypeExtensionPoint() 134 { 135 if (_contentTypeExtensionPoint == null) 136 { 137 try 138 { 139 _contentTypeExtensionPoint = (ContentTypeExtensionPoint) __serviceManager.lookup(ContentTypeExtensionPoint.ROLE); 140 } 141 catch (ServiceException e) 142 { 143 throw new RuntimeException("Unable to lookup after the content type extension point", e); 144 } 145 } 146 147 return _contentTypeExtensionPoint; 148 } 149 150 /** 151 * Retrieves the {@link ModelItemTypeExtensionPoint} for available criterion types 152 * @return the {@link ModelItemTypeExtensionPoint} for available criterion types 153 */ 154 protected ModelItemTypeExtensionPoint _getCriterionTypeExtensionPoint() 155 { 156 if (_criterionTypeExtensionPoint == null) 157 { 158 try 159 { 160 _criterionTypeExtensionPoint = (ModelItemTypeExtensionPoint) __serviceManager.lookup(ModelItemTypeExtensionPoint.ROLE_CRITERION_DEFINITION); 161 } 162 catch (ServiceException e) 163 { 164 throw new RuntimeException("Unable to lookup after the criterion type extension point", e); 165 } 166 } 167 168 return _criterionTypeExtensionPoint; 169 } 170}