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