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.commons.lang.StringUtils;
022
023import org.ametys.plugins.repository.AmetysObjectFactory;
024import org.ametys.plugins.repository.AmetysRepositoryException;
025import org.ametys.plugins.repository.UnknownAmetysObjectException;
026import org.ametys.web.repository.page.Page;
027
028/**
029 * {@link AmetysObjectFactory} handling {@link TransitionalPage}.
030 */
031public class TransitionalPageFactory extends AbstractUserDirectoryPageFactory implements AmetysObjectFactory<TransitionalPage>
032{
033    /**
034     * Create a transitional page.
035     * @param root the user directory root page.
036     * @param prefix the page's title.
037     * @param path the path
038     * @return The TransitionalPage created
039     */
040    public TransitionalPage createTransitionalPage(Page root, String prefix, String path)
041    {
042        return new TransitionalPage(root, getConfiguration(), getScheme(), this, prefix, path);
043    }
044    
045    public TransitionalPage getAmetysObjectById(String id) throws AmetysRepositoryException
046    {
047        // E.g: udtransitional://path?rootId=...
048        String path = StringUtils.substringBefore(StringUtils.substringAfter(id, "udtransitional://"), "?rootId=");
049        
050        String rootId = StringUtils.substringAfter(id, "?rootId=");
051        Page root = _resolver.resolveById(rootId);
052        
053        String pathName = path;
054        String parentPath = "_root";
055        if (StringUtils.contains(path, "/"))
056        {
057            pathName = StringUtils.substringAfterLast(path, "/");
058            parentPath = StringUtils.substringBeforeLast(path, "/");
059        }
060        
061        String name = _userDirectoryPageHandler.getName(pathName);
062        
063        SortedSet<String> transitionalPagesName = _userDirectoryPageHandler.getTransitionalPagesName(root, _userDirectoryPageHandler.getName(parentPath));
064        if (transitionalPagesName.contains(name))
065        {
066            return createTransitionalPage(root, name, path);
067        }
068        else
069        {
070            throw new UnknownAmetysObjectException("No transitional page named " + name + " (full page path " + path + ").");
071        }
072    }
073
074    public boolean hasAmetysObjectForId(String id) throws AmetysRepositoryException
075    {
076        try
077        {
078            // E.g: udtransitional://path?rootId=...
079            String path = StringUtils.substringBefore(StringUtils.substringAfter(id, "udtransitional://"), "?rootId=");
080            
081            String rootId = StringUtils.substringAfter(id, "?rootId=");
082            Page root = _resolver.resolveById(rootId);
083            
084            String pathName = path;
085            String parentPath = "_root";
086            if (StringUtils.contains(path, "/"))
087            {
088                pathName = StringUtils.substringAfterLast(path, "/");
089                parentPath = StringUtils.substringBeforeLast(path, "/");
090            }
091            
092            String name = _userDirectoryPageHandler.getName(pathName);
093            
094            SortedSet<String> transitionalPagesName = _userDirectoryPageHandler.getTransitionalPagesName(root, _userDirectoryPageHandler.getName(parentPath));
095            return transitionalPagesName.contains(name);
096        }
097        catch (UnknownAmetysObjectException e)
098        {
099            return false;
100        }
101        
102    }
103
104    public String getScheme()
105    {
106        return "udtransitional";
107    }
108}