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