KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > shipment > shipment > ShipmentEvents


1 /*
2  * $Id: ShipmentEvents.java 5631 2005-09-02 21:38:26Z sichen $
3  *
4  * Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.shipment.shipment;
25
26 import java.io.IOException JavaDoc;
27
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.http.HttpServletResponse JavaDoc;
30
31 import org.ofbiz.base.util.Debug;
32 import org.ofbiz.base.util.UtilHttp;
33 import org.ofbiz.base.util.UtilMisc;
34 import org.ofbiz.entity.GenericDelegator;
35 import org.ofbiz.entity.GenericEntityException;
36 import org.ofbiz.entity.GenericValue;
37 import org.ofbiz.entity.util.ByteWrapper;
38
39 /**
40  * ShippingEvents - Events used for processing shipping fees
41  *
42  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
43  * @version $Rev: 5631 $
44  * @since 2.2
45  */

46 public class ShipmentEvents {
47     
48     public static final String JavaDoc module = ShipmentEvents.class.getName();
49
50     public static String JavaDoc viewShipmentPackageRouteSegLabelImage(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
51         
52         GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
53         
54         String JavaDoc shipmentId = request.getParameter("shipmentId");
55         String JavaDoc shipmentRouteSegmentId = request.getParameter("shipmentRouteSegmentId");
56         String JavaDoc shipmentPackageSeqId = request.getParameter("shipmentPackageSeqId");
57         
58         GenericValue shipmentPackageRouteSeg = null;
59         try {
60             shipmentPackageRouteSeg = delegator.findByPrimaryKey("ShipmentPackageRouteSeg", UtilMisc.toMap("shipmentId", shipmentId, "shipmentRouteSegmentId", shipmentRouteSegmentId, "shipmentPackageSeqId", shipmentPackageSeqId));
61         } catch (GenericEntityException e) {
62             String JavaDoc errorMsg = "Error looking up ShipmentPackageRouteSeg: " + e.toString();
63             Debug.logError(e, errorMsg, module);
64             request.setAttribute("_ERROR_MESSAGE_", errorMsg);
65             return "error";
66         }
67         
68         if (shipmentPackageRouteSeg == null) {
69             request.setAttribute("_ERROR_MESSAGE_", "Could not find ShipmentPackageRouteSeg where shipmentId=[" + shipmentId + "], shipmentRouteSegmentId=[" + shipmentRouteSegmentId + "], shipmentPackageSeqId=[" + shipmentPackageSeqId + "]");
70             return "error";
71         }
72         
73         byte[] bytes = shipmentPackageRouteSeg.getBytes("labelImage");
74         if (bytes == null || bytes.length == 0) {
75             request.setAttribute("_ERROR_MESSAGE_", "The ShipmentPackageRouteSeg was found where shipmentId=[" + shipmentId + "], shipmentRouteSegmentId=[" + shipmentRouteSegmentId + "], shipmentPackageSeqId=[" + shipmentPackageSeqId + "], but there was no labelImage on the value.");
76             return "error";
77         }
78         
79         // TODO: record the image format somehow to make this block nicer. Right now we're just trying GIF first as a default, then if it doesn't work, trying PNG.
80
// It would be nice to store the actual type of the image alongside the image data.
81
try {
82             UtilHttp.streamContentToBrowser(response, bytes, "image/gif");
83         } catch (IOException JavaDoc e1) {
84             try {
85                 UtilHttp.streamContentToBrowser(response, bytes, "image/png");
86             } catch (IOException JavaDoc e2) {
87                 String JavaDoc errorMsg = "Error writing labelImage to OutputStream: " + e2.toString();
88                 Debug.logError(e2, errorMsg, module);
89                 request.setAttribute("_ERROR_MESSAGE_", errorMsg);
90                 return "error";
91             }
92         }
93         
94         return "success";
95     }
96 }
97
98
Popular Tags