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