001/*
002 *  Copyright 2022 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 */
016
017package org.ametys.web.repository.page;
018
019import java.util.Map;
020
021import org.ametys.cms.data.holder.impl.DefaultModifiableIndexableDataHolder;
022import org.ametys.cms.repository.Content;
023import org.ametys.plugins.repository.AbstractAmetysObject;
024import org.ametys.plugins.repository.AmetysObject;
025import org.ametys.plugins.repository.AmetysRepositoryException;
026import org.ametys.plugins.repository.data.holder.ModelAwareDataHolder;
027import org.ametys.plugins.repository.data.holder.DataHolder;
028import org.ametys.plugins.repository.data.holder.ModifiableModelAwareDataHolder;
029import org.ametys.plugins.repository.data.holder.impl.DefaultDataHolder;
030import org.ametys.plugins.repository.data.repositorydata.RepositoryData;
031import org.ametys.plugins.repository.data.repositorydata.impl.MemoryRepositoryData;
032import org.ametys.web.data.type.ModelItemTypeExtensionPoint;
033import org.ametys.web.service.Service;
034
035/**
036 * A static zone item.
037 * Currently, the static zone item handle only service zone items
038 */
039public class StaticZoneItem extends AbstractAmetysObject implements ZoneItem
040{
041    /** The service */
042    protected Service _service;
043    
044    /** The service parameters */
045    protected Map<String, Object> _parameters;
046    
047    /** The zone type */
048    protected ZoneType _type;
049    
050    /** The zone item data type extension point */
051    protected ModelItemTypeExtensionPoint _zoneItemDataTypeExtensionPoint;
052    
053    /** The service parameters holder */
054    protected ModelAwareDataHolder _serviceParametersHolder;
055    
056    /**
057     * The constructor for a zone item containing a service
058     * @param service the service
059     * @param parameters the service parameters
060     * @param zoneItemDataTypeExtensionPoint the extension point with available data types for zone items
061     */
062    public StaticZoneItem(Service service, Map<String, Object> parameters, ModelItemTypeExtensionPoint zoneItemDataTypeExtensionPoint)
063    {
064        this._service = service;
065        this._parameters = parameters;
066        this._type = ZoneType.SERVICE;
067        this._zoneItemDataTypeExtensionPoint = zoneItemDataTypeExtensionPoint;
068        
069        ModifiableModelAwareDataHolder serviceParameters = (ModifiableModelAwareDataHolder) getServiceParameters();
070        for (String key : parameters.keySet())
071        {
072            serviceParameters.setValue(key, parameters.get(key));
073        }
074    }
075    
076    public ZoneType getType() throws AmetysRepositoryException
077    {
078        return this._type;
079    }
080
081    public <C extends Content> C getContent() throws AmetysRepositoryException
082    {
083        throw new UnsupportedOperationException("getContent method is not supported for static zone item");
084    }
085
086    public String getServiceId() throws AmetysRepositoryException
087    {
088        return _service.getId();
089    }
090
091    public String getId() throws AmetysRepositoryException
092    {
093        return "wrappedzoneitem://unused?serviceId=" + getServiceId();
094    }
095
096    public String getName() throws AmetysRepositoryException
097    {
098        return "default";
099    }
100
101    public Zone getZone()
102    {
103        throw new UnsupportedOperationException("getZone method is not supported for static zone item");
104    }
105
106    public String getPath() throws AmetysRepositoryException
107    {
108        throw new UnsupportedOperationException("getPath method is not supported for static zone item");
109    }
110
111    public <A extends AmetysObject> A getParent() throws AmetysRepositoryException
112    {
113        throw new UnsupportedOperationException("getParent method is not supported for static zone item");
114    }
115
116    public String getParentPath() throws AmetysRepositoryException
117    {
118        throw new UnsupportedOperationException("getParentPath method is not supported for static zone item");
119    }
120
121    @Override
122    protected DataHolder getDataHolder()
123    {
124        RepositoryData repositoryData = new MemoryRepositoryData(Zone.ZONEITEM_DATA_NAME);
125        return new DefaultDataHolder(this._zoneItemDataTypeExtensionPoint, repositoryData);
126    }
127
128    public ModelAwareDataHolder getServiceParameters() throws AmetysRepositoryException
129    {
130        if (this._serviceParametersHolder == null)
131        {
132            MemoryRepositoryData memoryData = new MemoryRepositoryData(this._service.getId());
133            this._serviceParametersHolder = new DefaultModifiableIndexableDataHolder(memoryData, _service);
134        }
135        
136        return this._serviceParametersHolder;
137    }
138
139    public ModelAwareDataHolder getZoneItemParametersHolder() throws AmetysRepositoryException
140    {
141        // Don't handle zone item view parameters for wrapped service
142        return null;
143    }
144
145    public ModelAwareDataHolder getContentViewParametersHolder(String contentViewName) throws AmetysRepositoryException
146    {
147        throw new UnsupportedOperationException("getContentViewParametersHolder method is not supported for static zone item");
148    }
149
150    public ModelAwareDataHolder getServiceViewParametersHolder(String serviceViewName) throws AmetysRepositoryException
151    {
152        // Don't handle view parameters for wrapped service
153        return null;
154    }
155}