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