001/*
002 *  Copyright 2016 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.plugins.userdirectory.page;
018
019import org.ametys.cms.repository.Content;
020import org.ametys.plugins.repository.AmetysRepositoryException;
021import org.ametys.plugins.repository.data.holder.ModelAwareDataHolder;
022import org.ametys.plugins.repository.data.holder.ModelLessDataHolder;
023import org.ametys.plugins.repository.data.holder.impl.DefaultModelLessDataHolder;
024import org.ametys.plugins.repository.data.repositorydata.RepositoryData;
025import org.ametys.plugins.repository.data.repositorydata.impl.MemoryRepositoryData;
026import org.ametys.plugins.userdirectory.UserDirectoryPageHandler;
027import org.ametys.web.data.type.ModelItemTypeExtensionPoint;
028import org.ametys.web.repository.page.Page;
029import org.ametys.web.repository.page.Zone;
030import org.ametys.web.repository.page.ZoneItem;
031import org.ametys.web.repository.page.jcr.DefaultPage;
032
033/**
034 * {@link ZoneItem} holding a content.
035 */
036public class UserZoneItem implements ZoneItem
037{
038    private UserPage _page;
039    private ModelItemTypeExtensionPoint _zoneDataTypeExtensionPoint;
040    private ModelItemTypeExtensionPoint _zoneItemDataTypeExtensionPoint;
041    
042    /**
043     * Constructor.
044     * @param page the parent {@link UserPage}.
045     * @param zoneDataTypeExtensionPoint the extension point with available data types for zones
046     * @param zoneItemDataTypeExtensionPoint the extension point with available data types for zone items
047     */
048    public UserZoneItem(UserPage page, ModelItemTypeExtensionPoint zoneDataTypeExtensionPoint, ModelItemTypeExtensionPoint zoneItemDataTypeExtensionPoint)
049    {
050        _page = page;
051        _zoneDataTypeExtensionPoint = zoneDataTypeExtensionPoint;
052        _zoneItemDataTypeExtensionPoint = zoneItemDataTypeExtensionPoint;
053    }
054    
055    /**
056     * Compute the zone item id of a content for a page 
057     * @param pageId The page id
058     * @return The zoneitem id
059     */
060    public static String getZoneItemId(String pageId)
061    {
062        return "uduserzoneitem://unused?pageId=" + pageId;
063    }
064    
065    @SuppressWarnings("unchecked")
066    @Override
067    public Content getContent() throws AmetysRepositoryException
068    {
069        Content content = _page.getSyncContent();
070        return content;
071    }
072
073    public String getViewName() throws AmetysRepositoryException
074    {
075        Page rootPage = getRootPage();
076        return rootPage != null ? rootPage.getValue(UserDirectoryPageHandler.USER_VIEW_NAME, "main") : "main";
077    }
078    
079    Page getRootPage()
080    {
081        Page parent = _page.getParent();
082        while (parent != null)
083        {
084            if (parent instanceof DefaultPage)
085            {
086                return parent;
087            }
088            parent = parent.getParent();
089        }
090        return null;
091    }
092    
093    @Override
094    public String getServiceId() throws AmetysRepositoryException
095    {
096        throw new UnsupportedOperationException("getServiceId not supported on virtual user directory pages");
097    }
098
099    public ModelAwareDataHolder getServiceParameters() throws AmetysRepositoryException
100    {
101        throw new UnsupportedOperationException("getServiceParameters not supported on virtual user directory pages");
102    }
103
104    @Override
105    public ZoneType getType() throws AmetysRepositoryException
106    {
107        return ZoneType.CONTENT;
108    }
109
110    @Override
111    public Zone getZone()
112    {
113        return new UserZone(_page, _zoneDataTypeExtensionPoint, _zoneItemDataTypeExtensionPoint);
114    }
115
116    public ModelLessDataHolder getDataHolder()
117    {
118        RepositoryData repositoryData = new MemoryRepositoryData(Zone.ZONEITEM_DATA_NAME);
119        return new DefaultModelLessDataHolder(_zoneItemDataTypeExtensionPoint, repositoryData);
120    }
121
122    @Override
123    public String getId() throws AmetysRepositoryException
124    {
125        return getZoneItemId(_page.getId());
126    }
127
128    @Override
129    public String getName() throws AmetysRepositoryException
130    {
131        return "default";
132    }
133
134    @SuppressWarnings("unchecked")
135    @Override
136    public UserZone getParent() throws AmetysRepositoryException
137    {
138        return new UserZone(_page, _zoneDataTypeExtensionPoint, _zoneItemDataTypeExtensionPoint);
139    }
140
141    @Override
142    public String getParentPath() throws AmetysRepositoryException
143    {
144        return _page.getPath() + "/default";
145    }
146
147    @Override
148    public String getPath() throws AmetysRepositoryException
149    {
150        return _page.getPath() + "/default/default";
151    }
152
153    public ModelAwareDataHolder getZoneItemParametersHolder() throws AmetysRepositoryException
154    {
155        return null;
156    }
157
158    public ModelAwareDataHolder getContentViewParametersHolder(String contentViewName) throws AmetysRepositoryException
159    {
160        return null;
161    }
162
163    public ModelAwareDataHolder getServiceViewParametersHolder(String serviceViewName) throws AmetysRepositoryException
164    {
165        return null;
166    }
167}