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.io.IOException;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.cocoon.ProcessingException;
025import org.apache.cocoon.environment.ObjectModelHelper;
026import org.apache.cocoon.generation.ServiceableGenerator;
027import org.apache.cocoon.xml.XMLUtils;
028import org.apache.commons.lang.StringUtils;
029import org.xml.sax.SAXException;
030
031import org.ametys.core.user.UserIdentity;
032import org.ametys.plugins.core.user.UserHelper;
033import org.ametys.plugins.odfweb.cart.ODFCartManager.ODFCartItem;
034import org.ametys.web.repository.site.Site;
035import org.ametys.web.repository.site.SiteManager;
036
037
038/**
039 * Generator for cart shared by mail
040 *
041 */
042public class ShareCartMailGenerator extends ServiceableGenerator
043{
044    private ODFCartManager _odfCartManager;
045    private UserHelper _userHelper;
046    private SiteManager _siteManager;
047    
048    @Override
049    public void service(ServiceManager smanager) throws ServiceException
050    {
051        super.service(smanager);
052        _odfCartManager = (ODFCartManager) smanager.lookup(ODFCartManager.ROLE);
053        _userHelper = (UserHelper) smanager.lookup(UserHelper.ROLE);
054        _siteManager = (SiteManager) smanager.lookup(SiteManager.ROLE);
055    }
056    
057    @SuppressWarnings("unchecked")
058    public void generate() throws IOException, SAXException, ProcessingException
059    {
060        contentHandler.startDocument();
061        XMLUtils.startElement(contentHandler, "Mail");
062        
063        Map<String, Object> ctxParameters = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT);
064        
065        UserIdentity owner = (UserIdentity) ctxParameters.get("owner");
066        String message = (String) ctxParameters.get("message");
067        String siteName = (String) ctxParameters.get("siteName");
068        List<ODFCartItem> items = (List<ODFCartItem>) ctxParameters.get("items");
069        
070        if (owner != null)
071        {
072            _userHelper.saxUserIdentity(owner, contentHandler, "owner");
073        }
074        
075        if (StringUtils.isNotEmpty(message))
076        {
077            XMLUtils.createElement(contentHandler, "message", message);
078        }
079        
080        Site site = _siteManager.getSite(siteName);
081        XMLUtils.createElement(contentHandler, "siteTitle", site.getTitle());
082        XMLUtils.createElement(contentHandler, "siteUrl", site.getUrl());
083        
084        XMLUtils.startElement(contentHandler, "items");
085        for (ODFCartItem item : items)
086        {
087            _odfCartManager.saxCartItem(contentHandler, item, siteName);
088        }
089        XMLUtils.endElement(contentHandler, "items");
090        
091        XMLUtils.endElement(contentHandler, "Mail");
092        contentHandler.endDocument();
093
094    }
095
096}