001/*
002 *  Copyright 2015 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.actions;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022import java.util.Set;
023
024import org.apache.avalon.framework.parameters.Parameters;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.cocoon.acting.ServiceableAction;
028import org.apache.cocoon.environment.ObjectModelHelper;
029import org.apache.cocoon.environment.Redirector;
030import org.apache.cocoon.environment.Request;
031import org.apache.cocoon.environment.SourceResolver;
032import org.apache.commons.lang3.StringUtils;
033
034import org.ametys.core.cocoon.JSonReader;
035import org.ametys.core.group.GroupIdentity;
036import org.ametys.core.group.GroupManager;
037import org.ametys.core.user.CurrentUserProvider;
038import org.ametys.core.user.UserIdentity;
039import org.ametys.plugins.cart.Cart;
040import org.ametys.plugins.cart.Cart.CartProfile;
041import org.ametys.plugins.cart.CartHelper;
042import org.ametys.plugins.cart.CartsDAO;
043import org.ametys.plugins.repository.AmetysObjectIterable;
044import org.ametys.plugins.repository.AmetysObjectResolver;
045
046/**
047 * SAX carts
048 *
049 */
050public class GetCartsAction extends ServiceableAction
051{
052    private CurrentUserProvider _userProvider;
053    private AmetysObjectResolver _resolver;
054    private GroupManager _groupManager;
055    private CartsDAO _cartDAO;
056    
057    @Override
058    public void service(ServiceManager serviceManager) throws ServiceException
059    {
060        super.service(serviceManager);
061        _cartDAO = (CartsDAO) serviceManager.lookup(CartsDAO.ROLE);
062        _userProvider = (CurrentUserProvider) serviceManager.lookup(CurrentUserProvider.ROLE);
063        _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE);
064        _groupManager = (GroupManager) serviceManager.lookup(GroupManager.ROLE);
065    }
066    
067    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
068    {
069        @SuppressWarnings("unchecked")
070        Map jsParameters = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT);
071        
072        String profileId = StringUtils.defaultIfEmpty((String) jsParameters.get("profile"), "read_access");
073        
074        List<Map<String, Object>> nodes = new ArrayList<>();
075        
076        if (!"read_access".equals(profileId) && !"write_access".equals(profileId))
077        {
078            throw new IllegalArgumentException("Unexpected profile identifier : " + profileId);
079        }
080        
081        CartProfile profile = CartProfile.valueOf(profileId.toUpperCase());
082        
083        UserIdentity identity = _userProvider.getUser();
084        Set<GroupIdentity> userGroups = _groupManager.getUserGroups(identity);
085        
086        String xPathQuery = CartProfile.READ_ACCESS.equals(profile) ? CartHelper.getXPathQueryForReadAccess(identity, userGroups) : CartHelper.getXPathQueryForWriteAccess(identity, userGroups);
087        AmetysObjectIterable<Cart> carts = _resolver.query(xPathQuery);
088        
089        for (Cart cart : carts)
090        {
091            nodes.add(_cartDAO.getCartProperties(cart));
092        }
093        
094        Map<String, Object> result = new HashMap<>();
095        result.put("carts", nodes);
096        
097        Request request = ObjectModelHelper.getRequest(objectModel);
098        request.setAttribute(JSonReader.OBJECT_TO_READ, result);
099        
100        return EMPTY_MAP;
101    }
102}