001/* 002 * Copyright 2019 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.odfweb.cart; 017 018import java.io.IOException; 019import java.util.Arrays; 020import java.util.Objects; 021import java.util.Set; 022import java.util.stream.Collectors; 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.environment.Request; 029import org.apache.cocoon.generation.ServiceableGenerator; 030import org.apache.cocoon.serialization.ZipArchiveSerializer; 031import org.apache.cocoon.xml.AttributesImpl; 032import org.apache.cocoon.xml.XMLUtils; 033import org.xml.sax.SAXException; 034 035import org.ametys.cms.repository.Content; 036 037/** 038 * Generate the list of PDF file to include in a zip for downloading cart 039 */ 040public class CartDownloadGenerator extends ServiceableGenerator 041{ 042 private ODFCartManager _cartManager; 043 044 @Override 045 public void service(ServiceManager smanager) throws ServiceException 046 { 047 super.service(smanager); 048 _cartManager = (ODFCartManager) smanager.lookup(ODFCartManager.ROLE); 049 } 050 051 public void generate() throws IOException, SAXException, ProcessingException 052 { 053 Request request = ObjectModelHelper.getRequest(objectModel); 054 String[] itemIds = request.getParameterValues("itemId"); 055 String siteName = request.getParameter("siteName"); 056 057 StringBuilder sb = new StringBuilder(); 058 sb.append("cocoon:/") 059 .append(siteName) 060 .append("/_content/"); 061 062 String downloadUriPrefix = sb.toString(); 063 064 contentHandler.startDocument(); 065 contentHandler.startPrefixMapping("zip", ZipArchiveSerializer.ZIP_NAMESPACE); 066 XMLUtils.startElement(contentHandler, ZipArchiveSerializer.ZIP_NAMESPACE, "archive"); 067 068 Set<Content> contents = Arrays.stream(itemIds) 069 .map(i -> _cartManager.getCartItem(i)) 070 .filter(Objects::nonNull) 071 .map(item -> item.getContent()) 072 .collect(Collectors.toSet()); 073 074 for (Content content : contents) 075 { 076 String contentName = content.getName(); 077 078 AttributesImpl zipAttrs = new AttributesImpl(); 079 zipAttrs.addAttribute("", "name", "name", "CDATA", content.getTitle() + ".pdf"); 080 zipAttrs.addAttribute("", "src", "src", "CDATA", downloadUriPrefix + contentName + ".pdf"); 081 XMLUtils.startElement(contentHandler, ZipArchiveSerializer.ZIP_NAMESPACE, "entry", zipAttrs); 082 XMLUtils.endElement(contentHandler, ZipArchiveSerializer.ZIP_NAMESPACE, "entry"); 083 } 084 085 XMLUtils.endElement(contentHandler, ZipArchiveSerializer.ZIP_NAMESPACE, "archive"); 086 contentHandler.endPrefixMapping("zip"); 087 contentHandler.endDocument(); 088 } 089}