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.plugins.repository.AmetysObjectResolver;
021import org.ametys.web.repository.page.Page;
022
023/**
024 * A configuration of result display.
025 */
026public class ResultDisplay
027{
028    private ResultDisplayType _type;
029    // keep id instead of real page object and then re-resolve it to avoid RepositoryException about session closed
030    private String _pageId;
031    private Boolean _launchSearchAtStartup;
032    private String _serviceGroupId;
033    private AmetysObjectResolver _resolver;
034
035    /**
036     * Creates a ResultDisplay
037     * @param type the type
038     * @param page the page. Must be non-null if type is {@link ResultDisplayType#ON_PAGE}, must be null otherwise.
039     * @param launchSearchAtStartup true to launch a search at startup. Must be null if type is {@link ResultDisplayType#ON_PAGE}, must not otherwise.
040     * @param serviceGroupId the service group id
041     * @param resolver The ametys object resolver
042     */
043    public ResultDisplay(ResultDisplayType type,
044            String page,
045            Boolean launchSearchAtStartup,
046            String serviceGroupId,
047            AmetysObjectResolver resolver)
048    {
049        _type = type;
050        
051        if (_type != ResultDisplayType.ON_PAGE && page != null)
052        {
053            throw new IllegalArgumentException("page cannot be set with a ResultDisplayType not equals to " + ResultDisplayType.ON_PAGE);
054        }
055        else if (_type == ResultDisplayType.ON_PAGE && page == null)
056        {
057            throw new IllegalArgumentException("page cannot be null with a ResultDisplayType equals to " + ResultDisplayType.ON_PAGE);
058        }
059        _pageId = page;
060        
061        if (_type == ResultDisplayType.ON_PAGE && launchSearchAtStartup != null)
062        {
063            throw new IllegalArgumentException("launchSearchAtStartup cannot be set with a ResultDisplayType equals to " + ResultDisplayType.ON_PAGE);
064        }
065        else if (_type != ResultDisplayType.ON_PAGE && launchSearchAtStartup == null)
066        {
067            throw new IllegalArgumentException("launchSearchAtStartup cannot be null with a ResultDisplayType not equals to " + ResultDisplayType.ON_PAGE);
068        }
069        _launchSearchAtStartup = launchSearchAtStartup;
070        
071        _serviceGroupId = serviceGroupId;
072        
073        _resolver = resolver;
074    }
075    
076    /**
077     * Gets the type
078     * @return the type
079     */
080    public ResultDisplayType getType()
081    {
082        return _type;
083    }
084    
085    /**
086     * Gets the result page. Must be non-empty if {@link #getType()} returns {@link ResultDisplayType#ON_PAGE}, must be empty otherwise.
087     * @return the result page
088     */
089    public Optional<Page> resultPage()
090    {
091        return Optional.ofNullable(_pageId)
092                .map(_resolver::resolveById);
093    }
094    
095    /**
096     * Returns <code>true</code> if a search at startup must be launched. Must be empty if {@link #getType()} returns {@link ResultDisplayType#ON_PAGE}, must not otherwise.
097     * @return <code>true</code> if a search at startup must be launched
098     */
099    public Optional<Boolean> launchSearchAtStartup()
100    {
101        return Optional.ofNullable(_launchSearchAtStartup);
102    }
103    
104    /**
105     * Gets the service group id
106     * @return the service group id
107     */
108    public String serviceGroupId()
109    {
110        return _serviceGroupId;
111    }
112}