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.instance.model; 017 018import java.util.Optional; 019 020import org.ametys.runtime.parameter.Validator; 021import org.ametys.web.frontoffice.search.instance.SearchServiceInstance; 022import org.ametys.web.frontoffice.search.metamodel.EnumeratedValues.RestrictedValues; 023import org.ametys.web.frontoffice.search.metamodel.SearchCriterionDefinition; 024 025/** 026 * A FO search criterion, representing a criterion valued by the webmaster when creating its {@link SearchServiceInstance}, 027 * i.e. a filled line in the 'Criterion' page (third page) on the creation/edition dialog box of a search service. 028 */ 029public class FOSearchCriterion 030{ 031 private String _id; 032 private SearchCriterionDefinition _criterionDefinition; 033 private String _operator; 034 private FOSearchCriterionMode _mode; 035 private RestrictedValues _restrictedValues; 036 private Object _staticValue; 037 038 /** 039 * Creates a SearchCriterion for Front-Office search service 040 * @param id the id of the criterion 041 * @param criterionDefinition the definition of the criterion 042 * @param operator the operator (as string) 043 * @param mode the mode 044 * @param restrictedValues the restricted values. Must be non-empty if mode is {@link FOSearchCriterionMode#RESTRICTED_USER_INPUT}, must be empty otherwise. 045 * @param staticValue the static value. Must be non-empty if mode is {@link FOSearchCriterionMode#STATIC}, must be empty otherwise. 046 */ 047 public FOSearchCriterion( 048 String id, 049 SearchCriterionDefinition criterionDefinition, 050 String operator, 051 FOSearchCriterionMode mode, 052 RestrictedValues restrictedValues, 053 Object staticValue) 054 { 055 _id = id; 056 _criterionDefinition = criterionDefinition; 057 _operator = operator; 058 _mode = mode; 059 060 if (_mode != FOSearchCriterionMode.RESTRICTED_USER_INPUT && restrictedValues != null) 061 { 062 throw new IllegalArgumentException("restricted values cannot be set with a FOSearchCriterionMode not equals to " + FOSearchCriterionMode.RESTRICTED_USER_INPUT.toString()); 063 } 064 else if (_mode == FOSearchCriterionMode.RESTRICTED_USER_INPUT && restrictedValues == null) 065 { 066 throw new IllegalArgumentException("restricted values cannot be null with a FOSearchCriterionMode equals to " + FOSearchCriterionMode.RESTRICTED_USER_INPUT.toString()); 067 } 068 _restrictedValues = restrictedValues; 069 070 if (_mode != FOSearchCriterionMode.STATIC && staticValue != null) 071 { 072 throw new IllegalArgumentException("static value cannot be set with a FOSearchCriterionMode not equals to " + FOSearchCriterionMode.STATIC.toString()); 073 } 074 else if (_mode == FOSearchCriterionMode.STATIC && staticValue == null) 075 { 076 throw new IllegalArgumentException("static value cannot be null with a FOSearchCriterionMode equals to " + FOSearchCriterionMode.STATIC.toString()); 077 } 078 _staticValue = staticValue; 079 } 080 081 /** 082 * Gets the id of the criterion 083 * @return the id of the criterion 084 */ 085 public String getId() 086 { 087 return _id; 088 } 089 090 /** 091 * Gets the definition of the criterion 092 * @return the definition of the criterion 093 */ 094 public SearchCriterionDefinition getCriterionDefinition() 095 { 096 return _criterionDefinition; 097 } 098 099 /** 100 * Gets the operator (as string) 101 * @return the operator (as string) 102 */ 103 public String getOperator() 104 { 105 return _operator; 106 } 107 108 /** 109 * Gets the mode 110 * @return the mode 111 */ 112 public FOSearchCriterionMode getMode() 113 { 114 return _mode; 115 } 116 117 /** 118 * Gets the restricted values. Must be non-empty if {@link #getMode()} returns {@link FOSearchCriterionMode#RESTRICTED_USER_INPUT}, must be empty otherwise. 119 * @return the restricted values 120 */ 121 public Optional<RestrictedValues> getRestrictedValues() 122 { 123 return Optional.ofNullable(_restrictedValues); 124 } 125 126 /** 127 * Gets the static value. Must be non-empty if {@link #getMode()} returns {@link FOSearchCriterionMode#STATIC}, must be empty otherwise. 128 * @return the static value 129 */ 130 public Optional<Object> getStaticValue() 131 { 132 return Optional.ofNullable(_staticValue); 133 } 134 135 /** 136 * True if the criterion is linked to mandatory data 137 * @return <code>true</code> if the criterion is linked to mandatory data 138 */ 139 public boolean isMandatory() 140 { 141 Validator validator = getCriterionDefinition().getValidator(); 142 if (validator != null) 143 { 144 return validator.getConfiguration().containsKey("mandatory") && (Boolean) validator.getConfiguration().get("mandatory"); 145 } 146 147 return false; 148 } 149}