1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.fosstrak.capturingapp.util;
22
23 import java.math.BigDecimal;
24 import java.text.DecimalFormat;
25 import java.util.GregorianCalendar;
26 import java.util.LinkedList;
27 import java.util.List;
28
29 import javax.xml.datatype.DatatypeConfigurationException;
30 import javax.xml.datatype.DatatypeFactory;
31 import javax.xml.datatype.XMLGregorianCalendar;
32
33 import org.fosstrak.ale.xsd.epcglobal.EPC;
34 import org.fosstrak.epcis.model.ActionType;
35 import org.fosstrak.epcis.model.BusinessLocationType;
36 import org.fosstrak.epcis.model.EPCISBodyType;
37 import org.fosstrak.epcis.model.EPCISDocumentType;
38 import org.fosstrak.epcis.model.EPCListType;
39 import org.fosstrak.epcis.model.EventListType;
40 import org.fosstrak.epcis.model.ObjectEventType;
41 import org.fosstrak.epcis.model.ReadPointType;
42
43
44
45
46
47
48
49
50 public class SimpleEPCISDocument {
51
52
53 protected LinkedList<ObjectEventType> objectEvents =
54 new LinkedList<ObjectEventType> ();
55
56
57
58
59
60
61
62
63
64
65 public void addObjectEvent(List<Object> epcs,
66 ActionType action, String bizSteps, String disposition,
67 String readPointId, String bizLocationId) {
68
69 EPCListType epcList = new EPCListType();
70
71 for (Object o : epcs) {
72 if (o instanceof EPC) {
73 EPC epc = (EPC) o;
74 org.fosstrak.epcis.model.EPC nepc =
75 new org.fosstrak.epcis.model.EPC();
76 nepc.setValue(epc.getValue());
77 epcList.getEpc().add(nepc);
78 }
79 }
80
81 ObjectEventType objEvent = new ObjectEventType();
82 objEvent.setEpcList(epcList);
83
84 objEvent.setEventTime(getNow());
85 objEvent.setEventTimeZoneOffset(getTimeOffset(objEvent.getEventTime()));
86
87
88 objEvent.setAction(action);
89
90
91 objEvent.setBizStep(bizSteps);
92
93
94 objEvent.setDisposition(disposition);
95
96
97 ReadPointType readPoint = new ReadPointType();
98 readPoint.setId(readPointId);
99 objEvent.setReadPoint(readPoint);
100
101
102 BusinessLocationType bizLocation = new BusinessLocationType();
103 bizLocation.setId(bizLocationId);
104 objEvent.setBizLocation(bizLocation);
105
106 objectEvents.add(objEvent);
107 }
108
109
110
111
112
113
114 protected String getTimeOffset(XMLGregorianCalendar eventTime) {
115 String offset = "";
116
117 if (null != eventTime) {
118 int timezone = eventTime.getTimezone();
119 int h = Math.abs(timezone / 60);
120 int m = Math.abs(timezone % 60);
121 DecimalFormat format = new DecimalFormat("00");
122 String sign = (timezone < 0) ? "-" : "+";
123 offset = sign + format.format(h) + ":" + format.format(m);
124 }
125 return offset;
126 }
127
128
129
130
131 protected XMLGregorianCalendar getNow() {
132
133 XMLGregorianCalendar now = null;
134 try {
135 DatatypeFactory dataFactory = DatatypeFactory.newInstance();
136 now = dataFactory.newXMLGregorianCalendar(new GregorianCalendar());
137 } catch (DatatypeConfigurationException e) {
138 e.printStackTrace();
139 }
140 return now;
141 }
142
143
144
145
146 public EPCISDocumentType getDocument() {
147
148 EPCISDocumentType epcisDoc = new EPCISDocumentType();
149 EPCISBodyType epcisBody = new EPCISBodyType();
150 EventListType eventList = new EventListType();
151
152 for (ObjectEventType objEvent : objectEvents) {
153 eventList.getObjectEventOrAggregationEventOrQuantityEvent().
154 add(objEvent);
155 }
156 epcisBody.setEventList(eventList);
157 epcisDoc.setEPCISBody(epcisBody);
158 epcisDoc.setSchemaVersion(new BigDecimal("1.0"));
159 epcisDoc.setCreationDate(getNow());
160 return epcisDoc;
161 }
162 }