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