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;
020
021import org.ametys.plugins.repository.AmetysObjectIterable;
022import org.ametys.plugins.repository.AmetysRepositoryException;
023import org.ametys.plugins.repository.CollectionIterable;
024import org.ametys.plugins.repository.data.holder.ModelAwareDataHolder;
025import org.ametys.plugins.repository.data.holder.ModelLessDataHolder;
026import org.ametys.plugins.repository.data.holder.impl.DefaultModelLessDataHolder;
027import org.ametys.plugins.repository.data.repositorydata.RepositoryData;
028import org.ametys.plugins.repository.data.repositorydata.impl.MemoryRepositoryData;
029import org.ametys.web.repository.page.Page;
030import org.ametys.web.repository.page.SitemapElement;
031import org.ametys.web.repository.page.Zone;
032import org.ametys.web.repository.page.ZoneItem;
033
034/**
035 * A zone which is configurable
036 */
037public class ConfigurableVirtualZone implements Zone 
038{
039    /** The Page */
040    protected AbstractConfigurableVirtualPage _page;
041    /** The VirtualPageZoneConfiguration */
042    protected VirtualZoneConfiguration _configuration;
043    /** The id */
044    protected String _id;
045    /** The scheme */
046    protected String _scheme;
047    /** The factory */
048    protected ConfigurableVirtualZoneFactory _factory;
049    
050    /**
051     * Constructor to create a configurable zone item
052     * @param page The abstract virtual configurable page
053     * @param configuration The virtual page's zone configuration
054     * @param scheme The scheme
055     * @param factory The zone's factory
056     */
057    public ConfigurableVirtualZone(AbstractConfigurableVirtualPage page, VirtualZoneConfiguration configuration, String scheme, ConfigurableVirtualZoneFactory factory)
058    {
059        _page = page;
060        _configuration = configuration;
061        _id = _configuration.getId();
062        _scheme = scheme;
063        _factory = factory;
064    }
065
066    @Override
067    public String getName() throws AmetysRepositoryException 
068    {
069        return _id;
070    }
071
072    @SuppressWarnings("unchecked")
073    @Override
074    public Page getParent() throws AmetysRepositoryException
075    {
076        return _page;
077    }
078    
079    @Override
080    public String getParentPath() throws AmetysRepositoryException 
081    {
082        return _page.getPath();
083    }
084
085    @Override
086    public SitemapElement getSitemapElement() 
087    {
088        return _page;
089    }
090
091    @Override
092    public String getPath() throws AmetysRepositoryException 
093    {
094        return _page.getPath() + "/ametys-internal:zones/" + this._id;
095    }
096
097    /**
098     * Get a zone item configuration by its name
099     * @param zoneItemId The zone item identifier
100     * @return La virtual page zone item configuration
101     */
102    public VirtualZoneItemConfiguration getZoneItemConfiguration(String zoneItemId) 
103    {
104        return _configuration.getZoneItemConfiguration(zoneItemId);
105    }
106
107    @Override
108    public ModelLessDataHolder getDataHolder() 
109    {
110        RepositoryData repositoryData = new MemoryRepositoryData(getName());
111        return new DefaultModelLessDataHolder(_factory.getZoneDataTypeExtensionPoint(), repositoryData);
112    }
113
114    @Override
115    public String getId() throws AmetysRepositoryException 
116    {
117        return _scheme + "://" + _id + "?pageId=" + _page.getId();
118    }
119    
120    @Override
121    public AmetysObjectIterable<? extends ZoneItem> getZoneItems() throws AmetysRepositoryException 
122    {
123        List<ZoneItem> zoneItems = new ArrayList<>();
124        
125        for (VirtualZoneItemConfiguration zoneItemConfiguration : _configuration.getZoneItemsConfigurations()) 
126        {
127            zoneItems.add(_factory.getZoneItemFactory().createZoneItem(_page, zoneItemConfiguration.getParentZoneName(), zoneItemConfiguration.getName()));
128        }
129        
130        return new CollectionIterable<>(zoneItems);
131    }
132
133    @Override
134    public ModelAwareDataHolder getZoneParametersHolder() throws AmetysRepositoryException 
135    {
136        // Parameters are not available on virtual zone
137        return null;
138    }
139}