001/*
002 *  Copyright 2010 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.newsletter.generators;
017
018import java.io.IOException;
019import java.util.Set;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.cocoon.ProcessingException;
024import org.apache.cocoon.environment.ObjectModelHelper;
025import org.apache.cocoon.environment.Request;
026import org.apache.cocoon.xml.XMLUtils;
027import org.xml.sax.SAXException;
028
029import org.ametys.core.user.CurrentUserProvider;
030import org.ametys.core.user.User;
031import org.ametys.core.user.UserIdentity;
032import org.ametys.core.user.UserManager;
033import org.ametys.plugins.core.user.UserHelper;
034import org.ametys.plugins.newsletter.category.Category;
035import org.ametys.plugins.newsletter.category.CategoryGenerator;
036import org.ametys.plugins.newsletter.category.CategoryProvider;
037import org.ametys.web.cache.PageHelper;
038import org.ametys.web.repository.page.Page;
039import org.ametys.web.repository.page.ZoneItem;
040import org.ametys.web.repository.page.ZoneItemException;
041import org.ametys.web.site.SiteConfigurationExtensionPoint;
042
043/**
044 * Newsletter service
045 *
046 */
047public class NewsletterServiceGenerator extends CategoryGenerator
048{
049    /** The site configuration */
050    protected SiteConfigurationExtensionPoint _siteConfiguration;
051    /** The user manager */
052    protected UserManager _userManager;
053    /** The user helper */
054    protected UserHelper _userHelper;
055    /** Page helper */
056    protected PageHelper _pageHelper;
057    /** The current user provider */
058    protected CurrentUserProvider _currentUserProvider;
059    
060    @Override
061    public void service(ServiceManager smanager) throws ServiceException
062    {
063        super.service(smanager);
064        _siteConfiguration = (SiteConfigurationExtensionPoint) smanager.lookup(SiteConfigurationExtensionPoint.ROLE);
065        _userManager = (UserManager) smanager.lookup(UserManager.ROLE);
066        _userHelper = (UserHelper) smanager.lookup(UserHelper.ROLE);
067        _pageHelper = (PageHelper) smanager.lookup(PageHelper.ROLE);
068        _currentUserProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE);
069    }
070    
071    @Override
072    public void generate() throws IOException, SAXException, ProcessingException
073    {
074        Request request = ObjectModelHelper.getRequest(objectModel);
075        ZoneItem zoneItem = (ZoneItem) request.getAttribute(ZoneItem.class.getName());
076        
077        contentHandler.startDocument();
078        XMLUtils.startElement(contentHandler, "NewsletterService");
079        
080        if (zoneItem != null)
081        {
082            XMLUtils.startElement(contentHandler, "Categories");
083            String[] categoryIDs = zoneItem.getServiceParameters().getString("categories", "").split(",");
084            Set<String> ids = _categoryProviderEP.getExtensionsIds();
085            
086            for (String categoryID : categoryIDs)
087            {
088                boolean found = false;
089                
090                for (String id : ids)
091                {
092                    CategoryProvider provider = _categoryProviderEP.getExtension(id);
093                    if (provider.hasCategory(categoryID))
094                    {
095                        Category category = provider.getCategory(categoryID);
096                        saxCategory (provider, category, true, false);
097                        found = true;
098                        break;
099                    }
100                }
101                
102                if (!found)
103                {
104                    throw new ZoneItemException("The newsletter category with id '" + categoryID + "' was not found");
105                }
106            }
107            XMLUtils.endElement(contentHandler, "Categories");
108        }
109        
110        XMLUtils.startElement(contentHandler, "FormResult");
111        // SAX message
112        String msg = parameters.getParameter("msg", "");
113        if (msg.length() != 0)
114        {
115            XMLUtils.createElement(contentHandler, "msg", msg);
116        }
117        
118        String email = parameters.getParameter("email", "");
119        if (email.length() != 0)
120        {
121            XMLUtils.createElement(contentHandler, "email", email);
122        }
123        
124        String[] ids = parameters.getParameter("subscribeTo", "").split(",");
125        for (String id : ids)
126        {
127            if (id.length() > 0)
128            {
129                XMLUtils.createElement(contentHandler, "subscribeTo", id);
130            }
131        }
132        
133        ids = parameters.getParameter("alreadySubscribeTo", "").split(",");
134        for (String id : ids)
135        {
136            if (id.length() > 0)
137            {
138                XMLUtils.createElement(contentHandler, "alreadySubscribeTo", id);
139            }
140            
141        }
142        
143        Page page = (Page) request.getAttribute(Page.class.getName());
144        XMLUtils.createElement(contentHandler, "has-captcha", String.valueOf(_pageHelper.isCaptchaRequired(page)));
145        
146        XMLUtils.endElement(contentHandler, "FormResult");
147        
148        _saxInfoUser();
149        
150        XMLUtils.endElement(contentHandler, "NewsletterService");
151        contentHandler.endDocument();
152    }
153    
154    private void _saxInfoUser() throws SAXException
155    {
156        UserIdentity userIdentity = _currentUserProvider.getUser();
157        if (userIdentity != null)
158        {
159            User user = _userManager.getUser(userIdentity);
160            _userHelper.saxUser(user, contentHandler);
161        }
162    }
163    
164    
165}