001/* 002 * Copyright 2016 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.SortedSet; 020 021import org.apache.avalon.framework.service.ServiceException; 022import org.apache.avalon.framework.service.ServiceManager; 023import org.apache.avalon.framework.service.Serviceable; 024import org.apache.commons.lang.StringUtils; 025 026import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollectionDAO; 027import org.ametys.plugins.repository.AmetysObjectFactory; 028import org.ametys.plugins.repository.AmetysObjectResolver; 029import org.ametys.plugins.repository.AmetysRepositoryException; 030import org.ametys.plugins.repository.UnknownAmetysObjectException; 031import org.ametys.plugins.userdirectory.UserDirectoryPageHandler; 032import org.ametys.web.repository.page.Page; 033import org.ametys.web.skin.SkinsManager; 034 035/** 036 * {@link AmetysObjectFactory} handling {@link TransitionalPage}. 037 */ 038public class TransitionalPageFactory implements AmetysObjectFactory<TransitionalPage>, Serviceable 039{ 040 private AmetysObjectResolver _resolver; 041 private UserDirectoryPageHandler _userDirectoryPageHandler; 042 private SynchronizableContentsCollectionDAO _synchronizableContentsCollectionDAO; 043 private SkinsManager _skinManager; 044 045 @Override 046 public void service(ServiceManager manager) throws ServiceException 047 { 048 _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 049 _userDirectoryPageHandler = (UserDirectoryPageHandler) manager.lookup(UserDirectoryPageHandler.ROLE); 050 _synchronizableContentsCollectionDAO = (SynchronizableContentsCollectionDAO) manager.lookup(SynchronizableContentsCollectionDAO.ROLE); 051 _skinManager = (SkinsManager) manager.lookup(SkinsManager.ROLE); 052 } 053 054 public TransitionalPage getAmetysObjectById(String id) throws AmetysRepositoryException 055 { 056 // E.g: udtransitional://path?rootId=... 057 String path = StringUtils.substringBefore(StringUtils.substringAfter(id, "udtransitional://"), "?rootId="); 058 059 String rootId = StringUtils.substringAfter(id, "?rootId="); 060 Page root = _resolver.resolveById(rootId); 061 062 String pathName = path; 063 String parentPath = "_root"; 064 if (StringUtils.contains(path, "/")) 065 { 066 pathName = StringUtils.substringAfterLast(path, "/"); 067 parentPath = StringUtils.substringBeforeLast(path, "/"); 068 } 069 070 String name = _userDirectoryPageHandler.getName(pathName); 071 072 SortedSet<String> transitionalPagesName = _userDirectoryPageHandler.getTransitionalPagesName(root, _userDirectoryPageHandler.getName(parentPath)); 073 if (transitionalPagesName.contains(name)) 074 { 075 return new TransitionalPage(root, name, path, _resolver, _userDirectoryPageHandler, _synchronizableContentsCollectionDAO, _skinManager); 076 } 077 else 078 { 079 throw new UnknownAmetysObjectException("No transitional page named " + name + " (full page path " + path + ")."); 080 } 081 } 082 083 public boolean hasAmetysObjectForId(String id) throws AmetysRepositoryException 084 { 085 try 086 { 087 // E.g: udtransitional://path?rootId=... 088 String path = StringUtils.substringBefore(StringUtils.substringAfter(id, "udtransitional://"), "?rootId="); 089 090 String rootId = StringUtils.substringAfter(id, "?rootId="); 091 Page root = _resolver.resolveById(rootId); 092 093 String pathName = path; 094 String parentPath = "_root"; 095 if (StringUtils.contains(path, "/")) 096 { 097 pathName = StringUtils.substringAfterLast(path, "/"); 098 parentPath = StringUtils.substringBeforeLast(path, "/"); 099 } 100 101 String name = _userDirectoryPageHandler.getName(pathName); 102 103 SortedSet<String> transitionalPagesName = _userDirectoryPageHandler.getTransitionalPagesName(root, _userDirectoryPageHandler.getName(parentPath)); 104 return transitionalPagesName.contains(name); 105 } 106 catch (UnknownAmetysObjectException e) 107 { 108 return false; 109 } 110 111 } 112 113 public String getScheme() 114 { 115 return "udtransitional"; 116 } 117 118}