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