001/*
002 *  Copyright 2017 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 java.util.ArrayList;
020
021import org.ametys.plugins.repository.AmetysObjectIterable;
022import org.ametys.plugins.repository.AmetysRepositoryException;
023import org.ametys.plugins.repository.CollectionIterable;
024import org.ametys.plugins.repository.data.holder.ModelLessDataHolder;
025import org.ametys.plugins.repository.data.holder.impl.DefaultModelLessDataHolder;
026import org.ametys.plugins.repository.data.repositorydata.RepositoryData;
027import org.ametys.plugins.repository.data.repositorydata.impl.MemoryRepositoryData;
028import org.ametys.plugins.repository.data.type.RepositoryModelItemType;
029import org.ametys.runtime.plugin.component.AbstractThreadSafeComponentExtensionPoint;
030import org.ametys.web.repository.page.Page;
031import org.ametys.web.repository.page.Zone;
032import org.ametys.web.repository.page.ZoneItem;
033
034/**
035 * {@link Zone} holding a content.
036 */
037public class OrgUnitZone implements Zone
038{
039    private OrgUnitPage _page;
040    private AbstractThreadSafeComponentExtensionPoint<RepositoryModelItemType> _zoneDataTypeExtensionPoint;
041    private AbstractThreadSafeComponentExtensionPoint<RepositoryModelItemType> _zoneItemDataTypeExtensionPoint;
042    
043    /**
044     * Constructor
045     * @param page the parent {@link OrgUnitPage}.
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 OrgUnitZone(OrgUnitPage page, AbstractThreadSafeComponentExtensionPoint<RepositoryModelItemType> zoneDataTypeExtensionPoint, AbstractThreadSafeComponentExtensionPoint<RepositoryModelItemType> zoneItemDataTypeExtensionPoint)
050    {
051        _page = page;
052        _zoneDataTypeExtensionPoint = zoneDataTypeExtensionPoint;
053        _zoneItemDataTypeExtensionPoint = zoneItemDataTypeExtensionPoint;
054    }
055    
056    @Override
057    public Page getPage()
058    {
059        return _page;
060    }
061
062    @Override
063    public AmetysObjectIterable<? extends ZoneItem> getZoneItems() throws AmetysRepositoryException
064    {
065        ArrayList<ZoneItem> zoneItems = new ArrayList<>();
066        zoneItems.add(new OrgUnitZoneItem(_page, _zoneDataTypeExtensionPoint, _zoneItemDataTypeExtensionPoint));
067        return new CollectionIterable<>(zoneItems);
068    }
069
070    public ModelLessDataHolder getDataHolder()
071    {
072        RepositoryData repositoryData = new MemoryRepositoryData(getName());
073        return new DefaultModelLessDataHolder(_zoneDataTypeExtensionPoint, repositoryData);
074    }
075
076    @Override
077    public String getId() throws AmetysRepositoryException
078    {
079        return "udorgunitzone://unused?pageId=" + _page.getId();
080    }
081
082    @Override
083    public String getName() throws AmetysRepositoryException
084    {
085        return "default";
086    }
087
088    @SuppressWarnings("unchecked")
089    @Override
090    public Page getParent() throws AmetysRepositoryException
091    {
092        return _page;
093    }
094
095    @Override
096    public String getParentPath() throws AmetysRepositoryException
097    {
098        return _page.getPath();
099    }
100
101    @Override
102    public String getPath() throws AmetysRepositoryException
103    {
104        return _page.getPath() + "/default";
105    }
106}