001/*
002 *  Copyright 2013 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.plugins.cart;
017
018import java.util.Set;
019
020import org.ametys.core.group.GroupIdentity;
021import org.ametys.core.user.UserIdentity;
022import org.ametys.plugins.repository.AmetysObjectResolver;
023import org.ametys.plugins.repository.AmetysRepositoryException;
024import org.ametys.plugins.repository.ModifiableTraversableAmetysObject;
025import org.ametys.plugins.repository.RepositoryConstants;
026
027/**
028 * Helper for {@link Cart}
029 *
030 */
031public final class CartHelper
032{
033    private static final String __PLUGIN_NODE_NAME = "cart";
034    private static final String __ROOT_CART_NODETYPE = RepositoryConstants.NAMESPACE_PREFIX + ":carts";
035    
036    private CartHelper()
037    {
038        // Private
039    }
040    
041    /**
042     * Get the root plugin storage object.
043     * @param resolver The Ametys object resolver
044     * @return the root plugin storage object.
045     * @throws AmetysRepositoryException if a repository error occurs.
046     */
047    public static ModifiableTraversableAmetysObject getCartsNode(AmetysObjectResolver resolver) throws AmetysRepositoryException
048    {
049        try
050        {
051            ModifiableTraversableAmetysObject pluginsNode = resolver.resolveByPath("/ametys:plugins");
052            
053            ModifiableTraversableAmetysObject pluginNode = getOrCreateNode(pluginsNode, __PLUGIN_NODE_NAME, "ametys:unstructured");
054            
055            return getOrCreateNode(pluginNode, "ametys:carts", __ROOT_CART_NODETYPE);
056        }
057        catch (AmetysRepositoryException e)
058        {
059            throw new AmetysRepositoryException("Unable to get the cart root node", e);
060        }
061    }
062    
063    private static ModifiableTraversableAmetysObject getOrCreateNode(ModifiableTraversableAmetysObject parentNode, String nodeName, String nodeType) throws AmetysRepositoryException
064    {
065        ModifiableTraversableAmetysObject definitionsNode;
066        if (parentNode.hasChild(nodeName))
067        {
068            definitionsNode = parentNode.getChild(nodeName);
069        }
070        else
071        {
072            definitionsNode = parentNode.createChild(nodeName, nodeType);
073            parentNode.saveChanges();
074        }
075        return definitionsNode;
076    }
077    
078    /**
079     * Creates the XPath query to get all carts in READ access
080     * @param user The user
081     * @param groups The user's groups
082     * @return The XPath query
083     */
084    public static String getXPathQueryForReadAccess (UserIdentity user, Set<GroupIdentity> groups)
085    {
086        String login = user.getLogin();
087        String populationId = user.getPopulationId();
088        
089        String publicPredicat = "(@ametys-internal:visibility = 'PUBLIC')";
090        String privatePredicat = String.format("(@ametys-internal:visibility='PRIVATE' and @ametys-internal:author/ametys:login='%s' and @ametys-internal:author/ametys:population='%s')", login, populationId);
091        
092        String groupCondition = "";
093        for (GroupIdentity group : groups)
094        {
095            groupCondition += String.format(" or (ametys-internal:read-access/ametys-internal:groups/ametys:groupId='%s' and ametys-internal:read-access/ametys-internal:groups/ametys:groupDirectory='%s')", group.getId(), group.getDirectoryId());
096            groupCondition += String.format(" or (ametys-internal:write-access/ametys-internal:groups/ametys:groupId='%s' and ametys-internal:write-access/ametys-internal:groups/ametys:groupDirectory='%s')", group.getId(), group.getDirectoryId());
097        }
098        String sharedPredicat = "(@ametys-internal:visibility='SHARED' and ";
099        sharedPredicat += String.format("((@ametys-internal:author/ametys:login='%s' and @ametys-internal:author/ametys:population='%s') or ", login, populationId);
100        sharedPredicat += String.format("(ametys-internal:read-access/ametys-internal:users/ametys:login='%s' and ametys-internal:read-access/ametys-internal:users/ametys:population='%s') or ", login, populationId);
101        sharedPredicat += String.format("(ametys-internal:write-access/ametys-internal:users/ametys:login='%s' and ametys-internal:write-access/ametys-internal:users/ametys:population='%s')", login, populationId);
102        sharedPredicat += groupCondition + "))";
103        
104        return "//element(*, ametys:cart)[" + publicPredicat + " or " + privatePredicat + " or " + sharedPredicat + "]";
105    }
106    
107    /**
108     * Creates the XPath query to get all carts in WRITE access
109     * @param user The user
110     * @param groups The user's groups
111     * @return The XPath query
112     */
113    public static String getXPathQueryForWriteAccess (UserIdentity user, Set<GroupIdentity> groups)
114    {
115        String login = user.getLogin();
116        String populationId = user.getPopulationId();
117        
118        String publicPredicat = "(@ametys-internal:visibility = 'PUBLIC')";
119        String privatePredicat = String.format("(@ametys-internal:visibility='PRIVATE' and @ametys-internal:author/ametys:login='%s' and @ametys-internal:author/ametys:population='%s')", login, populationId);
120        
121        String groupCondition = "";
122        for (GroupIdentity group : groups)
123        {
124            groupCondition += String.format(" or (ametys-internal:write-access/ametys-internal:groups/ametys:groupId='%s' and ametys-internal:write-access/ametys-internal:groups/ametys:groupDirectory='%s')", group.getId(), group.getDirectoryId());
125        }
126        String sharedPredicat = "(@ametys-internal:visibility='SHARED' and ";
127        sharedPredicat += String.format("((@ametys-internal:author/ametys:login='%s' and @ametys-internal:author/ametys:population='%s') or ", login, populationId);
128        sharedPredicat += String.format("(ametys-internal:write-access/ametys-internal:users/ametys:login='%s' and ametys-internal:write-access/ametys-internal:users/ametys:population='%s')", login, populationId);
129        sharedPredicat += groupCondition + "))";
130        
131        return "//element(*, ametys:cart)[" + publicPredicat + " or " + privatePredicat + " or " + sharedPredicat + "]";
132    }
133}