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.category; 017 018import java.io.IOException; 019import java.util.Collection; 020import java.util.List; 021import java.util.Map; 022import java.util.Set; 023 024import org.apache.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026import org.apache.cocoon.ProcessingException; 027import org.apache.cocoon.environment.ObjectModelHelper; 028import org.apache.cocoon.generation.ServiceableGenerator; 029import org.apache.cocoon.xml.AttributesImpl; 030import org.apache.cocoon.xml.XMLUtils; 031import org.apache.commons.lang.StringUtils; 032import org.xml.sax.SAXException; 033 034import org.ametys.cms.repository.Content; 035import org.ametys.plugins.repository.AmetysObjectIterable; 036import org.ametys.plugins.repository.AmetysObjectResolver; 037import org.ametys.runtime.i18n.I18nizableText; 038 039/** 040 * SAX events representing a newsletter category 041 * 042 */ 043public class CategoryGenerator extends ServiceableGenerator 044{ 045 /** The object resolver */ 046 protected AmetysObjectResolver _resolver; 047 /** The category provider manager */ 048 protected CategoryProviderExtensionPoint _categoryProviderEP; 049 050 @Override 051 public void service(ServiceManager serviceManager) throws ServiceException 052 { 053 super.service(serviceManager); 054 _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE); 055 _categoryProviderEP = (CategoryProviderExtensionPoint) serviceManager.lookup(CategoryProviderExtensionPoint.ROLE); 056 } 057 058 @Override 059 public void generate() throws IOException, SAXException, ProcessingException 060 { 061 contentHandler.startDocument(); 062 XMLUtils.startElement(contentHandler, "categories"); 063 064 @SuppressWarnings("unchecked") 065 Map<String, Object> jsParameters = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT); 066 @SuppressWarnings("unchecked") 067 List<String> categoryIDs = (List<String>) jsParameters.get("id"); 068 069 saxCategories(categoryIDs); 070 071 XMLUtils.endElement(contentHandler, "categories"); 072 contentHandler.endDocument(); 073 074 } 075 076 /** 077 * SAX categories 078 * @param categoryIDs The category ids 079 * @throws SAXException if an error occurred while SAXing 080 */ 081 protected void saxCategories (List<String> categoryIDs) throws SAXException 082 { 083 Set<String> ids = _categoryProviderEP.getExtensionsIds(); 084 085 for (String categoryID : categoryIDs) 086 { 087 for (String id : ids) 088 { 089 CategoryProvider provider = _categoryProviderEP.getExtension(id); 090 if (provider.hasCategory(categoryID)) 091 { 092 Category category = provider.getCategory(categoryID); 093 saxCategory (provider, category, false, false); 094 } 095 } 096 } 097 } 098 /** 099 * SAX a category 100 * @param provider The category provider 101 * @param category The category to SAX 102 * @param children true to SAX category sub-categories 103 * @param newsletter true to SAX the newsletter linked with this category 104 * @throws SAXException if an error occurred while SAXing 105 */ 106 protected void saxCategory(CategoryProvider provider, Category category, boolean children, boolean newsletter) throws SAXException 107 { 108 String id = category.getId(); 109 I18nizableText title = category.getTitle(); 110 I18nizableText description = category.getDescription(); 111 String template = category.getTemplate(); 112 String siteName = category.getSiteName(); 113 String lang = category.getLang(); 114 Collection<String> automaticIds = provider.getAutomaticIds(id); 115 116 AttributesImpl attr = new AttributesImpl(); 117 attr.addAttribute("", "id", "id", "CDATA", category.getId()); 118 attr.addAttribute("", "parentId", "parentId", "CDATA", category.getParentId()); 119 attr.addAttribute("", "name", "name", "CDATA", category.getName()); 120 attr.addAttribute("", "siteName", "siteName", "CDATA", category.getSiteName()); 121 attr.addAttribute("", "lang", "lang", "CDATA", category.getLang()); 122 123 if (provider.hasChildren(id) || provider.hasNewsletters(id, siteName, lang)) 124 { 125 attr.addAttribute("", "hasChild", "hasChild", "CDATA", "true"); 126 } 127 128 attr.addAttribute("", "mode", "mode", "CDATA", provider.isWritable() ? "write" : "read"); 129 attr.addCDATAAttribute("automaticIds", StringUtils.join(automaticIds, ',')); 130 131 XMLUtils.startElement(contentHandler, "category", attr); 132 133 XMLUtils.startElement(contentHandler, "title"); 134 title.toSAX(contentHandler); 135 XMLUtils.endElement(contentHandler, "title"); 136 137 if (description != null) 138 { 139 XMLUtils.startElement(contentHandler, "description"); 140 description.toSAX(contentHandler); 141 XMLUtils.endElement(contentHandler, "description"); 142 } 143 144 XMLUtils.createElement(contentHandler, "template", template != null ? template : ""); 145 146 if (children) 147 { 148 List<Category> childCategories = provider.getCategories(category.getId()); 149 for (Category child : childCategories) 150 { 151 saxCategory(provider, child, false, false); 152 } 153 } 154 155 if (newsletter) 156 { 157 // SAX newsletters 158 AmetysObjectIterable<Content> contents = provider.getNewsletters(id, siteName, lang); 159 saxNewsletters(contents); 160 } 161 162 XMLUtils.endElement(contentHandler, "category"); 163 } 164 165 /** 166 * SAX the newsletters 167 * @param newsletters The newsletters to SAX 168 * @throws SAXException If an error occurred while SAXing 169 */ 170 protected void saxNewsletters (AmetysObjectIterable<Content> newsletters) throws SAXException 171 { 172 for (Content content : newsletters) 173 { 174 AttributesImpl attrs = new AttributesImpl(); 175 attrs.addAttribute("", "id", "id", "CDATA", content.getId()); 176 attrs.addAttribute("", "name", "name", "CDATA", content.getName()); 177 attrs.addAttribute("", "title", "title", "CDATA", content.getTitle()); 178 179 boolean automatic = content.getInternalDataHolder().getValue("automatic", false); 180 attrs.addAttribute("", "automatic", "automatic", "CDATA", String.valueOf(automatic)); 181 182 XMLUtils.createElement(contentHandler, "newsletter", attrs); 183 } 184 } 185}