001/*
002 *  Copyright 2023 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.repository.page.virtual;
017
018import java.util.ArrayList;
019import java.util.List;
020import java.util.Set;
021
022import org.apache.commons.lang3.StringUtils;
023
024import org.ametys.cms.repository.Content;
025import org.ametys.plugins.explorer.resources.ResourceCollection;
026import org.ametys.plugins.repository.AmetysObjectIterable;
027import org.ametys.plugins.repository.AmetysRepositoryException;
028import org.ametys.plugins.repository.CollectionIterable;
029import org.ametys.plugins.repository.data.holder.ModelAwareDataHolder;
030import org.ametys.web.repository.page.Page;
031import org.ametys.web.repository.page.UnknownZoneException;
032import org.ametys.web.repository.page.Zone;
033import org.ametys.web.repository.site.Site;
034import org.ametys.web.repository.sitemap.Sitemap;
035import org.ametys.web.skin.Skin;
036
037/**
038 * An abstract configurable virtual page.
039 * @param <T> The type of page factory to use
040 */
041public abstract class AbstractConfigurableVirtualPage<T extends AbstractConfigurableVirtualPageFactory> implements Page 
042{
043    /** The root page */
044    protected Page _root;
045    /** The AbstractVirtualPageConfiguration */
046    protected VirtualPageConfiguration _configuration;
047    /** The ModelItemTypeExtensionPoint for zones */
048    /** The page's scheme */
049    protected String _scheme;
050    /** The factory */
051    protected T _factory;
052    
053    /**
054     * The constructor for an abstract configurable virtual page
055     * @param root The root page
056     * @param configuration The abstract virtual page configuration
057     * @param scheme The page's scheme
058     * @param factory The page's factory
059     */
060    public AbstractConfigurableVirtualPage(Page root, VirtualPageConfiguration configuration, String scheme, T factory)
061    {
062        _root = root;
063        _configuration = configuration;
064        _factory = factory;
065        _scheme = scheme;
066    }
067    
068    @Override
069    public ResourceCollection getRootAttachments() throws AmetysRepositoryException
070    {
071        // Attachments not available on virtual pages
072        return null;
073    }
074    /**
075     * Get a zone configuration by its name
076     * @param name The zone name
077     * @return the VirtualPageZoneConfiguration
078     */
079    public VirtualZoneConfiguration getZoneConfiguration(String name) 
080    {
081        return _configuration.getZoneConfiguration(name, _root);
082    }
083    
084    @Override
085    public String getTemplate() throws AmetysRepositoryException 
086    {
087        Skin skin = _factory.getSkinsManager().getSkin(getSite().getSkinId());
088        
089        String template = _configuration.getTemplate(_root);
090        
091        if (StringUtils.isNotEmpty(template) && skin.getTemplate(template) != null)
092        {
093            return template;
094        }
095        else
096        {
097            return "page"; 
098        }
099    }
100
101    @Override
102    public boolean hasZone(String name) throws AmetysRepositoryException 
103    {
104        return _configuration.hasZoneConfiguration(name, _root);
105    }
106
107    @Override
108    public PageType getType() throws AmetysRepositoryException 
109    {
110        return PageType.CONTAINER;
111    }
112
113    @Override
114    public String getURL() throws AmetysRepositoryException 
115    {
116        throw new UnsupportedOperationException("getURL not supported on virtual pages");
117    }
118
119    @Override
120    public LinkType getURLType() throws AmetysRepositoryException 
121    {
122        throw new UnsupportedOperationException("getURLType not supported on virtual pages");
123    }
124
125    @Override
126    public Set<String> getReferers() throws AmetysRepositoryException 
127    {
128        throw new UnsupportedOperationException("getReferers not supported on virtual pages");
129    }
130
131    @Override
132    public Site getSite() throws AmetysRepositoryException
133    {
134        return _root.getSite();
135    }
136    
137    @Override
138    public String getSiteName() throws AmetysRepositoryException
139    {
140        return _root.getSiteName();
141    }
142
143    @Override
144    public Sitemap getSitemap() throws AmetysRepositoryException
145    {
146        return _root.getSitemap();
147    }
148
149    @Override
150    public String getSitemapName() throws AmetysRepositoryException
151    {
152        return _root.getSitemapName();
153    }
154    
155    @Override
156    public String getPath() throws AmetysRepositoryException
157    {
158        return getParentPath() + "/" + getName();
159    }
160    
161    @Override
162    public Set<String> getTags() throws AmetysRepositoryException
163    {
164        return Set.of();
165    }
166    
167    @Override
168    public boolean isVisible() throws AmetysRepositoryException 
169    {
170        return true;
171    }
172
173    /**
174     * Get the page's content
175     * @param <C> The content's type
176     * @return The content
177     */
178    public <C extends Content> C getContent() 
179    {
180        throw new UnsupportedOperationException("This virtual page does not have a content and the overriding configuration requires one.");
181    }
182    
183    @Override
184    public Zone getZone(String name) throws UnknownZoneException, AmetysRepositoryException
185    {
186        return _factory.getZoneFactory().createZone(this, name);
187    }
188
189    @Override
190    public AmetysObjectIterable< ? extends Zone> getZones() throws AmetysRepositoryException
191    {
192        List<Zone> zones = new ArrayList<>();
193        
194        for (VirtualZoneConfiguration zoneConfiguration : _configuration.getZonesConfigurations(_root)) 
195        {
196            zones.add(getZone(zoneConfiguration.getId()));
197        }
198        
199        return new CollectionIterable<>(zones);
200    }
201    
202    @Override
203    public ModelAwareDataHolder getTemplateParametersHolder() throws AmetysRepositoryException
204    {
205        // Template's parameters not available on virtual pages
206        return null;
207    }
208    
209    /**
210     * Get the page's scheme
211     * @return The scheme of the page
212     */
213    public String getScheme() 
214    {
215        return _scheme;
216    }
217}