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