001/* 002 * Copyright 2018 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.web.frontoffice.search.metamodel.impl; 017 018import java.util.Map; 019import java.util.Optional; 020 021import org.apache.avalon.framework.service.ServiceException; 022 023import org.ametys.cms.data.type.ModelItemTypeExtensionPoint; 024import org.ametys.cms.search.model.impl.AbstractCriterionDefinition; 025import org.ametys.runtime.model.DefinitionContext; 026import org.ametys.runtime.model.Enumerator; 027import org.ametys.web.frontoffice.search.metamodel.RestrictedEnumerator; 028import org.ametys.web.frontoffice.search.metamodel.SearchServiceCriterionDefinition; 029import org.ametys.web.frontoffice.search.metamodel.SearchServiceCriterionDefinitionHelper; 030import org.ametys.web.frontoffice.search.metamodel.Searchable; 031 032/** 033 * Default implementation of {@link SearchServiceCriterionDefinition} 034 * @param <T> Type of the criterion value 035 */ 036public abstract class AbstractSearchServiceCriterionDefinition<T> extends AbstractCriterionDefinition<T> implements SearchServiceCriterionDefinition<T> 037{ 038 /** The extension point containing all available criterion types */ 039 protected ModelItemTypeExtensionPoint _criterionTypeExtensionPoint; 040 041 /** The search service criterion definition helper */ 042 protected SearchServiceCriterionDefinitionHelper _searchServiceCriterionDefinitionHelper; 043 044 private Optional<Searchable> _searchable = Optional.empty(); 045 046 public Optional<Searchable> getSearchable() 047 { 048 return _searchable; 049 } 050 051 public void setSearchable(Searchable searchable) 052 { 053 _searchable = Optional.ofNullable(searchable); 054 } 055 056 public boolean isEnumerated() 057 { 058 return getEnumerator() != null; 059 } 060 061 /** 062 * Determines if the current criterion definition is enumerated but can contain too much data 063 * @return <code>true</code> if the criterion definition can contain too much data, <code>false</code> otherwise 064 */ 065 protected boolean isTooBigForStaticEnumerator() 066 { 067 return false; 068 } 069 070 public RestrictedEnumerator<T> getRestrictedEnumerator(Map<String, Object> contextualParameters) 071 { 072 Enumerator<T> enumerator = getEnumerator(); 073 return enumerator != null 074 ? new RestrictedWrappedEnumerator<>(enumerator, getName()) 075 : null; 076 } 077 078 @Override 079 protected Map<String, Object> _toJSON(DefinitionContext context) 080 { 081 Map<String, Object> result = super._toJSON(context); 082 result.putAll(_getSearchServiceCriterionDefinitionHelper().getDefaultSearchServiceCriterionDefinitionJSON(this, isTooBigForStaticEnumerator())); 083 return result; 084 } 085 086 /** 087 * Releases and destroys any resource it owns. 088 */ 089 protected void dispose() 090 { 091 // Override if needed 092 } 093 094 /** 095 * Retrieves the {@link ModelItemTypeExtensionPoint} for available criterion types 096 * @return the {@link ModelItemTypeExtensionPoint} for available criterion types 097 */ 098 protected ModelItemTypeExtensionPoint _getCriterionTypeExtensionPoint() 099 { 100 if (_criterionTypeExtensionPoint == null) 101 { 102 try 103 { 104 _criterionTypeExtensionPoint = (ModelItemTypeExtensionPoint) __serviceManager.lookup(ModelItemTypeExtensionPoint.ROLE_CRITERION_DEFINITION); 105 } 106 catch (ServiceException e) 107 { 108 throw new RuntimeException("Unable to lookup after the criterion type extension point", e); 109 } 110 } 111 112 return _criterionTypeExtensionPoint; 113 } 114 115 /** 116 * Retrieves the search service criterion definition helper 117 * @return the search service criterion definition helper 118 */ 119 protected SearchServiceCriterionDefinitionHelper _getSearchServiceCriterionDefinitionHelper() 120 { 121 if (_searchServiceCriterionDefinitionHelper == null) 122 { 123 try 124 { 125 _searchServiceCriterionDefinitionHelper = (SearchServiceCriterionDefinitionHelper) __serviceManager.lookup(SearchServiceCriterionDefinitionHelper.ROLE); 126 } 127 catch (ServiceException e) 128 { 129 throw new RuntimeException("Unable to lookup after the search service criterion definition helper", e); 130 } 131 } 132 133 return _searchServiceCriterionDefinitionHelper; 134 } 135} 136