001/*
002 *  Copyright 2019 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.odfweb.cart;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.parameters.Parameters;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.cocoon.acting.ServiceableAction;
027import org.apache.cocoon.environment.ObjectModelHelper;
028import org.apache.cocoon.environment.Redirector;
029import org.apache.cocoon.environment.Request;
030import org.apache.cocoon.environment.SourceResolver;
031
032import org.ametys.cms.repository.Content;
033import org.ametys.core.cocoon.JSonReader;
034import org.ametys.core.user.CurrentUserProvider;
035import org.ametys.core.user.UserIdentity;
036import org.ametys.core.userpref.UserPreferencesException;
037import org.ametys.odf.program.Program;
038import org.ametys.plugins.odfweb.cart.ODFCartManager.ODFCartItem;
039import org.ametys.web.WebHelper;
040
041/**
042 * Get the ODF cart items saved in user preferences
043 *
044 */
045public class GetSavedOdfCartItemsAction extends ServiceableAction
046{
047    private ODFCartManager _cartManager;
048    private CurrentUserProvider _currentUserProvider;
049    
050    @Override
051    public void service(ServiceManager smanager) throws ServiceException
052    {
053        super.service(smanager);
054        _cartManager = (ODFCartManager) smanager.lookup(ODFCartManager.ROLE);
055        _currentUserProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE);
056    }
057    
058    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
059    {
060        Request request = ObjectModelHelper.getRequest(objectModel);
061
062        List<Map<String, Object>> items2json = new ArrayList<>();
063        
064        String siteName = WebHelper.getSiteName(request);
065        
066        UserIdentity owner = _currentUserProvider.getUser();
067        if (owner != null)
068        {
069            try
070            {
071                List<ODFCartItem> items = _cartManager.getCartItems(owner, siteName);
072                for (ODFCartItem item : items)
073                {
074                    Map<String, Object> item2json = new HashMap<>();
075                    item2json.put("id", item.getId());
076                    Content content = item.getContent();
077                    item2json.put("contentId", content.getId());
078                    item2json.put("title", content.getTitle());
079                    
080                    Program parentProgram = item.getParentProgram();
081                    if (parentProgram != null)
082                    {
083                        item2json.put("parentProgramId", parentProgram.getId());
084                    }
085                    items2json.add(item2json);
086                }
087            }
088            catch (UserPreferencesException e)
089            {
090                getLogger().error("Failed to get cart items for current user " + owner, e);
091            }
092        }
093        
094        Map<String, Object> result = new HashMap<>();
095        result.put("items", items2json);
096        result.put("isAnonymous", owner == null);
097        
098        request.setAttribute(JSonReader.OBJECT_TO_READ, result);
099        return EMPTY_MAP;
100    }
101
102}