1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.fosstrak.epcis.captureclient;
22
23 import java.net.URL;
24 import java.text.DecimalFormat;
25 import java.util.ArrayList;
26 import java.util.Calendar;
27 import java.util.GregorianCalendar;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.StringTokenizer;
31 import java.util.TimeZone;
32 import java.util.Map.Entry;
33
34 import javax.swing.ImageIcon;
35
36 import org.w3c.dom.Document;
37 import org.w3c.dom.Element;
38
39
40
41
42
43
44
45 public class CaptureClientHelper {
46
47
48
49
50 public static final DecimalFormat XX_FORMAT = new DecimalFormat("00");
51 public static final DecimalFormat XXX_FORMAT = new DecimalFormat("000");
52 public static final DecimalFormat XXXX_FORMAT = new DecimalFormat("0000");
53
54
55
56
57 public static final String toolTipDate = "Format is ISO 8601, i.e. YYYY-MM-DDThh:mm:ss.SSSZ";
58 public static final String toolTipUri = "URI";
59 public static final String toolTipUris = "One or multiple URIs, separated by spaces";
60 public static final String toolTipInteger = "Integer number";
61 public static final String toolTipOptional = ". This field is optional";
62 public static final String toolTipBizTransType = "Business Transaction Type";
63 public static final String toolTipBizTransID = "Business Transactio ID";
64
65
66
67
68 public static final String[] ACTIONS = { "ADD", "OBSERVE", "DELETE" };
69
70
71
72
73 public static final String[] EPCIS_EVENT_NAMES = {
74 "Object event", "Aggregation event", "Quantity event", "Transaction event" };
75
76 public enum EpcisEventType {
77
78 ObjectEvent(0, "Object event"), AggregationEvent(1, "Aggregation event"), QuantityEvent(2, "Quantity event"),
79 TransactionEvent(3, "Transaction event");
80
81 private int guiIndex;
82 private String guiName;
83
84 private EpcisEventType(int guiIndex, String guiName) {
85 this.guiIndex = guiIndex;
86 this.guiName = guiName;
87 }
88
89 public static EpcisEventType fromGuiIndex(int index) {
90 for (EpcisEventType eventType : values()) {
91 if (eventType.guiIndex == index) {
92 return eventType;
93 }
94 }
95 return null;
96 }
97
98 public static String[] guiNames() {
99 String[] guiNames = new String[values().length];
100 for (int i = 0; i < values().length; i++) {
101 guiNames[i] = values()[i].getGuiName();
102 }
103 return guiNames;
104 }
105
106 public String getGuiName() {
107 return guiName;
108 }
109 }
110
111
112
113
114
115
116
117
118
119 public static String getTimeZone(final Calendar cal) {
120 StringBuilder buf = new StringBuilder();
121 TimeZone tz = cal.getTimeZone();
122
123 int offset = tz.getOffset(cal.getTimeInMillis());
124 int hours = Math.abs((offset / (60 * 1000)) / 60);
125 int minutes = Math.abs((offset / (60 * 1000)) % 60);
126 buf.append(offset < 0 ? '-' : '+');
127 buf.append(XX_FORMAT.format(hours));
128 buf.append(':');
129 buf.append(XX_FORMAT.format(minutes));
130 return buf.toString();
131 }
132
133
134
135
136
137
138
139
140
141 public static String format(final Calendar cal) {
142 if (cal == null) {
143 throw new IllegalArgumentException("argument can not be null");
144 }
145
146
147 int year = cal.get(Calendar.YEAR);
148 if (cal.isSet(Calendar.ERA) && cal.get(Calendar.ERA) == GregorianCalendar.BC) {
149
150
151
152
153 year = 0 - year + 1;
154 }
155
156
157
158
159
160
161 StringBuilder buf = new StringBuilder();
162
163 buf.append(XXXX_FORMAT.format(year));
164 buf.append('-');
165
166 buf.append(XX_FORMAT.format(cal.get(Calendar.MONTH) + 1));
167 buf.append('-');
168
169 buf.append(XX_FORMAT.format(cal.get(Calendar.DAY_OF_MONTH)));
170 buf.append('T');
171
172 buf.append(XX_FORMAT.format(cal.get(Calendar.HOUR_OF_DAY)));
173 buf.append(':');
174
175 buf.append(XX_FORMAT.format(cal.get(Calendar.MINUTE)));
176 buf.append(':');
177
178 buf.append(XX_FORMAT.format(cal.get(Calendar.SECOND)));
179 buf.append('.');
180
181 buf.append(XXX_FORMAT.format(cal.get(Calendar.MILLISECOND)));
182
183 buf.append(getTimeZone(cal));
184 return buf.toString();
185 }
186
187
188
189
190
191
192
193 public static final class ExampleEvents {
194
195 private static List<CaptureEvent> examples = null;
196
197
198
199
200 private static void initEvents() {
201 examples = new ArrayList<CaptureEvent>();
202 CaptureEvent ex = new CaptureEvent();
203 ex.setDescription("DEMO 1: Item is assigned a new EPC");
204 ex.setType(0);
205 ex.setEventTime("2006-09-20T06:36:17Z");
206 ex.setEventTimeZoneOffset("+00:00");
207 ex.setAction(0);
208 ex.setBizStep("urn:epcglobal:cbv:bizstep:commissioning");
209 ex.setDisposition("urn:epcglobal:cbv:bizstep:active");
210 ex.setBizLocation("urn:epc:id:sgln:0614141.00729.loc5");
211 ex.setReadPoint("urn:epc:id:sgln:0614141.00729.rp97");
212 ex.setEpcList("urn:epc:id:sgtin:0057000.123780.7788");
213 examples.add(ex);
214
215 ex = new CaptureEvent();
216 ex.setDescription("DEMO 2: Item is inspected for QA");
217 ex.setType(0);
218 ex.setEventTime("2006-09-20T07:33:31.116Z");
219 ex.setEventTimeZoneOffset("+00:00");
220 ex.setAction(1);
221 ex.setBizStep("urn:epcglobal:cbv:bizstep:inspecting");
222 ex.setDisposition("urn:epcglobal:cbv:bizstep:active");
223 ex.setBizLocation("urn:epc:id:sgln:0614141.00729.loc5");
224 ex.setReadPoint("urn:epc:id:sgln:0614141.00729.handheld8");
225 ex.setEpcList("urn:epc:id:sgtin:0057000.123780.7788");
226 examples.add(ex);
227
228 ex = new CaptureEvent();
229 ex.setDescription("DEMO 3: Item is aggregated onto a pallet");
230 ex.setType(1);
231 ex.setAction(0);
232 ex.setEventTime("2006-09-20T08:55:04Z");
233 ex.setEventTimeZoneOffset("+00:00");
234 ex.setBizStep("urn:epcglobal:cbv:bizstep:packing");
235 ex.setDisposition("urn:epcglobal:cbv:disp:in_progress");
236 ex.setBizLocation("urn:epc:id:sgln:0614141.00729.loc450");
237 ex.setReadPoint("urn:epc:id:sgln:0614141.00729.rp104");
238 ex.setBizTransaction("urn:epcglobal:cbv:btt:po", "http://transaction.fosstrak.org/po/12345678");
239 ex.setParentID("urn:epc:id:sscc:0614141.1234567890");
240 ex.setChildEPCs("urn:epc:id:sgtin:0057000.123780.7788 urn:epc:id:sgtin:0057000.123430.2027 "
241 + "urn:epc:id:sgtin:0057000.123430.2028 urn:epc:id:sgtin:0057000.123430.2029");
242 examples.add(ex);
243
244 ex = new CaptureEvent();
245 ex.setDescription("DEMO 4: Pallet arrives at port of Kaohsiung");
246 ex.setType(0);
247 ex.setEventTime("2006-09-20T10:33:31.116Z");
248 ex.setEventTimeZoneOffset("+00:00");
249 ex.setAction(1);
250 ex.setBizStep("urn:epcglobal:cbv:bizstep:arriving");
251 ex.setDisposition("urn:epcglobal:cbv:disp:in_progress");
252 ex.setBizLocation("http://epcis.fosstrak.org/demo/loc/china/kaohsiung");
253 ex.setReadPoint("http://epcis.fosstrak.org/demo/loc/china/kaohsiung/e62");
254 ex.setEpcList("urn:epc:id:sscc:0614141.1234567890");
255 examples.add(ex);
256
257 ex = new CaptureEvent();
258 ex.setDescription("DEMO 5: Pallet departs from port of Kaohsiung");
259 ex.setType(0);
260 ex.setEventTime("2006-09-21T13:27:08.155Z");
261 ex.setEventTimeZoneOffset("+00:00");
262 ex.setAction(1);
263 ex.setBizStep("urn:epcglobal:cbv:bizstep:departing");
264 ex.setDisposition("urn:epcglobal:cbv:disp:in_transit");
265 ex.setBizLocation("http://epcis.fosstrak.org/demo/loc/china/kaohsiung");
266 ex.setReadPoint("http://epcis.fosstrak.org/demo/loc/china/kaohsiung/b18");
267 ex.setEpcList("urn:epc:id:sscc:0614141.1234567890");
268 examples.add(ex);
269
270 ex = new CaptureEvent();
271 ex.setDescription("Item passes a reader during manufacturing process");
272 ex.setType(0);
273 ex.setEventTime("2006-04-03T20:33:31.116Z");
274 ex.setEventTimeZoneOffset("+00:00");
275 ex.setAction(1);
276 ex.setBizStep("http://epcis.fosstrak.org/bizstep/production");
277 ex.setBizLocation("urn:epc:id:sgln:0614141.00101.loc210");
278 ex.setReadPoint("urn:epc:id:sgln:0614141.00101.rp210");
279 ex.setEpcList("urn:epc:id:sgtin:0057000.123780.3167");
280 examples.add(ex);
281
282 ex = new CaptureEvent();
283 ex.setDescription("Two pallets are loaded onto a truck");
284 ex.setType(0);
285 ex.setEventTime("2006-05-09T21:01:44Z");
286 ex.setEventTimeZoneOffset("+00:00");
287 ex.setAction(1);
288 ex.setBizStep("urn:epcglobal:cbv:bizstep:loading");
289 ex.setDisposition("urn:epcglobal:cbv:disp:in_transit");
290 ex.setBizLocation("urn:epc:id:sgln:0614141.00101.loc209");
291 ex.setReadPoint("urn:epc:id:sgln:0614141.00101.rp53");
292 ex.setEpcList("urn:epc:id:sscc:0614141.2644895423 urn:epc:id:sscc:0614141.2644895424");
293 ex.setBizTransaction("urn:epcglobal:cbv:fmcg:btt:po", "http://transaction.example.com/po/12345678");
294 examples.add(ex);
295
296 ex = new CaptureEvent();
297 ex.setDescription("Item arrives for repair");
298 ex.setType(0);
299 ex.setEventTime("2006-05-10T04:50:35Z");
300 ex.setEventTimeZoneOffset("+00:00");
301 ex.setAction(1);
302 ex.setBizStep("urn:epcglobal:cbv:bizstep:receiving");
303 ex.setDisposition("http://epcis.fosstrak.org/disp/in_repair");
304 ex.setBizLocation("urn:epc:id:sgln:0614141.00101.repair-center");
305 ex.setReadPoint("urn:epc:id:sgln:0614141.00101.rp77");
306 ex.setEpcList("urn:epc:id:sgtin:0034000.987650.2686");
307 examples.add(ex);
308
309 ex = new CaptureEvent();
310 ex.setDescription("Three items are aggregated onto a barcode-labeled pallet");
311 ex.setType(1);
312 ex.setAction(0);
313 ex.setEventTime("2006-06-01T15:55:04Z");
314 ex.setEventTimeZoneOffset("+00:00");
315 ex.setBizStep("urn:epcglobal:cbv:bizstep:packing");
316 ex.setDisposition("urn:epcglobal:cbv:disp:in_progress");
317 ex.setBizLocation("urn:epc:id:sgln:0614141.00101.loc208");
318 ex.setReadPoint("urn:epc:id:sgln:0614141.00101.rp98");
319 ex.setBizTransaction("urn:epcglobal:cbv:fmcg:btt:po", "http://transaction.example.com/po/12345678");
320 ex.setBizTransaction("urn:epcglobal:cbv:btt:asn", "http://transaction.example.com/asn/1152");
321 ex.setParentID("urn:epc:id:sscc:0614141.2644895423");
322 ex.setChildEPCs("urn:epc:id:sgtin:0057000.123430.2025 urn:epc:id:sgtin:0057000.123430.2027 "
323 + "urn:epc:id:sgtin:0057000.123430.2028");
324 examples.add(ex);
325
326 ex = new CaptureEvent();
327 ex.setDescription("Aggregation ends at customer site");
328 ex.setType(1);
329 ex.setAction(2);
330 ex.setEventTime("2006-06-05T09:26:06Z");
331 ex.setEventTimeZoneOffset("+00:00");
332 ex.setBizLocation("urn:epc:id:sgln:0614141.82863.loc3");
333 ex.setReadPoint("urn:epc:id:sgln:0614141.82863.reader10");
334 ex.setBizTransaction("urn:epcglobal:cbv:fmcg:btt:po", "http://po.example.org/E58J3Q");
335 ex.setBizTransaction("urn:epcglobal:cbv:btt:asn", "http://transaction.example.com/asn/1152");
336 ex.setParentID("urn:epc:id:sscc:0614141.2644895423");
337 examples.add(ex);
338
339 ex = new CaptureEvent();
340 ex.setDescription("Physical inventory count: 67 items");
341 ex.setType(2);
342 ex.setEventTime("2006-01-15T16:15:31Z");
343 ex.setEventTimeZoneOffset("+00:00");
344 ex.setBizStep("urn:epcglobal:cbv:bizstep:storing");
345 ex.setDisposition("urn:epcglobal:cbv:disp:active");
346 ex.setBizLocation("urn:epc:id:sgln:0614141.82863.loc6");
347 ex.setReadPoint("urn:epc:id:sgln:0614141.82863.loc6-virt");
348 ex.setEpcClass("urn:epc:id:sgtin:0069000.957110");
349 ex.setQuantity(67);
350 examples.add(ex);
351
352 ex = new CaptureEvent();
353 ex.setDescription("Order changed by customer - two more objects added to transaction");
354 ex.setType(3);
355 ex.setEventTime("2006-08-18T11:53:01Z");
356 ex.setEventTimeZoneOffset("+00:00");
357 ex.setAction(0);
358 ex.setBizTransaction("urn:epcglobal:cbv:fmcg:btt:po", "http://transaction.example.com/po/6677150");
359 ex.setEpcList("urn:epc:id:sgtin:0057000.678930.5003 urn:epc:id:sgtin:0057000.678930.5004");
360 examples.add(ex);
361
362 ex = new CaptureEvent();
363 ex.setDescription("Transaction is finished");
364 ex.setType(3);
365 ex.setEventTime("2006-08-20T07:03:51Z");
366 ex.setEventTimeZoneOffset("+00:00");
367 ex.setAction(2);
368 ex.setBizTransaction("urn:epcglobal:cbv:fmcg:btt:po", "http://transaction.example.com/po/6677150");
369 ex.setEpcList("urn:epc:id:sgtin:0057000.678930.5003 urn:epc:id:sgtin:0057000.678930.5004");
370 examples.add(ex);
371 }
372
373 public static List<CaptureEvent> getExamples() {
374 if (examples == null) {
375 initEvents();
376 }
377 return examples;
378 }
379 }
380
381
382
383
384
385
386
387
388 public static ImageIcon getImageIcon(final String filename) {
389
390 ClassLoader classLoader = CaptureClientHelper.class.getClassLoader();
391 URL url = classLoader.getResource("gui/" + filename);
392 if (url != null) {
393 return new ImageIcon(url);
394 } else {
395
396
397 ImageIcon ii;
398 ii = new ImageIcon("./tools/capturingGUI/media/" + filename);
399 if (ii.getImageLoadStatus() != java.awt.MediaTracker.COMPLETE) {
400 ii = new ImageIcon("./gui/" + filename);
401 }
402 return ii;
403 }
404 }
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420 public static boolean addQuantity(final Document document, final Element root, final String quantity) {
421 Integer n = null;
422 try {
423 n = new Integer(quantity);
424 } catch (NumberFormatException e) {
425 return false;
426 }
427 return addElement(document, root, n.toString(), "quantity");
428 }
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444 public static boolean addEpcClass(final Document document, final Element root, final String epcClass) {
445 return addElement(document, root, epcClass, "epcClass");
446 }
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463 public static boolean addChildEpcList(final Document document, final Element root, final String childEPCs) {
464 if (isEmpty(childEPCs)) {
465 return false;
466 }
467 Element element = document.createElement("childEPCs");
468 StringTokenizer st = new StringTokenizer(childEPCs);
469 while (st.hasMoreTokens()) {
470 addElement(document, element, st.nextToken(), "epc");
471 }
472 root.appendChild(element);
473 return true;
474 }
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493 public static boolean addBizTransactions(final Document document, final Element root,
494 final Map<String, String> bizTransactions) {
495 if (bizTransactions == null || bizTransactions.isEmpty() || bizTransactions.keySet().size() < 1) {
496 return false;
497 }
498 Element element = document.createElement("bizTransactionList");
499 for (Entry<String, String> bizTransEntry : bizTransactions.entrySet()) {
500 if (!isEmpty(bizTransEntry.getKey()) && !isEmpty(bizTransEntry.getValue())) {
501 Element bizNode = document.createElement("bizTransaction");
502 bizNode.appendChild(document.createTextNode(bizTransEntry.getKey()));
503 bizNode.setAttribute("type", bizTransEntry.getValue());
504 element.appendChild(bizNode);
505 }
506 }
507 root.appendChild(element);
508 return true;
509 }
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526 public static boolean addBizLocation(final Document document, final Element root, final String bizLocation) {
527 if (isEmpty(bizLocation)) {
528 return false;
529 }
530 Element element = document.createElement("bizLocation");
531 addElement(document, element, bizLocation, "id");
532 root.appendChild(element);
533 return true;
534 }
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551 public static boolean addReadPoint(final Document document, final Element root, final String readPoint) {
552 if (isEmpty(readPoint)) {
553 return false;
554 }
555 Element element = document.createElement("readPoint");
556 addElement(document, element, readPoint, "id");
557 root.appendChild(element);
558 return true;
559 }
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576 public static boolean addDisposition(final Document document, final Element root, String disposition) {
577 return addElement(document, root, disposition, "disposition");
578 }
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594 public static boolean addBizStep(final Document document, final Element root, final String bizStep) {
595 return addElement(document, root, bizStep, "bizStep");
596 }
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612 public static boolean addAction(final Document document, final Element root, String action) {
613 return addElement(document, root, action, "action");
614 }
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631 public static boolean addEpcList(final Document document, final Element root, final String epcList) {
632 if (isEmpty(epcList)) {
633 return false;
634 }
635 Element element = document.createElement("epcList");
636 StringTokenizer st = new StringTokenizer(epcList);
637 while (st.hasMoreTokens()) {
638 addElement(document, element, st.nextToken(), "epc");
639 }
640 root.appendChild(element);
641 return true;
642 }
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658 public static boolean addParentId(final Document document, final Element root, String parentID) {
659 return addElement(document, root, parentID, "parentID");
660 }
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676 public static boolean addEventTime(final Document document, final Element root, final String eventTime) {
677 return addElement(document, root, eventTime, "eventTime");
678 }
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695 public static boolean addEventTimeZoneOffset(final Document document, final Element root,
696 final String eventTimeZoneOffset) {
697 return addElement(document, root, eventTimeZoneOffset, "eventTimeZoneOffset");
698 }
699
700 private static boolean addElement(final Document document, final Element root, final String elementValue,
701 final String elementName) {
702 if (isEmpty(elementValue)) {
703 return false;
704 }
705 Element element = document.createElement(elementName);
706 element.appendChild(document.createTextNode(elementValue));
707 root.appendChild(element);
708 return true;
709 }
710
711 private static boolean isEmpty(String s) {
712 return s == null || s.trim().equals("");
713 }
714 }