KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > pos > device > DeviceLoader


1 /*
2  * $Id: DeviceLoader.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 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.pos.device;
26
27 import java.util.Map JavaDoc;
28
29 import jpos.JposException;
30
31 import org.ofbiz.base.container.ContainerConfig;
32 import org.ofbiz.base.util.Debug;
33 import org.ofbiz.base.util.GeneralException;
34 import org.ofbiz.base.util.UtilValidate;
35 import org.ofbiz.pos.device.impl.CashDrawer;
36 import org.ofbiz.pos.device.impl.CheckScanner;
37 import org.ofbiz.pos.device.impl.Journal;
38 import org.ofbiz.pos.device.impl.Keyboard;
39 import org.ofbiz.pos.device.impl.LineDisplay;
40 import org.ofbiz.pos.device.impl.Msr;
41 import org.ofbiz.pos.device.impl.PinPad;
42 import org.ofbiz.pos.device.impl.Receipt;
43 import org.ofbiz.pos.device.impl.Scanner;
44
45 /**
46  *
47  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
48  * @version $Rev: 5462 $
49  * @since 3.2
50  */

51 public class DeviceLoader {
52
53     public static final String JavaDoc module = DeviceLoader.class.getName();
54
55     public static CashDrawer[] drawer = null;
56     public static CheckScanner check = null;
57     public static Journal journal = null;
58     public static Keyboard keyboard = null;
59     public static LineDisplay ldisplay = null;
60     public static Msr msr = null;
61     public static PinPad pinpad = null;
62     public static Receipt receipt = null;
63     public static Scanner scanner = null;
64
65     public static void load(Map JavaDoc devices) throws GeneralException {
66         // load the keyboard
67
if (devices.get("Keyboard") != null) {
68             String JavaDoc keyboardDevice = ((ContainerConfig.Container.Property) devices.get("Keyboard")).value;
69             if (UtilValidate.isNotEmpty(keyboardDevice) && !"[NOT IMPLEMENTED]".equals(keyboardDevice)) {
70                 keyboard = new Keyboard(keyboardDevice, -1);
71                 try {
72                     keyboard.open();
73                 } catch (jpos.JposException jpe) {
74                     Debug.logError(jpe, "JPOS Exception", module);
75                     throw new GeneralException(jpe.getOrigException());
76                 }
77             }
78         }
79
80         // load the scanner
81
if (devices.get("Scanner") != null) {
82             String JavaDoc scannerDevice = ((ContainerConfig.Container.Property) devices.get("Scanner")).value;
83             if (UtilValidate.isNotEmpty(scannerDevice) && !"[NOT IMPLEMENTED]".equals(scannerDevice)) {
84                 scanner = new Scanner(scannerDevice, -1);
85                 try {
86                     scanner.open();
87                 } catch (jpos.JposException jpe) {
88                     Debug.logError(jpe, "JPOS Exception", module);
89                     throw new GeneralException(jpe.getOrigException());
90                 }
91             }
92         }
93
94         // load the check reader
95
if (devices.get("CheckScanner") != null) {
96             String JavaDoc checkScannerDevice = ((ContainerConfig.Container.Property) devices.get("CheckScanner")).value;
97             if (UtilValidate.isNotEmpty(checkScannerDevice) && !"[NOT IMPLEMENTED]".equals(checkScannerDevice)) {
98                 check = new CheckScanner(checkScannerDevice, -1);
99                 try {
100                     check.open();
101                 } catch (jpos.JposException jpe) {
102                     Debug.logError(jpe, "JPOS Exception", module);
103                     throw new GeneralException(jpe.getOrigException());
104                 }
105             }
106         }
107
108         // load the msr
109
if (devices.get("Msr") != null) {
110             String JavaDoc msrDevice = ((ContainerConfig.Container.Property) devices.get("Msr")).value;
111             if (UtilValidate.isNotEmpty(msrDevice) && !"[NOT IMPLEMENTED]".equals(msrDevice)) {
112                 msr = new Msr(msrDevice, -1);
113                 try {
114                     msr.open();
115                 } catch (jpos.JposException jpe) {
116                     Debug.logError(jpe, "JPOS Exception", module);
117                     throw new GeneralException(jpe.getOrigException());
118                 }
119             }
120         }
121
122         // load the receipt printer
123
if (devices.get("Receipt") != null) {
124             String JavaDoc receiptDevice = ((ContainerConfig.Container.Property) devices.get("Receipt")).value;
125             if (UtilValidate.isNotEmpty(receiptDevice) && !"[NOT IMPLEMENTED]".equals(receiptDevice)) {
126                 receipt = new Receipt(receiptDevice, -1);
127                 try {
128                     receipt.open();
129                 } catch (jpos.JposException jpe) {
130                     Debug.logError(jpe, "JPOS Exception", module);
131                     throw new GeneralException(jpe.getOrigException());
132                 }
133             }
134         }
135
136         // load the journal printer
137
if (devices.get("Journal") != null) {
138             String JavaDoc journalDevice = ((ContainerConfig.Container.Property) devices.get("Journal")).value;
139             if (UtilValidate.isNotEmpty(journalDevice) && !"[NOT IMPLEMENTED]".equals(journalDevice)) {
140                 journal = new Journal(journalDevice, -1);
141                 try {
142                     journal.open();
143                 } catch (jpos.JposException jpe) {
144                     Debug.logError(jpe, "JPOS Exception", module);
145                     throw new GeneralException(jpe.getOrigException());
146                 }
147             }
148         }
149
150         // load the line display
151
if (devices.get("LineDisplay") != null) {
152             String JavaDoc lineDisplayDevice = ((ContainerConfig.Container.Property) devices.get("LineDisplay")).value;
153             if (UtilValidate.isNotEmpty(lineDisplayDevice) && !"[NOT IMPLEMENTED]".equals(lineDisplayDevice)) {
154                 ldisplay = new LineDisplay(lineDisplayDevice, -1);
155                 try {
156                     ldisplay.open();
157                 } catch (jpos.JposException jpe) {
158                     Debug.logError(jpe, "JPOS Exception", module);
159                     throw new GeneralException(jpe.getOrigException());
160                 }
161             }
162         }
163
164         // load the cash drawer(s) -- Currently only supports one drawer per terminal
165
for (int i = 1; i < 10; i++) { // more than 10 cash drawers on a terminal??
166
String JavaDoc idName = "CashDrawer." + i;
167             if (devices.get(idName) != null) {
168                 String JavaDoc cashDrawerDevice = ((ContainerConfig.Container.Property) devices.get(idName)).value;
169                 if (UtilValidate.isNotEmpty(cashDrawerDevice) && !"[NOT IMPLEMENTED]".equals(cashDrawerDevice)) {
170                     if (drawer == null) {
171                         drawer = new CashDrawer[10];
172                     }
173
174                     // create the instance
175
drawer[i-1] = new CashDrawer(cashDrawerDevice, -1);
176                     try {
177                         drawer[i-1].open();
178                     } catch (jpos.JposException jpe) {
179                         Debug.logError(jpe, "JPOS Exception", module);
180                         throw new GeneralException(jpe.getOrigException());
181                     }
182                 }
183             }
184         }
185     }
186
187     public static void enable(boolean enable) {
188         if (keyboard != null) {
189             keyboard.enable(enable);
190         }
191         if (scanner != null) {
192             scanner.enable(enable);
193         }
194         if (msr != null) {
195             msr.enable(enable);
196         }
197         if (check != null) {
198             check.enable(enable);
199         }
200         if (ldisplay != null) {
201             ldisplay.enable(enable);
202         }
203         if (pinpad != null) {
204             pinpad.enable(enable);
205         }
206         if (receipt != null) {
207             receipt.enable(enable);
208         }
209         
210         // cash drawers and journal printer are
211
// never able to be disabled so we can
212
// notify when the drawer is open and
213
// print any information needed to the
214
// journal
215
}
216
217     public static void stop() throws GeneralException {
218         try {
219             if (keyboard != null) {
220                 keyboard.close();
221             }
222             if (scanner != null) {
223                 scanner.close();
224             }
225             if (msr != null) {
226                 msr.close();
227             }
228             if (check != null) {
229                 check.close();
230             }
231             if (ldisplay != null) {
232                 ldisplay.close();
233             }
234             if (pinpad != null) {
235                 pinpad.close();
236             }
237
238             if (drawer != null) {
239                 for (int i = 0; i < drawer.length; i++) {
240                     if (drawer[i] != null) {
241                         drawer[i].close();
242                     }
243                 }
244             }
245
246             if (receipt != null) {
247                 receipt.close();
248             }
249             if (journal != null) {
250                 journal.close();
251             }
252         } catch (JposException jpe) {
253             Debug.logError(jpe, "JPOS Exception", module);
254             throw new GeneralException(jpe.getOrigException());
255         }
256     }
257 }
258
Popular Tags