001/* 002 * Copyright 2025 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.model.properties; 017 018import org.apache.avalon.framework.service.ServiceException; 019 020import org.ametys.cms.data.ametysobject.ModelAwareDataAwareAmetysObject; 021import org.ametys.cms.data.type.indexing.IndexableElementType; 022import org.ametys.cms.model.CMSDataContext; 023import org.ametys.cms.search.model.CriterionDefinitionAwareElementDefinition; 024import org.ametys.cms.search.model.CriterionDefinitionHelper; 025import org.ametys.cms.search.model.IndexationAwareElementDefinitionHelper; 026 027/** 028 * Abstract class for indexable and searchable property 029 * @param <T> type of the property values 030 * @param <C> type of criterion 031 * @param <X> type of ametys object supported by this property 032 */ 033public abstract class AbstractIndexableProperty<T, C, X extends ModelAwareDataAwareAmetysObject> extends AbstractProperty<T, X> implements CriterionDefinitionAwareElementDefinition<T, C, X> 034{ 035 /** The criterion definition helper */ 036 protected CriterionDefinitionHelper _criterionDefinitionHelper; 037 /** The indexable aware element definition helper */ 038 protected IndexationAwareElementDefinitionHelper _indexationAwareElementDefinitionHelper; 039 040 /** The type to use when rendering the element as a criterion */ 041 protected IndexableElementType _criterionType; 042 043 @Override 044 public void init(String availableTypesRole) throws Exception 045 { 046 super.init(availableTypesRole); 047 048 String typeId = getCriterionTypeId(); 049 _criterionType = _getCriterionDefinitionHelper().getCriterionDefinitionType(typeId); 050 } 051 052 public String getSolrSortFieldName() 053 { 054 return _getIndexationAwareElementDefinitionHelper().getDefaultSolrSortFieldName(this); 055 } 056 057 public IndexableElementType getCriterionType() 058 { 059 return _criterionType; 060 } 061 062 /** 063 * Get the identifier of type to use when rendering the property as a criterion 064 * @return the identifier of type to use for a criterion 065 */ 066 protected String getCriterionTypeId() 067 { 068 CMSDataContext context = CMSDataContext.newInstance() 069 .withModelItem(this); 070 return getType().getCriterionTypeId(context); 071 } 072 073 /** 074 * Retrieves the {@link CriterionDefinitionHelper} 075 * @return the {@link CriterionDefinitionHelper} 076 */ 077 protected CriterionDefinitionHelper _getCriterionDefinitionHelper() 078 { 079 if (_criterionDefinitionHelper == null) 080 { 081 try 082 { 083 _criterionDefinitionHelper = (CriterionDefinitionHelper) __serviceManager.lookup(CriterionDefinitionHelper.ROLE); 084 } 085 catch (ServiceException e) 086 { 087 throw new RuntimeException("Unable to lookup after the criterion definition helper", e); 088 } 089 } 090 091 return _criterionDefinitionHelper; 092 } 093 094 /** 095 * Retrieves the {@link CriterionDefinitionHelper} 096 * @return the {@link CriterionDefinitionHelper} 097 */ 098 protected IndexationAwareElementDefinitionHelper _getIndexationAwareElementDefinitionHelper() 099 { 100 if (_indexationAwareElementDefinitionHelper == null) 101 { 102 try 103 { 104 _indexationAwareElementDefinitionHelper = (IndexationAwareElementDefinitionHelper) __serviceManager.lookup(IndexationAwareElementDefinitionHelper.ROLE); 105 } 106 catch (ServiceException e) 107 { 108 throw new RuntimeException("Unable to lookup after the indexation aware element definition helper", e); 109 } 110 } 111 112 return _indexationAwareElementDefinitionHelper; 113 } 114}