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 org.apache.avalon.framework.configuration.Configuration;
019import org.apache.avalon.framework.configuration.ConfigurationException;
020
021import org.ametys.web.repository.page.ZoneItem.ZoneType;
022
023/**
024 * Virtual page zone's zone item configuration
025 */
026public class VirtualZoneItemConfiguration 
027{
028    private ZoneType _zoneType;
029    private String _name;
030    private String _serviceId;
031    private String _view;
032    private VirtualZoneConfiguration _parentZoneConfiguration;
033    private Configuration _configuration;
034
035    /**
036     * The constructor of virtual page's zone item configuration
037     * @param configuration The configuration
038     * @param parentZoneConfiguration The parent's configuration
039     * @throws ConfigurationException If an error occurs
040     */
041    public VirtualZoneItemConfiguration (Configuration configuration, VirtualZoneConfiguration parentZoneConfiguration) throws ConfigurationException 
042    {
043        String zoneItemType = configuration.getName();
044        if ("service".equals(zoneItemType))
045        {
046            _zoneType = ZoneType.SERVICE;
047            _serviceId = configuration.getAttribute("id");
048            _name = _serviceId;
049            
050            // Ensure name uniqueness if the same service is used twice in the zone 
051            int count = 2;
052            while (parentZoneConfiguration.getZoneItemConfiguration(_name) != null)
053            {
054                _name = _serviceId + "-" + count;
055                count++;
056            }
057        }
058        else if ("content".equals(zoneItemType))
059        {
060            _zoneType = ZoneType.CONTENT;
061            _name = "defaultContent";
062        }
063        else
064        {
065            throw new ConfigurationException("The zone item type '" + zoneItemType + "i does not exist");
066        }
067        
068        _view = configuration.getAttribute("view", "main");
069        _parentZoneConfiguration = parentZoneConfiguration;
070        
071        _configuration = configuration;
072    }
073
074    /**
075     * Get the type of zone item
076     * @return The <code>ZoneType</code> of the zone item
077     */
078    public ZoneType getZoneType() 
079    {
080        return _zoneType;
081    }
082
083    /**
084     * Get the name of the zone item
085     * @return The name of the zone item
086     */
087    public String getName() 
088    {
089        return _name;
090    }
091
092    /**
093     * Get the service id
094     * @return The id of the service
095     */
096    public String getServiceId() 
097    {
098        return _serviceId;
099    }
100
101    /**
102     * Get the view
103     * @return The view
104     */
105    public String getView() 
106    {
107        return _view;
108    }
109
110    /**
111     * Get the service parameters configurations of the zone item
112     * @return a list of <code>Configuration</code> for the services paramters
113     */
114    public Configuration getConfiguration() 
115    {
116        return _configuration;
117    }
118    
119    /**
120     * Get the configuration of the zone parent
121     * @return The <code>VirtualPageZoneConfiguration</code> of the zone.
122     */
123    public VirtualZoneConfiguration getParentZoneConfiguration() 
124    {
125        return _parentZoneConfiguration;
126    }
127
128    /**
129     * Get the name of the parent zone.
130     * @return The name of the parent zone
131     */
132    public String getParentZoneName() 
133    {
134        return _parentZoneConfiguration.getId();
135    }
136}