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