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.subscribe; 017 018import java.io.IOException; 019import java.util.List; 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.generation.ServiceableGenerator; 027import org.apache.cocoon.xml.XMLUtils; 028import org.xml.sax.SAXException; 029 030import org.ametys.core.util.DateUtils; 031import org.ametys.plugins.newsletter.daos.Subscriber; 032import org.ametys.plugins.newsletter.daos.SubscribersDAO; 033 034/** 035 * Generates SAX events for subscribers 036 */ 037public class SubscribersGenerator extends ServiceableGenerator 038{ 039 private SubscribersDAO _subscribersDao; 040 041 @Override 042 public void service(ServiceManager smanager) throws ServiceException 043 { 044 super.service(smanager); 045 _subscribersDao = (SubscribersDAO) smanager.lookup(SubscribersDAO.ROLE); 046 } 047 048 @Override 049 public void generate() throws IOException, SAXException, ProcessingException 050 { 051 Request request = ObjectModelHelper.getRequest(objectModel); 052 053 String siteName = request.getParameter("siteName"); 054 String categoryId = request.getParameter("categoryID"); 055 056 contentHandler.startDocument(); 057 XMLUtils.startElement(contentHandler, "subscribers"); 058 059 List<Subscriber> subscribers; 060 if (siteName != null && categoryId != null) 061 { 062 subscribers = _subscribersDao.getSubscribers(siteName, categoryId); 063 } 064 else 065 { 066 subscribers = _subscribersDao.getSubscribers(); 067 } 068 069 for (Subscriber subscriber : subscribers) 070 { 071 XMLUtils.startElement(contentHandler, "subscriber"); 072 073 XMLUtils.createElement(contentHandler, "email", subscriber.getEmail()); 074 XMLUtils.createElement(contentHandler, "siteName", subscriber.getSiteName()); 075 XMLUtils.createElement(contentHandler, "category", subscriber.getCategoryId()); 076 XMLUtils.createElement(contentHandler, "subscribedAt", DateUtils.dateToString(subscriber.getSubscribedAt())); 077 078 XMLUtils.endElement(contentHandler, "subscriber"); 079 } 080 081 XMLUtils.endElement(contentHandler, "subscribers"); 082 contentHandler.endDocument(); 083 } 084 085}