001/* 002 * Copyright 2014 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.sms.service; 017 018import java.io.IOException; 019 020import javax.jcr.RepositoryException; 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.environment.Request; 027import org.apache.cocoon.generation.ServiceableGenerator; 028import org.apache.cocoon.xml.AttributesImpl; 029import org.apache.cocoon.xml.XMLUtils; 030import org.apache.commons.lang.StringUtils; 031import org.xml.sax.SAXException; 032 033import org.ametys.plugins.repository.AmetysObjectResolver; 034import org.ametys.plugins.sms.dao.JCRSubscribersList; 035import org.ametys.plugins.sms.dao.SmsListDAO; 036import org.ametys.runtime.config.Config; 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.site.SiteConfigurationExtensionPoint; 042 043 044/** 045 * Generate the list register xml form. 046 */ 047public class SubscribeGenerator extends ServiceableGenerator 048{ 049 /** Subscriber list DAO */ 050 protected SmsListDAO _subscriberListDAO; 051 052 /** The ametys resolver */ 053 protected AmetysObjectResolver _resolver; 054 055 /** The site configuration */ 056 protected SiteConfigurationExtensionPoint _siteConfiguration; 057 058 /** Page helper */ 059 protected PageHelper _pageHelper; 060 061 @Override 062 public void service(ServiceManager serviceManager) throws ServiceException 063 { 064 super.service(serviceManager); 065 _subscriberListDAO = (SmsListDAO) serviceManager.lookup(SmsListDAO.ROLE); 066 _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE); 067 _siteConfiguration = (SiteConfigurationExtensionPoint) serviceManager.lookup(SiteConfigurationExtensionPoint.ROLE); 068 _pageHelper = (PageHelper) serviceManager.lookup(PageHelper.ROLE); 069 } 070 071 @Override 072 public void generate() throws IOException, SAXException, ProcessingException 073 { 074 Request request = ObjectModelHelper.getRequest(objectModel); 075 076 String siteName = (String) request.getAttribute(WebConstants.REQUEST_ATTR_SITE_NAME); 077 Page page = (Page) request.getAttribute(WebConstants.REQUEST_ATTR_PAGE); 078 String lang = page.getSitemapName(); 079 080 ZoneItem zoneItem = (ZoneItem) request.getAttribute(WebConstants.REQUEST_ATTR_ZONEITEM); 081 String zoneItemId = zoneItem.getId(); 082 083 String list = zoneItem.getServiceParameters().getValue("subscribers-list", false, ""); 084 085 contentHandler.startDocument(); 086 XMLUtils.startElement(contentHandler, "form"); 087 088 // we put the zoneID to get all the service parameter 089 XMLUtils.startElement(contentHandler, "zone"); 090 XMLUtils.createElement(contentHandler, "zone-id", zoneItemId); 091 XMLUtils.endElement(contentHandler, "zone"); 092 093 // we get the number exemple in the configuration 094 String samples = Config.getInstance().getValue("org.ametys.plugins.sms.broker.sample"); 095 String[] tabSamples = StringUtils.split(samples, "\n"); 096 097 XMLUtils.startElement(contentHandler, "samples"); 098 for (int i = 0; i < tabSamples.length; i++) 099 { 100 XMLUtils.createElement(contentHandler, "sample", tabSamples[i]); 101 } 102 XMLUtils.endElement(contentHandler, "samples"); 103 104 XMLUtils.startElement(contentHandler, "lists"); 105 106 // If the admin lets the user choose 107 if (list.equals("-")) 108 { 109 // we get all the subscribers list 110 try 111 { 112 for (JCRSubscribersList subList : _subscriberListDAO.getSubscribersLists(siteName, lang)) 113 { 114 AttributesImpl attr = new AttributesImpl(); 115 attr.addCDATAAttribute("id", subList.getId()); 116 117 XMLUtils.createElement(contentHandler, "list", attr, subList.getTitle()); 118 } 119 } 120 catch (RepositoryException e) 121 { 122 getLogger().error("Unable to retrieve SMS list", e); 123 } 124 } 125 else 126 { 127 // we put the chosen list 128 AttributesImpl attr = new AttributesImpl(); 129 attr.addCDATAAttribute("id", list); 130 131 JCRSubscribersList subList = _resolver.resolveById(list); 132 133 XMLUtils.createElement(contentHandler, "list", attr, subList.getTitle()); 134 } 135 136 XMLUtils.endElement(contentHandler, "lists"); 137 138 XMLUtils.startElement(contentHandler, "page-info"); 139 XMLUtils.createElement(contentHandler, "has-captcha", String.valueOf(_pageHelper.isCaptchaRequired(page))); 140 XMLUtils.endElement(contentHandler, "page-info"); 141 142 XMLUtils.endElement(contentHandler, "form"); 143 144 contentHandler.endDocument(); 145 } 146 147}