001/*
002 *  Copyright 2019 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.requesttime.input;
017
018import org.apache.commons.lang3.StringUtils;
019
020import org.ametys.web.frontoffice.search.instance.SearchServiceInstance;
021import org.ametys.web.frontoffice.search.instance.model.ResultDisplayType;
022import org.ametys.web.repository.page.ZoneItem;
023
024/**
025 * Validator to determine if the search has to be launched or not
026 * <br>If both {@link #rejects()} and {@link #validate()} return <code>false</code>, then it is unknown and the search will not be launched.
027 */
028
029/*
030 * For developers:
031 * The business logic of this class is the migration of the business logic code of the search service v1
032 * The part of org.ametys.web.frontoffice.AbstractSearchGenerator.isInputValid()
033 * Which was rewritten and modify a little as time went by
034 */
035public class InputValidator
036{
037    /** The current service instance */
038    protected SearchServiceInstance _serviceInstance;
039    /** The current zone item */
040    protected ZoneItem _zoneItem;
041    /** The value of the parameter for "submit-form" */
042    protected String _submittedFormParamValue;
043    /** The zone item id the request comes from */
044    protected String _fromZoneItemId;
045
046    /**
047     * Constructor
048     * @param serviceInstance The current service instance
049     * @param zoneItem The current zone item
050     * @param submittedFormParamValue The value of the parameter for "submit-form"
051     * @param fromZoneItemId The zone item id the request comes from
052     */
053    public InputValidator(SearchServiceInstance serviceInstance, ZoneItem zoneItem, String submittedFormParamValue, String fromZoneItemId)
054    {
055        _serviceInstance = serviceInstance;
056        _zoneItem = zoneItem;
057        _submittedFormParamValue = submittedFormParamValue;
058        _fromZoneItemId = fromZoneItemId;
059    }
060    
061    /**
062     * Determines if the search has <b>NOT</b> to be launched
063     * <br>If this method returns <code>true</code>, then it will ensure that is the search will not be launched,
064     * even though {@link #validate()} would return <code>true</code> as well.
065     * @return <code>true</code> if the search has <b>NOT</b> to be launched
066     */
067    public boolean rejects()
068    {
069        return resultDisplayTypeIsOnPage();
070    }
071    
072    /**
073     * {@link ResultDisplayType} of the service instance is {@link ResultDisplayType#ON_PAGE on page} (whether on another or on itself)
074     * @return <code>true</code> if {@link ResultDisplayType} of the service instance is {@link ResultDisplayType#ON_PAGE on page} (whether on another or on itself)
075     */
076    protected boolean resultDisplayTypeIsOnPage()
077    {
078        // Sometimes the target page is itself because the results will be displayed on another zone (thus, on another service instance)
079        // When submitted, it sould not launch search anyway
080        return _serviceInstance.getResultDisplay().getType() == ResultDisplayType.ON_PAGE;
081    }
082    
083    /**
084     * Validates in order to determine if the search has to be launched or not
085     * @return <code>true</code> if the search has to be launched
086     */
087    public boolean validate()
088    {
089        return notPartOfAService()
090                || comesFromSourcePageWithNoGroupId()
091                || comesFromSameGroupId()
092                || comesFromSameZoneItemId();
093    }
094    
095    /**
096     * The current request is not part of a service
097     * @return <code>true</code> if the current request is not part of a service
098     */
099    protected boolean notPartOfAService()
100    {
101        // Be sure to return true if request is not part of a service
102        return _zoneItem == null;
103    }
104    
105    /**
106     * Request comes from another page with a search service configured with {@link ResultDisplayType#ON_PAGE} and no service group id
107     * @return <code>true</code> if request comes from another page with a search service configured with {@link ResultDisplayType#ON_PAGE} and no service group id
108     */
109    protected boolean comesFromSourcePageWithNoGroupId()
110    {
111        return StringUtils.isEmpty(_submittedFormParamValue) && _fromZoneItemId == null;
112    }
113    
114    /**
115     * It comes from the same group id
116     * @return <code>true</code> if it comes from the same group id
117     */
118    protected boolean comesFromSameGroupId()
119    {
120        String currentServiceGroupId = _serviceInstance.getResultDisplay().serviceGroupId();
121        // Same service group ids
122        return StringUtils.isNotEmpty(currentServiceGroupId) && currentServiceGroupId.equals(_submittedFormParamValue);
123    }
124    
125    /**
126     * It comes from the same zone item id
127     * @return  <code>true</code> if it comes from the same zone item id
128     */
129    protected boolean comesFromSameZoneItemId()
130    {
131        // Same zone item id
132        return _zoneItem.getId().equals(_fromZoneItemId);
133    }
134}