View Javadoc

1   /*
2    * Copyright (C) 2007 ETH Zurich
3    *
4    * This file is part of Fosstrak (www.fosstrak.org).
5    *
6    * Fosstrak is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License version 2.1, as published by the Free Software Foundation.
9    *
10   * Fosstrak is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with Fosstrak; if not, write to the Free
17   * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18   * Boston, MA  02110-1301  USA
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   * Helper class to assemble an EPCIS document. you can add object events and 
45   * when you have all the events together, you can compile the final 
46   * EPCIS document for further processing.
47   * @author sawielan
48   *
49   */
50  public class SimpleEPCISDocument {
51  	
52  	/** a list of all the object events in the EPCIS document. */
53  	protected LinkedList<ObjectEventType> objectEvents = 
54  		new LinkedList<ObjectEventType> ();	
55  	
56  	/**
57  	 * add a new object event to the EPCIS document.
58  	 * @param epcs a list of EPCs to put into the report.
59  	 * @param action the kind of action triggered by this object even.
60  	 * @param bizSteps the <code>bizsteps</code> to set in the event.
61  	 * @param disposition the disposition.
62  	 * @param readPointId the id of the read point.
63  	 * @param bizLocationId the id of the location.
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  		// add the epcs
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  		// set action
88  		objEvent.setAction(action);
89  	
90  		// set bizStep
91  		objEvent.setBizStep(bizSteps);
92  	
93  		// set disposition
94  		objEvent.setDisposition(disposition);
95  	
96  		// set readPoint
97  		ReadPointType readPoint = new ReadPointType();
98  		readPoint.setId(readPointId);
99  		objEvent.setReadPoint(readPoint);
100 	
101 		// set bizLocation
102 		BusinessLocationType bizLocation = new BusinessLocationType();
103 		bizLocation.setId(bizLocationId);
104 		objEvent.setBizLocation(bizLocation);
105 		
106 		objectEvents.add(objEvent);
107 	}
108 	
109 	/**
110 	 * returns a string that describes the time offset.
111 	 * @param eventTime a gregorian calendar holding a time.
112 	 * @return a time offset string.
113 	 */
114 	protected String getTimeOffset(XMLGregorianCalendar eventTime) {
115 		String offset = "";
116 		// get the current time zone and set the eventTimeZoneOffset
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 	 * @return a gregorian calendar describing the current time.
130 	 */
131 	protected XMLGregorianCalendar getNow() {
132 		// get the current time and set the eventTime
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 	 * @return the assembled EPCIS document.
145 	 */
146 	public EPCISDocumentType getDocument() {
147 		// create the EPCISDocument
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 }