KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > wstore > InvoiceServlet


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Smart Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2003 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.wstore;
15
16 import javax.servlet.*;
17 import javax.servlet.http.*;
18 import java.io.*;
19 import java.util.*;
20 import java.sql.*;
21
22 import org.apache.ecs.*;
23 import org.apache.ecs.xhtml.*;
24 import org.apache.log4j.Logger;
25
26 import org.compiere.util.*;
27 import org.compiere.model.*;
28 import org.compiere.www.*;
29
30
31 /**
32  * Check Out.
33  *
34  * @author Jorg Janke
35  * @version $Id: InvoiceServlet.java,v 1.4 2003/07/24 03:36:41 jjanke Exp $
36  */

37 public class InvoiceServlet extends HttpServlet
38 {
39     /** Logging */
40     private Logger log = Logger.getLogger(getClass());
41     /** Name */
42     static public final String JavaDoc NAME = "invoiceServlet";
43
44     /**
45      * Initialize global variables
46      *
47      * @param config Configuration
48      * @throws ServletException
49      */

50     public void init(ServletConfig config)
51         throws ServletException
52     {
53         super.init(config);
54         if (!WEnv.initWeb(config))
55             throw new ServletException("InvoiceServlet.init");
56     } // init
57

58     /**
59      * Get Servlet information
60      * @return Info
61      */

62     public String JavaDoc getServletInfo()
63     {
64         return "Compiere Web Invoice Servlet";
65     } // getServletInfo
66

67     /**
68      * Clean up resources
69      */

70     public void destroy()
71     {
72         log.debug("destroy");
73     } // destroy
74

75
76     /**
77      * Process the HTTP Get request.
78      * (logout, deleteCookie)
79      * Sends Web Request Page
80      *
81      * @param request request
82      * @param response response
83      * @throws ServletException
84      * @throws IOException
85      */

86     public void doGet(HttpServletRequest request, HttpServletResponse response)
87         throws ServletException, IOException
88     {
89         log.info("doGet from " + request.getRemoteHost() + " - " + request.getRemoteAddr());
90
91         String JavaDoc url = "invoice.jsp";
92         //
93
HttpSession session = request.getSession(false);
94         session.removeAttribute(JSPEnv.HDR_MESSAGE);
95         if (session == null || session.getAttribute(Info.NAME) == null)
96             url = "login.jsp";
97         else
98         {
99             Info info = (Info)session.getAttribute(Info.NAME);
100             if (info != null)
101                 info.setMessage("");
102
103             // Parameter = Invoice_ID - if invoice is valid and belongs to wu then create PDF & stream it
104
String JavaDoc msg = streamInvoice(request, response);
105             if (msg == null || msg.length() == 0)
106                 return;
107             if (info != null)
108                 info.setMessage(msg);
109         }
110
111         log.info ("doGet - Forward to " + url);
112         RequestDispatcher dispatcher = getServletContext ().getRequestDispatcher (url);
113         dispatcher.forward (request, response);
114     } // doGet
115

116     /**
117      * Process the HTTP Post request
118      *
119      * @param request request
120      * @param response response
121      * @throws ServletException
122      * @throws IOException
123      */

124     public void doPost(HttpServletRequest request, HttpServletResponse response)
125         throws ServletException, IOException
126     {
127         log.info("doPost from " + request.getRemoteHost() + " - " + request.getRemoteAddr());
128         HttpSession session = request.getSession(false);
129     } // doPost
130

131     /**
132      * Stream invoice
133      * @param request request
134      * @param response response
135      * @return "" or error message
136      */

137     private String JavaDoc streamInvoice (HttpServletRequest request, HttpServletResponse response)
138     {
139         // Get Invoice ID
140
String JavaDoc invoiceString = request.getParameter("Invoice_ID");
141         if (invoiceString == null || invoiceString.length() == 0)
142             return "";
143         int C_Invoice_ID = 0;
144         try
145         {
146             C_Invoice_ID = Integer.parseInt (invoiceString);
147         }
148         catch (NumberFormatException JavaDoc ex)
149         {
150             log.debug("streamInvoice - " + ex);
151         }
152         if (C_Invoice_ID == 0)
153         {
154             log.debug("streamInvoice - no ID)");
155             return "No Invoice ID";
156         }
157
158         // Get Invoice
159
Properties ctx = JSPEnv.getCtx(request);
160         HttpSession session = request.getSession(true);
161         MInvoice invoice = new MInvoice(ctx, C_Invoice_ID);
162         if (invoice.getC_Invoice_ID() != C_Invoice_ID)
163         {
164             log.debug("streamInvoice - Invoice not found - ID=" + C_Invoice_ID);
165             return "Invoice not found";
166         }
167         // Get WebUser & Compare with invoice
168
WebUser wu = (WebUser)session.getAttribute(WebUser.NAME);
169         if (wu.getC_BPartner_ID() != invoice.getC_BPartner_ID())
170         {
171             log.warn ("streamInvoice - Invoice from BPartner - C_Invoice_ID="
172                 + C_Invoice_ID + " - BP_Invoice=" + invoice.getC_BPartner_ID()
173                 + " = BP_User=" + wu.getC_BPartner_ID());
174             return "Your invoice not found";
175         }
176
177         // Check if Invoice already created
178
String JavaDoc dirName = ctx.getProperty("documentDir", ".");
179         try
180         {
181             File dir = new File (dirName);
182             if (!dir.exists ())
183                 dir.mkdir ();
184         }
185         catch (Exception JavaDoc ex)
186         {
187             log.error("streamInvoice - Could not create directory " + dirName, ex);
188             return "Streaming error - directory";
189         }
190         String JavaDoc fileName = invoice.getPDFFileName(dirName);
191         File file = new File(fileName);
192         if (!file.exists()) // create Invoice PDF
193
{
194             file = invoice.getPDF (file);
195             if (file != null)
196             {
197                 invoice.setDatePrinted (new Timestamp(System.currentTimeMillis()));
198                 invoice.save();
199             }
200         }
201         if (file == null || !file.exists())
202         {
203             log.warn("streamInvoice - File does not exist - " + file);
204             return "Streaming error - file";
205         }
206
207         // Send PDF
208
try
209         {
210             response.setContentType("application/pdf");
211             int length = 2048; // 2k Buffer
212
int fileLength = (int)file.length();
213             response.setBufferSize(length);
214         // response.setContentLength(fileLength);
215
//
216
log.debug("streamInvoice - " + file.getAbsolutePath() + ", length=" + fileLength);
217             long time = System.currentTimeMillis();
218             //
219
FileInputStream in = new FileInputStream (file);
220             ServletOutputStream out = response.getOutputStream ();
221             byte[] buffer = new byte[length];
222             int totalSize = 0;
223             int count = 0;
224             do
225             {
226                 count = in.read(buffer, 0, length);
227                 if (count > 0)
228                 {
229                     totalSize += count;
230                     out.write (buffer, 0, count);
231                 }
232             } while (count != -1);
233             out.flush();
234             out.close();
235             //
236
in.close();
237             time = System.currentTimeMillis() - time;
238             float speed = ((float)totalSize/1024) / ((float)time/1000);
239             log.debug("streamInvoice - " + totalSize + " B - " + time + " ms - " + speed + " kB/sec");
240         }
241         catch (IOException ex)
242         {
243             log.error("streamInvoice - " + ex);
244             return "Streaming error";
245         }
246
247         return null;
248     } // streamInvoice
249

250 } // InvoiceServlet
251
Popular Tags