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.function.Predicate;
023import java.util.stream.Collectors;
024
025import org.apache.avalon.framework.parameters.Parameters;
026import org.apache.avalon.framework.service.ServiceException;
027import org.apache.avalon.framework.service.ServiceManager;
028import org.apache.cocoon.acting.ServiceableAction;
029import org.apache.cocoon.environment.ObjectModelHelper;
030import org.apache.cocoon.environment.Redirector;
031import org.apache.cocoon.environment.Request;
032import org.apache.cocoon.environment.SourceResolver;
033import org.apache.commons.lang3.StringUtils;
034
035import org.ametys.core.cocoon.JSonReader;
036import org.ametys.core.user.CurrentUserProvider;
037import org.ametys.core.user.UserIdentity;
038import org.ametys.plugins.cart.Cart;
039import org.ametys.plugins.cart.Cart.CartProfile;
040import org.ametys.plugins.cart.CartsDAO;
041import org.ametys.plugins.repository.AmetysObjectResolver;
042
043/**
044 * SAX carts
045 *
046 */
047public class GetCartsAction extends ServiceableAction
048{
049    private CurrentUserProvider _userProvider;
050    private AmetysObjectResolver _resolver;
051    private CartsDAO _cartDAO;
052    
053    @Override
054    public void service(ServiceManager serviceManager) throws ServiceException
055    {
056        super.service(serviceManager);
057        _cartDAO = (CartsDAO) serviceManager.lookup(CartsDAO.ROLE);
058        _userProvider = (CurrentUserProvider) serviceManager.lookup(CurrentUserProvider.ROLE);
059        _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE);
060    }
061    
062    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
063    {
064        @SuppressWarnings("unchecked")
065        Map jsParameters = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT);
066        
067        String profileId = StringUtils.defaultIfEmpty((String) jsParameters.get("profile"), "read_access");
068        
069        List<Map<String, Object>> nodes = new ArrayList<>();
070        
071        if (!"read_access".equals(profileId) && !"write_access".equals(profileId) && !"right_access".equals(profileId))
072        {
073            throw new IllegalArgumentException("Unexpected profile identifier : " + profileId);
074        }
075        CartProfile profile = CartProfile.valueOf(profileId.toUpperCase());
076
077        UserIdentity identity = _userProvider.getUser();
078        Predicate<Cart> filterCarts;
079        switch (profile)
080        {
081            case WRITE_ACCESS:
082                filterCarts = cart -> _cartDAO.canWrite(identity, cart);
083                break;
084            case RIGHT_ACCESS:
085                filterCarts = cart -> _cartDAO.canAssignRights(identity, cart);
086                break;
087            case READ_ACCESS:
088            default:
089                filterCarts = cart -> _cartDAO.canRead(identity, cart);
090                break;
091        }
092
093        nodes = _resolver.query("//element(*, ametys:cart)")
094                        .stream()
095                        .filter(Cart.class::isInstance)
096                        .map(obj -> (Cart) obj)
097                        .filter(filterCarts)
098                        .map(cart -> _cartDAO.getCartProperties(cart))
099                        .collect(Collectors.toList());
100                
101        
102        Map<String, Object> result = new HashMap<>();
103        result.put("carts", nodes);
104        
105        Request request = ObjectModelHelper.getRequest(objectModel);
106        request.setAttribute(JSonReader.OBJECT_TO_READ, result);
107        
108        return EMPTY_MAP;
109    }
110}