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.List;
019import java.util.Optional;
020import java.util.stream.Collectors;
021
022import org.ametys.web.repository.site.Site;
023import org.ametys.web.repository.site.SiteManager;
024
025/**
026 * A site context
027 */
028public class SiteContext
029{
030    private SiteContextType _type;
031    // keep ids instead of real site objects and then re-resolve them to avoid RepositoryException about session closed
032    private List<String> _siteIds;
033    private SiteManager _siteManager;
034
035    /**
036     * Creates a SiteContext
037     * @param type the type
038     * @param sites the sites. Must be non-null if type is {@link SiteContextType#AMONG}, must be null otherwise.
039     * @param siteManager The site manager
040     */
041    public SiteContext(SiteContextType type, List<String> sites, SiteManager siteManager)
042    {
043        _type = type;
044        if (_type != SiteContextType.AMONG && sites != null)
045        {
046            throw new IllegalArgumentException("sites cannot be set with a SiteContextType not equals to " + SiteContextType.AMONG.toString());
047        }
048        else if (_type == SiteContextType.AMONG && sites == null)
049        {
050            throw new IllegalArgumentException("sites cannot be null with a SiteContextType equals to " + SiteContextType.AMONG.toString());
051        }
052        _siteIds = sites;
053        _siteManager = siteManager;
054    }
055    
056    /**
057     * Gets the type
058     * @return the type
059     */
060    public SiteContextType getType()
061    {
062        return _type;
063    }
064    
065    /**
066     * Gets the sites. Must be non-empty if {@link #getType()} returns {@link SiteContextType#AMONG}, must be empty otherwise.
067     * @return the sites
068     */
069    public Optional<List<Site>> getSites()
070    {
071        return Optional.ofNullable(_siteIds)
072                .map(siteIds -> siteIds.stream()
073                        .map(_siteManager::getSite)
074                        .collect(Collectors.toList())
075                );
076    }
077
078    @Override
079    public int hashCode()
080    {
081        final int prime = 31;
082        int result = 1;
083        result = prime * result + ((_siteIds == null) ? 0 : _siteIds.hashCode());
084        result = prime * result + ((_type == null) ? 0 : _type.hashCode());
085        return result;
086    }
087
088    @Override
089    public boolean equals(Object obj)
090    {
091        if (this == obj)
092        {
093            return true;
094        }
095        if (obj == null)
096        {
097            return false;
098        }
099        if (!(obj instanceof SiteContext))
100        {
101            return false;
102        }
103        SiteContext other = (SiteContext) obj;
104        if (_siteIds == null)
105        {
106            if (other._siteIds != null)
107            {
108                return false;
109            }
110        }
111        else if (!_siteIds.equals(other._siteIds))
112        {
113            return false;
114        }
115        if (_type != other._type)
116        {
117            return false;
118        }
119        return true;
120    }
121}