001/*
002 *  Copyright 2010 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 */
016package org.ametys.web.alias;
017
018import java.util.Date;
019
020import org.apache.commons.lang.time.FastDateFormat;
021
022import org.ametys.plugins.repository.AmetysRepositoryException;
023import org.ametys.plugins.repository.ModifiableTraversableAmetysObject;
024import org.ametys.plugins.repository.TraversableAmetysObject;
025import org.ametys.web.repository.site.Site;
026
027/**
028 * Helper for alias
029 *
030 */
031public final class AliasHelper
032{
033    /** The alias default name */
034    public static final String DEFAULT_ALIAS_NAME = "alias";
035    
036    private AliasHelper()
037    {
038        // nothing
039    }
040    
041    /**
042     * Get the next unique name for alias
043     * @param rootNode The alias root node
044     * @return the next unique name
045     */
046    public static String getAliasNextUniqueName (TraversableAmetysObject rootNode)
047    {
048        // Find unique name
049        String defaultName = DEFAULT_ALIAS_NAME;
050        String uniqueName = defaultName;
051        int index = 2;
052        while (rootNode.hasChild(uniqueName))
053        {
054            uniqueName = defaultName + "-" + index;
055            index++;
056        }
057        
058        return uniqueName;
059    }
060    
061    /**
062     * Get the alias root node for the given site
063     * @param site The site
064     * @return The alias root node
065     * @throws AmetysRepositoryException If an error occurs.
066     */
067    public static ModifiableTraversableAmetysObject getRootNode (Site site) throws AmetysRepositoryException
068    {
069        ModifiableTraversableAmetysObject pluginsNode = site.getRootPlugins();
070        
071        ModifiableTraversableAmetysObject webNode = null;
072        if (!pluginsNode.hasChild("web"))
073        {
074            webNode = (ModifiableTraversableAmetysObject) pluginsNode.createChild("web", "ametys:unstructured");
075        }
076        else
077        {
078            webNode = pluginsNode.getChild("web");
079        }
080        
081        ModifiableTraversableAmetysObject aliasNode = null;
082        if (!webNode.hasChild("pageAlias"))
083        {
084            aliasNode = (ModifiableTraversableAmetysObject) webNode.createChild("pageAlias", "ametys:pageAlias");
085        }
086        else
087        {
088            aliasNode = webNode.getChild("pageAlias");
089        }
090        
091        if (pluginsNode.needsSave())
092        {
093            pluginsNode.saveChanges();
094        }
095        
096        return aliasNode;
097        
098    }
099    
100    /**
101     * Creates the XPath query
102     * @param siteName The site name
103     * @param date The expiration date
104     * @return the XPath query
105     */
106    public static String getXPath (String siteName, Date date)
107    {
108        return "//element(" + siteName + ", ametys:site)/ametys-internal:plugins/web/pageAlias/element(*, ametys:alias)[not(@ametys-internal:expirationDate) or @ametys-internal:expirationDate >= xs:dateTime('" + FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.SSSZZ").format(date) + "')]";
109    }
110    
111    /**
112     * Creates the XPath query corresponding to specified alias url
113     * @param siteName The site name
114     * @param url The url
115     * @return the XPath query
116     */
117    public static String getXPath (String siteName, String url)
118    {
119        return "//element(" + siteName + ", ametys:site)/ametys-internal:plugins/web/pageAlias/element(*, ametys:alias)[@ametys-internal:url='" + url + "']";
120    }
121}