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