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