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.stream.Stream;
019
020/**
021 * A site context type
022 */
023public enum SiteContextType
024{
025    /**
026     * The site of the context is the current one.
027     */
028    CURRENT("CURRENT_SITE"),
029    /**
030     * The site of the context can be one among the ones returned by {@link SiteContext#getSites()}.
031     */
032    AMONG("SITES_LIST"),
033    /**
034     * The site of the context can be any site.
035     */
036    ALL("SITES"),
037    /**
038     * The site of the context can be any site but the current one.
039     */
040    OTHERS("OTHER_SITES");
041    
042    private String _clientSideName;
043
044    private SiteContextType(String clientSideName)
045    {
046        _clientSideName = clientSideName;
047    }
048    
049    /**
050     * Gets the client side name of the type of site context
051     * @return the client side name of the type of site context
052     */
053    public String clientSideName()
054    {
055        return _clientSideName;
056    }
057    
058    /**
059     * Gets a {@link SiteContextType} from its client-side name
060     * @param clientSideName the client-side name
061     * @return the {@link SiteContextType}
062     */
063    public static SiteContextType fromClientSideName(String clientSideName)
064    {
065        return Stream.of(SiteContextType.values())
066                .filter(t -> t.clientSideName().equals(clientSideName))
067                .findAny()
068                .orElseThrow(() -> new IllegalArgumentException("No SiteContextType with client side name '" + clientSideName + "'"));
069    }
070}