001/*
002 *  Copyright 2016 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.userpref;
017
018import java.util.ArrayList;
019import java.util.Collection;
020import java.util.Collections;
021import java.util.HashMap;
022import java.util.List;
023import java.util.Map;
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.environment.ObjectModelHelper;
029import org.apache.cocoon.environment.Redirector;
030import org.apache.cocoon.environment.Request;
031import org.apache.cocoon.environment.SourceResolver;
032import org.apache.commons.lang.StringUtils;
033
034import org.ametys.cms.repository.Content;
035import org.ametys.core.user.UserIdentity;
036import org.ametys.core.userpref.UserPreference;
037import org.ametys.core.userpref.UserPreferencesErrors;
038import org.ametys.odf.course.Course;
039import org.ametys.odf.program.AbstractProgram;
040import org.ametys.odf.program.Program;
041import org.ametys.plugins.odfweb.repository.OdfPageResolver;
042import org.ametys.plugins.repository.AmetysObjectResolver;
043import org.ametys.plugins.repository.UnknownAmetysObjectException;
044import org.ametys.web.repository.page.Page;
045import org.ametys.web.userpref.SetFOUserPreferencesAction;
046
047/**
048 *  Set user preference for ODF cart 
049 */
050public class SetODFCartUserPreferencesAction extends SetFOUserPreferencesAction
051{
052    /** The ametys object resolver */
053    protected AmetysObjectResolver _resolver;
054    
055    /** The odf page resolver */
056    protected OdfPageResolver _odfPageResolver;
057    
058    private Map<String, String> _preferences;
059    
060    private List<String> _contentIdsError;
061    
062    @Override
063    public void service(ServiceManager serviceManager) throws ServiceException
064    {
065        super.service(serviceManager);
066        _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE);
067        _odfPageResolver = (OdfPageResolver) serviceManager.lookup(OdfPageResolver.ROLE);
068    }
069    
070    @Override
071    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
072    {
073        _preferences = new HashMap<>();
074        _contentIdsError = new ArrayList<>();
075        
076        Request request = ObjectModelHelper.getRequest(objectModel);
077        @SuppressWarnings("unchecked")
078        Map<String, Object> parentContext = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT);
079        
080        String storageContext = getStorageContext(request, parameters);
081        UserIdentity user = getUser(request, parameters);
082        
083        Map<String, String> results = new HashMap<>();
084        Map<String, String> contextVars = getContextVars(request);
085        
086        _handleValues(request, contextVars);
087        
088        if (user != null)
089        {
090            List<String> preferenceIds = getPreferenceIds(request, parentContext);
091            
092            results = setUserPreferences(request, storageContext, contextVars, user, preferenceIds);
093            
094            String siteName = (String) request.getAttribute("site");
095            notifyUserPrefChanged(siteName, storageContext);
096        }
097        
098        results.put("contentError", StringUtils.join(_contentIdsError, ','));
099        return results;
100    }
101    
102    private void _handleValues(Request request, Map<String, String> contextVars)
103    {
104        String siteName = (String) request.getAttribute("site");
105        
106        for (UserPreference preference : _userPrefEP.getUserPreferences(contextVars).values())
107        {
108            String id = preference.getId();
109            String contentIdsAsString = request.getParameter(id);
110            if (contentIdsAsString != null)
111            {
112                List<String> contentIds = new ArrayList<>();
113                for (String contentId : StringUtils.split(contentIdsAsString, ","))
114                {
115                    try
116                    {
117                        Content content = _resolver.resolveById(contentId);
118                        Page page = null;
119                        if (id.equals(ODFCartUserPreferencesStorage.USER_PREF_PROGRAM_IDS))
120                        {
121                            page = _odfPageResolver.getProgramPage((Program) content, siteName);
122                        }
123                        else if (id.equals(ODFCartUserPreferencesStorage.USER_PREF_COURSE_IDS))
124                        {
125                            page = _odfPageResolver.getCoursePage((Course) content, (AbstractProgram) null, siteName);
126                        }
127                        
128                        if (page != null)
129                        {
130                            contentIds.add(contentId);
131                        }
132                        else
133                        {
134                            _contentIdsError.add(contentId);
135                        }
136                    }
137                    catch (UnknownAmetysObjectException e)
138                    {
139                        _contentIdsError.add(contentId);
140                    }
141                }
142                String valuesStr = StringUtils.join(contentIds, ',');
143                _preferences.put(id, valuesStr);
144            }
145        }
146    }
147    
148    @Override
149    protected Map<String, String> _getValues(Request request, Map<String, String> contextVars, UserIdentity user, Collection<String> preferenceIds, UserPreferencesErrors errors)
150    {
151        return _preferences;
152    }
153    
154    @Override
155    protected List<String> getPreferenceIds(Request request, Map<String, Object> parentContext)
156    {
157        List<String> preferenceIdSet = new ArrayList<>();
158        
159        String prefIdStr = (String) request.get("preferences-ids");
160        if (StringUtils.isNotBlank(prefIdStr))
161        {
162            Collections.addAll(preferenceIdSet, StringUtils.split(prefIdStr, ','));
163        }
164        
165        return preferenceIdSet;
166    }
167}