KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > content > compdoc > CompDocEvents


1 /*
2  * $Id: $
3  *
4  * Copyright 2005-2006 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7  * use this file except in compliance with the License. You may obtain a copy of
8  * the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations
16  * under the License.
17  */

18 package org.ofbiz.content.compdoc;
19
20 import java.io.ByteArrayInputStream JavaDoc;
21 import java.io.FileNotFoundException JavaDoc;
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Locale JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 import javax.servlet.ServletContext JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33 import javax.servlet.http.HttpSession JavaDoc;
34
35 import org.ofbiz.base.util.Debug;
36 import org.ofbiz.base.util.UtilHttp;
37 import org.ofbiz.base.util.UtilMisc;
38 import org.ofbiz.base.util.UtilProperties;
39 import org.ofbiz.base.util.UtilValidate;
40 import org.ofbiz.base.util.UtilFormatOut;
41 import org.ofbiz.entity.GenericDelegator;
42 import org.ofbiz.entity.GenericEntityException;
43 import org.ofbiz.entity.GenericValue;
44 import org.ofbiz.entity.util.ByteWrapper;
45 import org.ofbiz.security.Security;
46 import org.ofbiz.service.GenericServiceException;
47 import org.ofbiz.service.LocalDispatcher;
48 import org.ofbiz.service.ModelService;
49 import org.ofbiz.service.ServiceAuthException;
50 import org.ofbiz.service.ServiceUtil;
51 import org.ofbiz.webapp.event.CoreEvents;
52 import org.ofbiz.service.GenericServiceException;
53
54
55 /**
56  * CompDocEvents Class
57  *
58  * @author <a HREF="mailto:byersa@automationgroups.com">Al Byers</a>
59  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
60  */

61 public class CompDocEvents {
62     public static final String JavaDoc module = CompDocEvents.class.getName();
63     
64     /**
65      *
66      * @param request
67      * @param response
68      * @return
69      *
70      * Creates the topmost Content entity of a Composite Document tree.
71      * Also creates an "empty" Composite Document Instance Content entity.
72      * Creates ContentRevision/Item records for each, as well.
73      */

74
75     public static String JavaDoc persistRootCompDoc(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
76         Map JavaDoc paramMap = UtilHttp.getParameterMap(request);
77         GenericDelegator delegator = (GenericDelegator)request.getAttribute("delegator");
78         LocalDispatcher dispatcher = (LocalDispatcher)request.getAttribute("dispatcher");
79         Locale JavaDoc locale = UtilHttp.getLocale(request);
80         HttpSession JavaDoc session = request.getSession();
81         //Security security = (Security)request.getAttribute("security");
82
GenericValue userLogin = (GenericValue)session.getAttribute("userLogin");
83         String JavaDoc contentId = (String JavaDoc)paramMap.get("contentId");
84         //String instanceContentId = null;
85

86         boolean contentExists = true;
87         if (UtilValidate.isEmpty(contentId)) {
88             contentExists = false;
89         } else {
90             try {
91                 GenericValue val = delegator.findByPrimaryKey("Content", UtilMisc.toMap("contentId", contentId));
92                 if (val == null) contentExists = false;
93             } catch(GenericEntityException e) {
94                 Debug.logError(e, "Error running serviceName persistContentAndAssoc", module);
95                 String JavaDoc errMsg = UtilProperties.getMessage(CoreEvents.err_resource, "coreEvents.error_modelservice_for_srv_name", locale);
96                 request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg + " [" + "persistContentAndAssoc" + "]: " + e.toString());
97                 return "error";
98            }
99         }
100         
101         ModelService modelService = null;
102         try {
103             modelService = dispatcher.getDispatchContext().getModelService("persistContentAndAssoc");
104         } catch (GenericServiceException e) {
105             String JavaDoc errMsg = "Error getting model service for serviceName, 'persistContentAndAssoc'. " + e.toString();
106             Debug.logError(errMsg, module);
107             request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg + "</li>");
108             return "error";
109         }
110         Map JavaDoc persistMap = modelService.makeValid(paramMap, ModelService.IN_PARAM);
111         persistMap.put("userLogin", userLogin);
112         try {
113             Map JavaDoc persistResult = dispatcher.runSync("persistContentAndAssoc", persistMap);
114             contentId = (String JavaDoc)persistResult.get("contentId");
115             //request.setAttribute("contentId", contentId);
116
Set JavaDoc keySet = persistResult.keySet();
117             Iterator JavaDoc iter = keySet.iterator();
118             while (iter.hasNext()) {
119                 Object JavaDoc obj = iter.next();
120                 Object JavaDoc val = persistResult.get(obj);
121                 request.setAttribute(obj.toString(), val);
122             }
123             // Update ContentRevision and ContentRevisonItem
124
Map JavaDoc contentRevisionMap = new HashMap JavaDoc();
125             contentRevisionMap.put("itemContentId", contentId);
126             contentRevisionMap.put("contentId", contentId);
127             contentRevisionMap.put("userLogin", userLogin);
128             Map JavaDoc result = dispatcher.runSync("persistContentRevisionAndItem", contentRevisionMap);
129             keySet = result.keySet();
130             iter = keySet.iterator();
131             while (iter.hasNext()) {
132                 Object JavaDoc obj = iter.next();
133                 Object JavaDoc val = persistResult.get(obj);
134                 request.setAttribute(obj.toString(), val);
135             }
136             String JavaDoc errorMsg = ServiceUtil.getErrorMessage(result);
137             if (UtilValidate.isNotEmpty(errorMsg)) {
138                 String JavaDoc errMsg = "Error running serviceName, 'persistContentRevisionAndItem'. " + errorMsg;
139                 Debug.logError(errMsg, module);
140                 request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg + "</li>");
141                 return "error";
142             }
143             
144         } catch(GenericServiceException e) {
145             String JavaDoc errMsg = "Error running serviceName, 'persistContentAndAssoc'. " + e.toString();
146             Debug.logError(errMsg, module);
147             request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg + "</li>");
148             return "error";
149         }
150         return "success";
151     }
152     
153     public static String JavaDoc padNumberWithLeadingZeros(Long JavaDoc num, Integer JavaDoc padLen) {
154         String JavaDoc s = UtilFormatOut.formatPaddedNumber(num.longValue(), padLen.intValue());
155         return s;
156     }
157
158     public static String JavaDoc genCompDocPdf(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
159         String JavaDoc responseStr = "success";
160         //ByteWrapper byteWrapper = null;
161
HttpSession JavaDoc session = request.getSession();
162         GenericValue userLogin = (GenericValue)session.getAttribute("userLogin");
163         ServletContext JavaDoc servletContext = session.getServletContext();
164         LocalDispatcher dispatcher = (LocalDispatcher)request.getAttribute("dispatcher");
165         Map JavaDoc paramMap = UtilHttp.getParameterMap(request);
166         String JavaDoc contentId = (String JavaDoc)paramMap.get("contentId");
167         Locale JavaDoc locale = UtilHttp.getLocale(request);
168         String JavaDoc rootDir = null;
169         String JavaDoc webSiteId = null;
170         String JavaDoc https = null;
171         
172         if (UtilValidate.isEmpty(rootDir)) {
173             rootDir = servletContext.getRealPath("/");
174         }
175         if (UtilValidate.isEmpty(webSiteId)) {
176             webSiteId = (String JavaDoc) servletContext.getAttribute("webSiteId");
177         }
178         if (UtilValidate.isEmpty(https)) {
179             https = (String JavaDoc) servletContext.getAttribute("https");
180         }
181         
182         Map JavaDoc mapIn = new HashMap JavaDoc();
183         mapIn.put("contentId", contentId);
184         mapIn.put("locale", locale);
185         mapIn.put("rootDir", rootDir);
186         mapIn.put("webSiteId", webSiteId);
187         mapIn.put("https", https);
188         mapIn.put("userLogin", userLogin);
189         
190         Map JavaDoc results = null;
191         try {
192             results = dispatcher.runSync("renderCompDocPdf", mapIn);
193         } catch(ServiceAuthException e) {
194             request.setAttribute("_ERROR_MESSAGE_", e.toString());
195             return "error";
196         } catch(GenericServiceException e) {
197             request.setAttribute("_ERROR_MESSAGE_", e.toString());
198             return "error";
199         } catch(Exception JavaDoc e) {
200             request.setAttribute("_ERROR_MESSAGE_", e.toString());
201             return "error";
202         }
203         
204         if (ServiceUtil.isError(results)) {
205             request.setAttribute("_ERROR_MESSAGE_", ServiceUtil.getErrorMessage(results));
206             return "error";
207         }
208         
209         ByteWrapper outByteWrapper = (ByteWrapper)results.get("outByteWrapper");
210
211         // setup content type
212
String JavaDoc contentType = "application/pdf; charset=ISO-8859-1";
213
214         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(outByteWrapper.getBytes());
215         
216         /*
217         try {
218             FileOutputStream fos = new FileOutputStream("/home/byersa/pdftest.pdf");
219             fos.write(outByteWrapper.getBytes());
220         } catch(FileNotFoundException e) {
221         } catch(IOException e) {
222         }
223         */

224         try {
225             UtilHttp.streamContentToBrowser(response, bais, outByteWrapper.getLength(), contentType);
226         } catch(IOException JavaDoc e) {
227             request.setAttribute("_ERROR_MESSAGE_", e.toString());
228             return "error";
229         }
230         return responseStr;
231     }
232     public static String JavaDoc genContentPdf(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
233         String JavaDoc responseStr = "success";
234         //ByteWrapper byteWrapper = null;
235
HttpSession JavaDoc session = request.getSession();
236         GenericValue userLogin = (GenericValue)session.getAttribute("userLogin");
237         ServletContext JavaDoc servletContext = session.getServletContext();
238         LocalDispatcher dispatcher = (LocalDispatcher)request.getAttribute("dispatcher");
239         Map JavaDoc paramMap = UtilHttp.getParameterMap(request);
240         String JavaDoc contentId = (String JavaDoc)paramMap.get("contentId");
241         Locale JavaDoc locale = UtilHttp.getLocale(request);
242         String JavaDoc rootDir = null;
243         String JavaDoc webSiteId = null;
244         String JavaDoc https = null;
245         
246         if (UtilValidate.isEmpty(rootDir)) {
247             rootDir = servletContext.getRealPath("/");
248         }
249         if (UtilValidate.isEmpty(webSiteId)) {
250             webSiteId = (String JavaDoc) servletContext.getAttribute("webSiteId");
251         }
252         if (UtilValidate.isEmpty(https)) {
253             https = (String JavaDoc) servletContext.getAttribute("https");
254         }
255         
256         Map JavaDoc mapIn = new HashMap JavaDoc();
257         mapIn.put("contentId", contentId);
258         mapIn.put("locale", locale);
259         mapIn.put("rootDir", rootDir);
260         mapIn.put("webSiteId", webSiteId);
261         mapIn.put("https", https);
262         mapIn.put("userLogin", userLogin);
263         
264         Map JavaDoc results = null;
265         try {
266             results = dispatcher.runSync("renderContentPdf", mapIn);
267         } catch(ServiceAuthException e) {
268             request.setAttribute("_ERROR_MESSAGE_", e.toString());
269             return "error";
270         } catch(GenericServiceException e) {
271             request.setAttribute("_ERROR_MESSAGE_", e.toString());
272             return "error";
273         } catch(Exception JavaDoc e) {
274             request.setAttribute("_ERROR_MESSAGE_", e.toString());
275             return "error";
276         }
277         
278         if (ServiceUtil.isError(results)) {
279             request.setAttribute("_ERROR_MESSAGE_", ServiceUtil.getErrorMessage(results));
280             return "error";
281         }
282         
283         ByteWrapper outByteWrapper = (ByteWrapper)results.get("outByteWrapper");
284
285         // setup content type
286
String JavaDoc contentType = "application/pdf; charset=ISO-8859-1";
287
288         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(outByteWrapper.getBytes());
289         
290         /*
291         try {
292             FileOutputStream fos = new FileOutputStream("/home/byersa/pdftest.pdf");
293             fos.write(outByteWrapper.getBytes());
294             fos.close();
295         } catch(FileNotFoundException e) {
296         } catch(IOException e) {
297         }
298         */

299         try {
300             UtilHttp.streamContentToBrowser(response, bais, outByteWrapper.getLength(), contentType);
301         } catch(IOException JavaDoc e) {
302             request.setAttribute("_ERROR_MESSAGE_", e.toString());
303             return "error";
304         }
305         return responseStr;
306     }
307 }
308
Popular Tags