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 */
016package org.ametys.plugins.userdirectory.page;
017
018import java.util.ArrayList;
019import java.util.List;
020import java.util.stream.Collectors;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025
026import org.ametys.cms.repository.Content;
027import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollectionDAO;
028import org.ametys.plugins.repository.AmetysObjectFactory;
029import org.ametys.plugins.repository.AmetysObjectIterable;
030import org.ametys.plugins.repository.AmetysObjectResolver;
031import org.ametys.plugins.repository.AmetysRepositoryException;
032import org.ametys.plugins.repository.CollectionIterable;
033import org.ametys.plugins.repository.UnknownAmetysObjectException;
034import org.ametys.plugins.repository.jcr.JCRAmetysObject;
035import org.ametys.plugins.repository.virtual.VirtualAmetysObjectFactory;
036import org.ametys.plugins.userdirectory.OrganisationChartPageHandler;
037import org.ametys.runtime.plugin.component.AbstractLogEnabled;
038import org.ametys.web.repository.page.Page;
039import org.ametys.web.repository.page.PageDataTypeExtensionPoint;
040import org.ametys.web.skin.SkinsManager;
041
042/**
043 * {@link AmetysObjectFactory} for handling "virtual" organisation chart root page
044 */
045public class VirtualOrganisationChartPageFactory extends AbstractLogEnabled implements VirtualAmetysObjectFactory<Page>, Serviceable
046{
047    private OrganisationChartPageHandler _organisationChartPageHandler;
048    private AmetysObjectResolver _resolver;
049    private SynchronizableContentsCollectionDAO _synchronizableContentsCollectionDAO;
050    private SkinsManager _skinManager;
051    private PageDataTypeExtensionPoint _pageDataTypeExtensionPoint;
052    
053
054    @Override
055    public void service(ServiceManager manager) throws ServiceException
056    {
057        _organisationChartPageHandler = (OrganisationChartPageHandler) manager.lookup(OrganisationChartPageHandler.ROLE);
058        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
059        _synchronizableContentsCollectionDAO = (SynchronizableContentsCollectionDAO) manager.lookup(SynchronizableContentsCollectionDAO.ROLE);
060        _skinManager = (SkinsManager) manager.lookup(SkinsManager.ROLE);
061        _pageDataTypeExtensionPoint = (PageDataTypeExtensionPoint) manager.lookup(PageDataTypeExtensionPoint.ROLE);
062    }
063
064    @Override
065    public Page getAmetysObjectById(String id) throws AmetysRepositoryException
066    {
067        // No page object linked to this factory
068        throw new UnsupportedOperationException();
069    }
070
071    @Override
072    public boolean hasAmetysObjectForId(String id) throws AmetysRepositoryException
073    {
074        // No page object linked to this factory
075        throw new UnsupportedOperationException();
076    }
077
078    @Override
079    public String getScheme()
080    {
081        return "ocroot";
082    }
083
084    @Override
085    public AmetysObjectIterable<Page> getChildren(JCRAmetysObject parent)
086    {
087        if (!(parent instanceof Page))
088        {
089            throw new IllegalArgumentException("The holder of the organisation chart virtual pages should be a page.");
090        }
091        
092        List<Page> children = new ArrayList<>();
093        Page rootPage = (Page) parent;
094        
095        AmetysObjectIterable<Content> contents = _organisationChartPageHandler.getContentsForRootPage(rootPage);
096        for (Content content : contents)
097        {
098            children.add(new OrgUnitPage(rootPage, content, content.getName(), _resolver, _organisationChartPageHandler, _synchronizableContentsCollectionDAO, _skinManager, _pageDataTypeExtensionPoint, _pageDataTypeExtensionPoint, _pageDataTypeExtensionPoint));
099        }
100        
101        return new CollectionIterable<>(children);
102    }
103
104    @Override
105    public Page getChild(JCRAmetysObject parent, String childName)
106    {
107        if (!(parent instanceof Page))
108        {
109            throw new IllegalArgumentException("The holder of the organisation chart virtual pages should be a page.");
110        }
111        
112        Page rootPage = (Page) parent;
113        AmetysObjectIterable<Content> contents = _organisationChartPageHandler.getContentsForRootPage(rootPage);
114        List<Content> contentFilters = contents.stream().filter(c -> c.getName().equals(childName)).collect(Collectors.toList());
115        
116        if (!contentFilters.isEmpty())
117        {
118            Content content = contentFilters.get(0);
119            return new OrgUnitPage(rootPage, content, content.getName(), _resolver, _organisationChartPageHandler, _synchronizableContentsCollectionDAO, _skinManager, _pageDataTypeExtensionPoint, _pageDataTypeExtensionPoint, _pageDataTypeExtensionPoint);
120        }
121        else
122        {
123            throw new UnknownAmetysObjectException("No orgUnit page named " + childName);
124        }
125    }
126
127    @Override
128    public boolean hasChild(JCRAmetysObject parent, String childName)
129    {
130        if (!(parent instanceof Page))
131        {
132            throw new IllegalArgumentException("The holder of the organisation chart virtual pages should be a page.");
133        }
134        
135        Page rootPage = (Page) parent;
136        AmetysObjectIterable<Content> contents = _organisationChartPageHandler.getContentsForRootPage(rootPage);
137        List<Content> contentFilters = contents.stream().filter(c -> c.getName().equals(childName)).collect(Collectors.toList());
138        
139        return !contentFilters.isEmpty();
140    }
141}