001/* 002 * Copyright 2013 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.cart.generators; 017 018import java.io.IOException; 019import java.util.HashMap; 020import java.util.List; 021import java.util.Map; 022 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.apache.cocoon.ProcessingException; 026import org.apache.cocoon.components.source.impl.SitemapSource; 027import org.apache.cocoon.environment.ObjectModelHelper; 028import org.apache.cocoon.environment.Request; 029import org.apache.cocoon.generation.ServiceableGenerator; 030import org.apache.cocoon.xml.AttributesImpl; 031import org.apache.cocoon.xml.XMLUtils; 032import org.apache.commons.lang3.StringUtils; 033import org.apache.excalibur.source.SourceResolver; 034import org.xml.sax.SAXException; 035 036import org.ametys.core.user.CurrentUserProvider; 037import org.ametys.core.user.User; 038import org.ametys.core.user.UserIdentity; 039import org.ametys.core.user.UserManager; 040import org.ametys.core.util.DateUtils; 041import org.ametys.core.util.IgnoreRootHandler; 042import org.ametys.core.util.JSONUtils; 043import org.ametys.core.util.URIUtils; 044import org.ametys.plugins.cart.Cart; 045import org.ametys.plugins.cart.CartElement; 046import org.ametys.plugins.cart.CartsDAO; 047import org.ametys.plugins.cart.ContentElement; 048import org.ametys.plugins.cart.QueryElement; 049import org.ametys.plugins.cart.QueryFromDirectoryElement; 050import org.ametys.plugins.cart.ResourceElement; 051import org.ametys.plugins.repository.AmetysObjectResolver; 052import org.ametys.runtime.i18n.I18nizableText; 053 054/** 055 * SAX elements of a cart 056 */ 057public class CartElementDetailsGenerator extends ServiceableGenerator 058{ 059 /** The Ametys object resolver */ 060 protected AmetysObjectResolver _resolver; 061 /** The current user provider */ 062 protected CurrentUserProvider _userProvider; 063 /** The Json utils */ 064 private JSONUtils _jsonUtils; 065 /** The source resolver */ 066 private SourceResolver _sourceResolver; 067 /** The user manager */ 068 private UserManager _userManager; 069 /** The carts DAO */ 070 private CartsDAO _cartsDAO; 071 072 @Override 073 public void service(ServiceManager smanager) throws ServiceException 074 { 075 super.service(smanager); 076 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 077 _jsonUtils = (JSONUtils) manager.lookup(JSONUtils.ROLE); 078 _sourceResolver = (SourceResolver) manager.lookup(SourceResolver.ROLE); 079 _userManager = (UserManager) smanager.lookup(UserManager.ROLE); 080 _userProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE); 081 _cartsDAO = (CartsDAO) smanager.lookup(CartsDAO.ROLE); 082 } 083 084 @Override 085 public void generate() throws IOException, SAXException, ProcessingException 086 { 087 088 Request request = ObjectModelHelper.getRequest(objectModel); 089 String id = request.getParameter("cartId"); 090 091 Cart cart = _resolver.resolveById(id); 092 UserIdentity user = _userProvider.getUser(); 093 094 contentHandler.startDocument(); 095 096 if (_cartsDAO.canRead(user, cart)) 097 { 098 AttributesImpl attrs = new AttributesImpl(); 099 attrs.addCDATAAttribute("id", cart.getId()); 100 101 XMLUtils.startElement(contentHandler, "cart", attrs); 102 103 XMLUtils.createElement(contentHandler, "label", cart.getTitle()); 104 XMLUtils.createElement(contentHandler, "description", cart.getDescription()); 105 106 try 107 { 108 List<QueryElement> queries = cart.getQueryCartElements(); 109 _saxQueries(queries); 110 } 111 catch (Exception e) 112 { 113 throw new SAXException("Unable to sax queries", e); 114 } 115 116 try 117 { 118 List<QueryFromDirectoryElement> queries = cart.getQueryFromDirectoryCartElements(); 119 _saxQueries(queries); 120 } 121 catch (Exception e) 122 { 123 throw new SAXException("Unable to sax queries", e); 124 } 125 126 try 127 { 128 List<ContentElement> contents = cart.getContentCartElements(); 129 _saxContents(contents); 130 } 131 catch (Exception e) 132 { 133 throw new SAXException("Unable to sax contents", e); 134 } 135 136 try 137 { 138 List<ResourceElement> resources = cart.getResourceCartElements(); 139 _saxResources(resources); 140 } 141 catch (Exception e) 142 { 143 throw new SAXException("Unable to sax resources", e); 144 } 145 146 XMLUtils.endElement(contentHandler, "cart"); 147 } 148 else 149 { 150 XMLUtils.createElement(contentHandler, "not-allowed"); 151 } 152 153 contentHandler.endDocument(); 154 } 155 156 /** 157 * SAX resources of the cart 158 * @param resources The resources of the cart 159 * @throws IOException if an error occurred 160 * @throws SAXException if an error occurred while saxing 161 */ 162 protected void _saxResources (List<ResourceElement> resources) throws IOException, SAXException 163 { 164 XMLUtils.startElement(contentHandler, "resources"); 165 for (ResourceElement resource : resources) 166 { 167 AttributesImpl attrs = new AttributesImpl(); 168 attrs.addCDATAAttribute("id", resource.getId()); 169 XMLUtils.startElement(contentHandler, "resource", attrs); 170 171 _saxCartElement(resource, resource.getGroups().get(0)); 172 173 XMLUtils.endElement(contentHandler, "resource"); 174 } 175 XMLUtils.endElement(contentHandler, "resources"); 176 } 177 178 /** 179 * SAX the attributes of a cart element 180 * @param cartElement The element to sax 181 * @param group The group to sax with 182 * @throws SAXException if an error occurred while saxing 183 */ 184 protected void _saxCartElement(CartElement cartElement, I18nizableText group) throws SAXException 185 { 186 cartElement.getTitle().toSAX(contentHandler, "title"); 187 188 XMLUtils.createElement(contentHandler, "creation", DateUtils.zonedDateTimeToString(cartElement.getCreationDate())); 189 190 UserIdentity creatorIdentity = cartElement.getCreator(); 191 User creator = _userManager.getUser(creatorIdentity); 192 XMLUtils.createElement(contentHandler, "creator", creator != null ? creator.getFullName() : StringUtils.EMPTY); 193 194 XMLUtils.createElement(contentHandler, "lastModification", DateUtils.zonedDateTimeToString(cartElement.getLastModified())); 195 196 UserIdentity lastContributorIdentity = cartElement.getLastContributor(); 197 User lastContributor = _userManager.getUser(lastContributorIdentity.getPopulationId(), lastContributorIdentity.getLogin()); 198 199 XMLUtils.createElement(contentHandler, "lastContributor", lastContributor != null ? lastContributor.getFullName() : StringUtils.EMPTY); 200 201 group.toSAX(contentHandler, "group"); 202 } 203 204 /** 205 * SAX contents of the cart 206 * @param contents The contents of the cart 207 * @throws IOException if an error occurred 208 * @throws SAXException if an error occurred while saxing 209 */ 210 protected void _saxContents (List<ContentElement> contents) throws IOException, SAXException 211 { 212 SitemapSource sitemapSource = null; 213 try 214 { 215 XMLUtils.startElement(contentHandler, "contents"); 216 for (ContentElement content : contents) 217 { 218 List<I18nizableText> groups = content.getGroups(); 219 for (I18nizableText group : groups) 220 { 221 XMLUtils.startElement(contentHandler, "content"); 222 _saxCartElement(content, group); 223 224 String cocoonUrl = _contentToCocoonUrl(content); 225 sitemapSource = (SitemapSource) _sourceResolver.resolveURI(cocoonUrl); 226 sitemapSource.toSAX(new IgnoreRootHandler(contentHandler)); 227 XMLUtils.endElement(contentHandler, "content"); 228 } 229 230 } 231 XMLUtils.endElement(contentHandler, "contents"); 232 } 233 finally 234 { 235 resolver.release(sitemapSource); 236 } 237 } 238 239 /** 240 * Returns the Cocoon pipeline URL that returns content details 241 * @param content The content 242 * @return The cocoon URL that returns the content details 243 */ 244 protected String _contentToCocoonUrl(ContentElement content) 245 { 246 return "cocoon://_content.html?contentId=" + URIUtils.encodeParameter(content.getId()); 247 } 248 249 /** 250 * SAX queries of the cart 251 * @param queries The queries of the cart 252 * @throws IOException if an error occurred 253 * @throws SAXException if an error occurred while saxing 254 */ 255 protected void _saxQueries (List<? extends QueryElement> queries) throws IOException, SAXException 256 { 257 SitemapSource sitemapSource = null; 258 try 259 { 260 XMLUtils.startElement(contentHandler, "queries"); 261 for (QueryElement query : queries) 262 { 263 XMLUtils.startElement(contentHandler, "query"); 264 _saxCartElement(query, query.getGroups().get(0)); 265 266 String queryContent = query.getQueryContent(); 267 Map<String, Object> queryDetails = _jsonUtils.convertJsonToMap(queryContent); 268 269 String printPluginUrl = queryDetails.containsKey("printUrlPlugin") ? (String) queryDetails.get("printUrlPlugin") : "cms"; 270 String printUrl = queryDetails.containsKey("printUrl") ? (String) queryDetails.get("printUrl") : "search/print.html"; 271 @SuppressWarnings("unchecked") 272 Map<String, Object> exportParams = (Map<String, Object>) queryDetails.get("exportParams"); 273 Map<String, String> params = new HashMap<>(); 274 params.put("parameters", _jsonUtils.convertObjectToJson(exportParams)); 275 276 sitemapSource = (SitemapSource) _sourceResolver.resolveURI("cocoon://_plugins/" + printPluginUrl + "/" + printUrl + "?parameters=" + _jsonUtils.convertObjectToJson(exportParams), null, new HashMap<>()); 277 sitemapSource.toSAX(new IgnoreRootHandler(contentHandler)); 278 XMLUtils.endElement(contentHandler, "query"); 279 } 280 XMLUtils.endElement(contentHandler, "queries"); 281 } 282 finally 283 { 284 resolver.release(sitemapSource); 285 } 286 } 287 288}