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.Map;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022import org.apache.cocoon.ProcessingException;
023import org.apache.cocoon.xml.AttributesImpl;
024import org.apache.cocoon.xml.XMLUtils;
025import org.apache.commons.lang.StringUtils;
026import org.xml.sax.SAXException;
027
028import org.ametys.cms.repository.ModifiableContent;
029import org.ametys.cms.transformation.xslt.ResolveURIComponent;
030import org.ametys.core.userpref.UserPreference;
031import org.ametys.odf.course.Course;
032import org.ametys.odf.program.AbstractProgram;
033import org.ametys.odf.program.Program;
034import org.ametys.plugins.odfweb.repository.OdfPageResolver;
035import org.ametys.plugins.repository.AmetysObjectResolver;
036import org.ametys.plugins.repository.UnknownAmetysObjectException;
037import org.ametys.runtime.parameter.ParameterHelper;
038import org.ametys.web.repository.page.Page;
039import org.ametys.web.userpref.FOUserPreferencesConstants;
040import org.ametys.web.userpref.FOUserPreferencesGenerator;
041
042/**
043 * Get user preferences for ODF cart
044 */
045public class ODFCartUserPreferencesGenerator extends FOUserPreferencesGenerator
046{
047    /** The ametys object resolver */
048    protected AmetysObjectResolver _resolver;
049    
050    /** The odf page resolver */
051    protected OdfPageResolver _odfPageResolver;
052    
053    @Override
054    public void service(ServiceManager serviceManager) throws ServiceException
055    {
056        super.service(serviceManager);
057        _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE);
058        _odfPageResolver = (OdfPageResolver) serviceManager.lookup(OdfPageResolver.ROLE);
059    }
060    
061    @Override
062    protected void _saxPreference(UserPreference preference, Object value, Map<String, String> contextVars) throws SAXException, ProcessingException
063    {
064        String preferenceId = preference.getId();
065        if (preferenceId.equals("cartProgramIds") || preferenceId.equals("cartCourseIds"))
066        {
067            String siteName = contextVars.get(FOUserPreferencesConstants.CONTEXT_VAR_SITENAME);
068            
069            AttributesImpl attr = new AttributesImpl();
070            attr.addCDATAAttribute("id", preferenceId);
071            XMLUtils.startElement(contentHandler, "preference", attr);
072            
073            String idContents = ParameterHelper.valueToString(value);
074            XMLUtils.startElement(contentHandler, "contents");
075            if (StringUtils.isNotBlank(idContents))
076            {
077                for (String idContent : StringUtils.split(idContents, ","))
078                {
079                    try
080                    {
081                        ModifiableContent content = _resolver.resolveById(idContent);
082                        Page page = null;
083                        if (content instanceof Course)
084                        {
085                            page = _odfPageResolver.getCoursePage((Course) content, (AbstractProgram) null, siteName);
086                        }
087                        else if (content instanceof Program)
088                        {
089                            page = _odfPageResolver.getProgramPage((Program) content, siteName);
090                        }
091                        
092                        if (page != null)
093                        {
094                            String pageId = page.getId();
095                            AttributesImpl attrContent = new AttributesImpl();
096                            String pagePath = ResolveURIComponent.resolve("page", pageId, false);
097                            
098                            attrContent.addCDATAAttribute("pagePath", pagePath);
099                            
100                            attrContent.addCDATAAttribute("pageId", pageId);
101                            attrContent.addCDATAAttribute("title", content.getTitle());
102                            XMLUtils.createElement(contentHandler, "content", attrContent, idContent);
103                        }
104                        else
105                        {
106                            getLogger().info("Cannot get the page linked to the content with id: " + idContent + ". It will be ignored.");
107                        }
108                        
109                    }
110                    catch (UnknownAmetysObjectException e)
111                    {
112                        getLogger().info("The content with id: " + idContent + " doesn't exist. It will be ignored.", e);
113                    }
114                }
115            }
116            XMLUtils.endElement(contentHandler, "contents");
117            
118            XMLUtils.endElement(contentHandler, "preference");
119        }
120        else
121        {
122            super._saxPreference(preference, value, contextVars);
123        }
124    }
125    
126}