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