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