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.Map; 019import java.util.Optional; 020 021import org.apache.cocoon.xml.XMLUtils; 022import org.xml.sax.ContentHandler; 023import org.xml.sax.SAXException; 024 025import org.ametys.cms.model.CMSDataContext; 026import org.ametys.cms.search.model.SearchModel; 027import org.ametys.cms.search.model.SearchModelCriterionDefinition; 028import org.ametys.cms.search.query.Query; 029import org.ametys.cms.search.query.Query.LogicalOperator; 030import org.ametys.cms.search.query.Query.Operator; 031import org.ametys.runtime.model.DefinitionContext; 032 033/** 034 * Default implementation for {@link SearchModelCriterionDefinition} 035 * @param <T> Type of the criterion value 036 */ 037public class DefaultSearchModelCriterionDefinition<T> extends AbstractCriterionDefinition<T> implements SearchModelCriterionDefinition<T> 038{ 039 /** The criteria operator */ 040 protected Operator _operator; 041 042 public Operator getOperator() 043 { 044 return Optional.ofNullable(_operator) 045 .orElseGet(this::_getDefaultOperator); 046 } 047 048 /** 049 * Retrieves the default operator for the criterion 050 * @return the default operator for the criterion 051 */ 052 protected Operator _getDefaultOperator() 053 { 054 return getType().getDefaultCriterionOperator(CMSDataContext.newInstance() 055 .withModelItem(this)); 056 } 057 058 public void setOperator(Operator operator) 059 { 060 _operator = operator; 061 } 062 063 public void setMultipleOperandOperator(LogicalOperator multipleOperand) 064 { 065 _multipleOperandOperator = multipleOperand; 066 } 067 068 /** 069 * Retrieves the property used to match criteria when switching between simple and advanced search modes. 070 * @return the criterion property, which defaults to the criterion identifier 071 */ 072 protected String _getCriterionProperty() 073 { 074 return getName(); 075 } 076 077 @Override 078 protected Map<String, Object> _toJSON(DefinitionContext context) 079 { 080 Map<String, Object> json = super._toJSON(context); 081 082 Operator operator = getOperator(); 083 if (operator != null) 084 { 085 // TODO use operator.getName()? 086 json.put("criterionOperator", operator.toString().toLowerCase()); 087 } 088 089 json.put("criterionMultipleOperand", getMultipleOperandOperator().name()); 090 json.put("criterionProperty", _getCriterionProperty()); 091 return json; 092 } 093 094 @Override 095 public void toSAX(ContentHandler contentHandler, DefinitionContext context) throws SAXException 096 { 097 super.toSAX(contentHandler, context); 098 099 Operator operator = getOperator(); 100 if (operator != null) 101 { 102 // TODO use operator.getName()? 103 XMLUtils.createElement(contentHandler, "test-operator", operator.toString().toLowerCase()); 104 } 105 106 XMLUtils.createElement(contentHandler, "multiple-operand", getMultipleOperandOperator().name()); 107 } 108 109 @Override 110 public Query getQuery(Object value, Operator operator, Map<String, Object> allValues, String language, Map<String, Object> contextualParameters) 111 { 112 return super.getQuery(value, operator != null ? operator : getOperator(), allValues, language, contextualParameters); 113 } 114 115 @SuppressWarnings("unchecked") 116 public void copyTo(SearchModelCriterionDefinition<T> criterion, Map<String, Object> contextualParameters) 117 { 118 criterion.setModel(getModel()); 119 120 criterion.setName(getName()); 121 criterion.setPluginName(getPluginName()); 122 criterion.setLabel(getLabel()); 123 criterion.setDescription(getDescription()); 124 criterion.setWidget(getWidget()); 125 criterion.setWidgetParameters(getWidgetParameters()); 126 127 criterion.setType(getType()); 128 criterion.setMultiple(isMultiple()); 129 130 criterion.setEnumerator(getEnumerator()); 131 criterion.setCustomEnumerator(getCustomEnumerator()); 132 criterion.setEnumeratorConfiguration(getEnumeratorConfiguration()); 133 134 criterion.setValidator(getValidator()); 135 criterion.setCustomValidator(getCustomValidator()); 136 criterion.setValidatorConfiguration(getValidatorConfiguration()); 137 138 criterion.setParsedDefaultValues(getParsedDefaultValues()); 139 140 criterion.setOperator(getOperator()); 141 criterion.setMultipleOperandOperator(getMultipleOperandOperator()); 142 143 if (criterion instanceof DefaultSearchModelCriterionDefinition defaultCriterion) 144 { 145 defaultCriterion.setSolrFacetFieldName(getSolrFacetFieldName(contextualParameters)); 146 defaultCriterion.setJoinedPaths(getJoinedPaths(contextualParameters)); 147 } 148 } 149 150 @Override 151 public SearchModel getModel() 152 { 153 return (SearchModel) super.getModel(); 154 } 155}