KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > order > order > OrderEvents


1 /*
2  * $Id: $
3  *
4  * Copyright 2006-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.order.order;
19
20 import java.io.IOException JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.util.List JavaDoc;
23
24 import javax.servlet.ServletContext JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27 import javax.servlet.http.HttpSession JavaDoc;
28
29 import org.ofbiz.base.util.Debug;
30 import org.ofbiz.base.util.GeneralException;
31 import org.ofbiz.base.util.UtilHttp;
32 import org.ofbiz.base.util.UtilMisc;
33 import org.ofbiz.content.data.DataResourceWorker;
34 import org.ofbiz.entity.GenericDelegator;
35 import org.ofbiz.entity.GenericEntityException;
36 import org.ofbiz.entity.GenericValue;
37
38 /**
39  * Order Events
40  *
41  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
42  */

43 public class OrderEvents {
44
45     public static final String JavaDoc module = OrderEvents.class.getName();
46
47     public static String JavaDoc downloadDigitalProduct(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
48         HttpSession JavaDoc session = request.getSession();
49         ServletContext JavaDoc application = session.getServletContext();
50         GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
51         GenericValue userLogin = (GenericValue) session.getAttribute("userLogin");
52         String JavaDoc dataResourceId = request.getParameter("dataResourceId");
53         
54         try {
55             // has the userLogin.partyId ordered a product with DIGITAL_DOWNLOAD content associated for the given dataResourceId?
56
List JavaDoc orderRoleAndProductContentInfoList = delegator.findByAnd("OrderRoleAndProductContentInfo",
57                     UtilMisc.toMap("partyId", userLogin.get("partyId"), "dataResourceId", dataResourceId, "productContentTypeId", "DIGITAL_DOWNLOAD", "statusId", "ITEM_COMPLETED"));
58             
59             if (orderRoleAndProductContentInfoList.size() == 0) {
60                 request.setAttribute("_ERROR_MESSAGE_", "No record of purchase for digital download found (dataResourceId=[" + dataResourceId + "]).");
61                 return "error";
62             }
63             
64             GenericValue orderRoleAndProductContentInfo = (GenericValue) orderRoleAndProductContentInfoList.get(0);
65             
66             // TODO: check validity based on ProductContent fields: useCountLimit, useTime/useTimeUomId
67

68             if (orderRoleAndProductContentInfo.getString("mimeTypeId") != null) {
69                 response.setContentType(orderRoleAndProductContentInfo.getString("mimeTypeId"));
70             }
71             OutputStream JavaDoc os = response.getOutputStream();
72             DataResourceWorker.streamDataResource(os, delegator, dataResourceId, "", application.getInitParameter("webSiteId"), UtilHttp.getLocale(request), application.getRealPath("/"));
73             os.flush();
74         } catch (GenericEntityException e) {
75             String JavaDoc errMsg = "Error downloading digital product content: " + e.toString();
76             Debug.logError(e, errMsg, module);
77             request.setAttribute("_ERROR_MESSAGE_", errMsg);
78             return "error";
79         } catch (GeneralException e) {
80             String JavaDoc errMsg = "Error downloading digital product content: " + e.toString();
81             Debug.logError(e, errMsg, module);
82             request.setAttribute("_ERROR_MESSAGE_", errMsg);
83             return "error";
84         } catch (IOException JavaDoc e) {
85             String JavaDoc errMsg = "Error downloading digital product content: " + e.toString();
86             Debug.logError(e, errMsg, module);
87             request.setAttribute("_ERROR_MESSAGE_", errMsg);
88             return "error";
89         }
90         
91         return "success";
92     }
93 }
94
Popular Tags