KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > pos > jpos > service > MsrKybService


1 /*
2  * $Id: MsrKybService.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.jpos.service;
26
27 import jpos.JposException;
28 import jpos.MSRConst;
29 import jpos.JposConst;
30 import jpos.events.DataEvent;
31 import jpos.events.ErrorEvent;
32
33 import org.ofbiz.pos.adaptor.KeyboardAdaptor;
34 import org.ofbiz.pos.adaptor.KeyboardReceiver;
35
36 /**
37  *
38  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
39  * @version $Rev: 5462 $
40  * @since 3.2
41  */

42 public class MsrKybService extends BaseService implements jpos.services.MSRService17, KeyboardReceiver {
43
44     public static final String JavaDoc module = MsrKybService.class.getName();
45     public static final int JPOS_MSR_ACCT_ERR = 100;
46     public static final int JPOS_MSR_EXPD_ERR = 101;
47
48     protected String JavaDoc title = new String JavaDoc();
49     protected String JavaDoc firstname = new String JavaDoc();
50     protected String JavaDoc middle = new String JavaDoc();
51     protected String JavaDoc surname = new String JavaDoc();
52     protected String JavaDoc suffix = new String JavaDoc();
53
54     protected String JavaDoc[] accountNumber = new String JavaDoc[2];
55     protected String JavaDoc[] expireDate = new String JavaDoc[2];
56     protected String JavaDoc serviceCode = new String JavaDoc();
57
58     protected byte[] track1DiscretionaryData = new byte[0];
59     protected byte[] track2DiscretionaryData = new byte[0];
60     protected byte[] track1Data = new byte[0];
61     protected byte[] track2Data = new byte[0];
62     protected byte[] track3Data = new byte[0];
63     protected int[] sentinels = new int[0];
64     protected int[] lrc = new int[0];
65
66     protected boolean parseDecodeData = true;
67     protected boolean decodeData = true;
68     protected boolean autoDisable = false;
69     protected boolean sendSentinels = true;
70
71     protected int tracksToRead = MSRConst.MSR_TR_1_2_3;
72     protected int errorType = MSRConst.MSR_ERT_CARD;
73
74     public MsrKybService() {
75         KeyboardAdaptor.getInstance(this, KeyboardAdaptor.MSR_DATA);
76     }
77
78     // MSRService12
79
public boolean getCapISO() throws JposException {
80         // the type of cards this reader supports (ISO only)
81
return true;
82     }
83
84     public boolean getCapJISOne() throws JposException {
85         // the type of cards this reader supports (ISO only)
86
return false;
87     }
88
89     public boolean getCapJISTwo() throws JposException {
90         // the type of cards this reader supports (ISO only)
91
return false;
92     }
93
94     public String JavaDoc getAccountNumber() throws JposException {
95         return this.accountNumber[1];
96     }
97
98     public boolean getAutoDisable() throws JposException {
99         return this.autoDisable;
100     }
101
102     public void setAutoDisable(boolean b) throws JposException {
103         this.autoDisable = b;
104     }
105
106     public boolean getDecodeData() throws JposException {
107         return this.decodeData;
108     }
109
110     public void setDecodeData(boolean b) throws JposException {
111         this.decodeData = b;
112         if (!b) {
113             this.parseDecodeData = false;
114         }
115     }
116
117     public int getErrorReportingType() throws JposException {
118         return this.errorType;
119     }
120
121     public void setErrorReportingType(int i) throws JposException {
122         this.errorType = i;
123     }
124
125     public String JavaDoc getExpirationDate() throws JposException {
126         return this.expireDate[1];
127     }
128
129     public String JavaDoc getFirstName() throws JposException {
130         return this.firstname;
131     }
132
133     public String JavaDoc getMiddleInitial() throws JposException {
134         return this.middle;
135     }
136
137     public boolean getParseDecodeData() throws JposException {
138         return this.parseDecodeData;
139     }
140
141     public void setParseDecodeData(boolean b) throws JposException {
142         this.parseDecodeData = b;
143     }
144
145     public String JavaDoc getServiceCode() throws JposException {
146         return this.serviceCode;
147     }
148
149     public String JavaDoc getSuffix() throws JposException {
150         return this.suffix;
151     }
152
153     public String JavaDoc getSurname() throws JposException {
154         return this.surname;
155     }
156
157     public String JavaDoc getTitle() throws JposException {
158         return this.title;
159     }
160
161     public byte[] getTrack1Data() throws JposException {
162         return this.track1Data;
163     }
164
165     public byte[] getTrack1DiscretionaryData() throws JposException {
166         return this.track1DiscretionaryData;
167     }
168
169     public byte[] getTrack2Data() throws JposException {
170         return this.track2Data;
171     }
172
173     public byte[] getTrack2DiscretionaryData() throws JposException {
174         return this.track2DiscretionaryData;
175     }
176
177     public byte[] getTrack3Data() throws JposException {
178         return this.track3Data;
179     }
180
181     public int getTracksToRead() throws JposException {
182         return this.tracksToRead;
183     }
184
185     public void setTracksToRead(int i) throws JposException {
186         this.tracksToRead = i;
187     }
188
189     public void clearInput() throws JposException {
190         this.title = null;
191         this.firstname = new String JavaDoc();
192         this.middle = new String JavaDoc();
193         this.surname = new String JavaDoc();
194         this.suffix = new String JavaDoc();
195         this.serviceCode = new String JavaDoc();
196         this.accountNumber = new String JavaDoc[2];
197         this.expireDate = new String JavaDoc[2];
198         this.track1Data = new byte[0];
199         this.track2Data = new byte[0];
200         this.track3Data = new byte[0];
201         this.track1DiscretionaryData = new byte[0];
202         this.track2DiscretionaryData = new byte[0];
203     }
204
205     // MSRService13
206
public int getCapPowerReporting() throws JposException {
207         return 0; // not used
208
}
209
210     public int getPowerNotify() throws JposException {
211         return 0; // not used
212
}
213
214     public void setPowerNotify(int i) throws JposException {
215         // not used
216
}
217
218     public int getPowerState() throws JposException {
219         return 0; // not used
220
}
221
222     // MSRService15
223
public boolean getCapTransmitSentinels() throws JposException {
224         return true;
225     }
226
227     public byte[] getTrack4Data() throws JposException {
228         return new byte[0]; // not implemented
229
}
230
231     public boolean getTransmitSentinels() throws JposException {
232         return this.sendSentinels;
233     }
234
235     public void setTransmitSentinels(boolean b) throws JposException {
236         this.sendSentinels = b;
237     }
238
239     // KeyboardReceiver
240
public synchronized void receiveData(int[] codes, char[] chars) {
241         String JavaDoc data = new String JavaDoc(chars);
242         this.parseMsrString(data);
243
244         DataEvent event = new DataEvent(this, 0);
245         this.fireEvent(event);
246     }
247
248     private void parseMsrString(String JavaDoc str) {
249         if (str.indexOf(";") == -1 || str.indexOf("^") == -1) {
250             ErrorEvent error = new ErrorEvent(this, JposConst.JPOS_E_FAILURE, -1,
251                     JposConst.JPOS_EL_INPUT, JposConst.JPOS_ER_CLEAR);
252             this.fireEvent(error);
253             return;
254         }
255
256         try {
257             // parse the tracks from the character string
258
String JavaDoc track1 = str.substring(0, str.indexOf(";"));
259             String JavaDoc track2 = str.substring(str.indexOf(";"), str.length() - 1);
260
261             // track 1 data
262
int firstCarrot = track1.indexOf("^", 2);
263             int nextCarrot = track1.indexOf("^", firstCarrot + 1);
264
265             String JavaDoc tr1BegChar = track1.substring(0, 1);
266             String JavaDoc formatCode = track1.substring(1, 2);
267             String JavaDoc tr1AcctNum = track1.substring(2, firstCarrot);
268             String JavaDoc cardHolder = track1.substring(firstCarrot + 1, nextCarrot);
269             String JavaDoc tr1ExpDate = track1.substring(nextCarrot + 1, nextCarrot + 5);
270             String JavaDoc tr1OptData = track1.substring(nextCarrot + 4, track1.length() - 2);
271             String JavaDoc tr1EndChar = track1.substring(track1.length() - 2, track1.length() - 1);
272             String JavaDoc tr1LrcCode = track1.substring(track1.length() - 1, track1.length());
273
274             // track 2 data
275
String JavaDoc tr2BegChar = track2.substring(0, 1);
276             String JavaDoc tr2AcctNum = track2.substring(1, track2.indexOf("="));
277             String JavaDoc tr2ExpDate = track2.substring((track2.indexOf("=") + 1), track2.indexOf("=") + 5);
278             String JavaDoc tr2OptData = track2.substring(track2.indexOf("=") + 5, track2.length() - 2);
279             String JavaDoc tr2EndChar = track2.substring(track2.length() - 2, track2.length() - 1);
280             String JavaDoc tr2LrcCode = track2.substring(track2.length() - 1, track2.length());
281
282             // see if we need to remove the sentinels
283
if (this.sendSentinels) {
284                 track1 = track1.substring(0, track1.length() - 1);
285                 track2 = track2.substring(0, track2.length() - 1);
286             } else {
287                 track1 = track1.substring(1, track1.length() - 2);
288                 track2 = track2.substring(0, track2.length() - 2);
289             }
290
291             // set the raw track data
292
this.track1Data = track1.getBytes();
293             this.track2Data = track2.getBytes();
294
295             // parse the decoded data
296
if (this.parseDecodeData) {
297                 // set the sentinels
298
this.sentinels = new int[4];
299                 this.sentinels[0] = (int) tr1BegChar.charAt(0);
300                 this.sentinels[1] = (int) tr1EndChar.charAt(0);
301                 this.sentinels[2] = (int) tr2BegChar.charAt(0);
302                 this.sentinels[3] = (int) tr2EndChar.charAt(0);
303
304                 // set the lrcs
305
this.lrc = new int[2];
306                 this.lrc[0] = (int) tr1LrcCode.charAt(0);
307                 this.lrc[1] = (int) tr2LrcCode.charAt(0);
308
309                 // set the account number
310
this.accountNumber = new String JavaDoc[2];
311                 this.accountNumber[0] = tr1AcctNum.trim();
312                 this.accountNumber[1] = tr2AcctNum.trim();
313
314                 // set the expiration date
315
this.expireDate = new String JavaDoc[2];
316                 this.expireDate[0] = tr1ExpDate.trim();
317                 this.expireDate[1] = tr2ExpDate.trim();
318
319                 // track discretionar info
320
this.track1DiscretionaryData = tr1OptData.getBytes();
321                 this.track2DiscretionaryData = tr2OptData.getBytes();
322                 this.track1Data = track1.getBytes();
323                 this.track2Data = track2.getBytes();
324
325                 // other field data (i.e. name)
326
this.serviceCode = formatCode.trim();
327                 this.surname = (cardHolder.substring(0, cardHolder.indexOf("/"))).trim();
328                 this.firstname = (cardHolder.substring((cardHolder.indexOf("/") + 1))).trim();
329                 // not sure how to support title, middle initial, and suffix
330
}
331         } catch (Exception JavaDoc e) {
332             ErrorEvent error = new ErrorEvent(this, JposConst.JPOS_E_FAILURE, -1,
333                     JposConst.JPOS_EL_INPUT, JposConst.JPOS_ER_CLEAR);
334             this.fireEvent(error);
335             return;
336         }
337     }
338 }
339
Popular Tags