KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > shipment > packing > PackingServices


1 /*
2  * $Id$
3  *
4  * Copyright (c) 2001-2005 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  */

25 package org.ofbiz.shipment.packing;
26
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.ofbiz.base.util.Debug;
31 import org.ofbiz.base.util.GeneralException;
32 import org.ofbiz.base.util.UtilValidate;
33 import org.ofbiz.service.DispatchContext;
34 import org.ofbiz.service.ServiceUtil;
35
36 /**
37  *
38  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
39  * @version $Rev: 6764 $
40  * @since Sep 1, 2005
41  */

42 public class PackingServices {
43
44     public static final String JavaDoc module = PackingServices.class.getName();
45
46     public static Map JavaDoc addPackLine(DispatchContext dctx, Map JavaDoc context) {
47         PackingSession session = (PackingSession) context.get("packingSession");
48         String JavaDoc shipGroupSeqId = (String JavaDoc) context.get("shipGroupSeqId");
49         String JavaDoc orderId = (String JavaDoc) context.get("orderId");
50         String JavaDoc productId = (String JavaDoc) context.get("productId");
51         Double JavaDoc quantity = (Double JavaDoc) context.get("quantity");
52         Integer JavaDoc packageSeq = (Integer JavaDoc) context.get("packageSeq");
53
54         // set the instructions -- will clear out previous if now null
55
String JavaDoc instructions = (String JavaDoc) context.get("handlingInstructions");
56         session.setHandlingInstructions(instructions);
57
58         if (quantity == null) {
59             quantity = new Double JavaDoc(1);
60         }
61
62         Debug.log("Pack input [" + productId + "] @ [" + quantity + "]", module);
63         
64         try {
65             session.addOrIncreaseLine(orderId, null, shipGroupSeqId, productId, quantity.doubleValue(), packageSeq.intValue());
66         } catch (GeneralException e) {
67             Debug.logError(e, module);
68             return ServiceUtil.returnError(e.getMessage());
69         }
70
71         return ServiceUtil.returnSuccess();
72     }
73
74     public static Map JavaDoc packBulk(DispatchContext dctx, Map JavaDoc context) {
75         PackingSession session = (PackingSession) context.get("packingSession");
76         String JavaDoc orderId = (String JavaDoc) context.get("orderId");
77         String JavaDoc shipGroupSeqId = (String JavaDoc) context.get("shipGroupSeqId");
78
79         // set the instructions -- will clear out previous if now null
80
String JavaDoc instructions = (String JavaDoc) context.get("handlingInstructions");
81         session.setHandlingInstructions(instructions);
82
83         Map JavaDoc prdInfo = (Map JavaDoc) context.get("prdInfo");
84         Map JavaDoc qtyInfo = (Map JavaDoc) context.get("qtyInfo");
85         Map JavaDoc pkgInfo = (Map JavaDoc) context.get("pkgInfo");
86         Map JavaDoc selInfo = (Map JavaDoc) context.get("selInfo");
87         if (selInfo != null) {
88             Iterator JavaDoc i = selInfo.keySet().iterator();
89             while (i.hasNext()) {
90                 String JavaDoc orderItemSeqId = (String JavaDoc) i.next();
91                 String JavaDoc qtyStr = (String JavaDoc) qtyInfo.get(orderItemSeqId);
92                 String JavaDoc pkgStr = (String JavaDoc) pkgInfo.get(orderItemSeqId);
93                 String JavaDoc prdStr = (String JavaDoc) prdInfo.get(orderItemSeqId);
94                 if (UtilValidate.isEmpty(prdStr)) {
95                     // set the productId to null if empty
96
prdStr = null;
97                 }
98                 Debug.log("Item: " + orderItemSeqId + " / Product: " + prdStr + " / Quantity: " + qtyStr + " / Package: " + pkgStr, module);
99
100                 double quantity = 0;
101                 int packageSeq = 0;
102                 try {
103                     quantity = Double.parseDouble(qtyStr);
104                     packageSeq = Integer.parseInt(pkgStr);
105                 } catch (Exception JavaDoc e) {
106                     return ServiceUtil.returnError(e.getMessage());
107                 }
108
109                 try {
110                     session.addOrIncreaseLine(orderId, orderItemSeqId, shipGroupSeqId, prdStr, quantity, packageSeq);
111                 } catch (GeneralException e) {
112                     Debug.logError(e, module);
113                     return ServiceUtil.returnError(e.getMessage());
114                 }
115             }
116         }
117
118         return ServiceUtil.returnSuccess();
119     }
120
121     public static Map JavaDoc incrementPackageSeq(DispatchContext dctx, Map JavaDoc context) {
122         PackingSession session = (PackingSession) context.get("packingSession");
123         int nextSeq = session.nextPackageSeq();
124         Map JavaDoc result = ServiceUtil.returnSuccess();
125         result.put("nextPackageSeq", new Integer JavaDoc(nextSeq));
126         return result;
127     }
128
129     public static Map JavaDoc clearPackLine(DispatchContext dctx, Map JavaDoc context) {
130         PackingSession session = (PackingSession) context.get("packingSession");
131         String JavaDoc orderId = (String JavaDoc) context.get("orderId");
132         String JavaDoc orderItemSeqId = (String JavaDoc) context.get("orderItemSeqId");
133         String JavaDoc shipGroupSeqId = (String JavaDoc) context.get("shipGroupSeqId");
134         String JavaDoc inventoryItemId = (String JavaDoc) context.get("inventoryItemId");
135         Integer JavaDoc packageSeqId = (Integer JavaDoc) context.get("packageSeqId");
136
137         PackingSessionLine line = session.findLine(orderId, orderItemSeqId, shipGroupSeqId,
138                 inventoryItemId, packageSeqId.intValue());
139
140         // remove the line
141
if (line != null) {
142             session.clearLine(line);
143         } else {
144             return ServiceUtil.returnError("Packing line item not found; cannot clear.");
145         }
146
147         return ServiceUtil.returnSuccess();
148     }
149
150     public static Map JavaDoc clearPackAll(DispatchContext dctx, Map JavaDoc context) {
151         PackingSession session = (PackingSession) context.get("packingSession");
152         session.clear();
153
154         return ServiceUtil.returnSuccess();
155     }
156
157
158     public static Map JavaDoc completePack(DispatchContext dctx, Map JavaDoc context) {
159         PackingSession session = (PackingSession) context.get("packingSession");
160
161         // set the instructions -- will clear out previous if now null
162
String JavaDoc instructions = (String JavaDoc) context.get("handlingInstructions");
163         session.setHandlingInstructions(instructions);
164
165         Boolean JavaDoc force = (Boolean JavaDoc) context.get("forceComplete");
166         if (force == null) {
167             force = Boolean.FALSE;
168         }
169
170         String JavaDoc shipmentId = null;
171         try {
172             shipmentId = session.complete(force.booleanValue());
173         } catch (GeneralException e) {
174             Debug.logError(e, module);
175             return ServiceUtil.returnError(e.getMessage(), e.getMessageList());
176         }
177
178         Map JavaDoc resp;
179         if ("EMPTY".equals(shipmentId)) {
180             resp = ServiceUtil.returnError("No items currently set to be shipped. Cannot complete!");
181         } else {
182             resp = ServiceUtil.returnSuccess("Shipment #" + shipmentId + " created and marked as PACKED.");
183         }
184         
185         resp.put("shipmentId", shipmentId);
186         return resp;
187     }
188 }
189
Popular Tags