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.epcis.queryclient;
22  
23  import java.util.ArrayList;
24  import java.util.Arrays;
25  import java.util.Calendar;
26  import java.util.List;
27  import java.util.TimeZone;
28  
29  import org.fosstrak.epcis.model.ArrayOfString;
30  import org.fosstrak.epcis.model.QueryParam;
31  import org.fosstrak.epcis.utils.TimeParser;
32  
33  /**
34   * This is a helper class which encapsulates common functionality used within
35   * the query client classes.
36   * 
37   * @author Marco Steybe
38   */
39  public class QueryClientHelper {
40  
41      /**
42       * Converts the values in a calendar object into a nicely formatted string.
43       * 
44       * @param cal
45       *            with the Calendar-Date
46       * @return String
47       */
48      public static String printCalendar(final Calendar cal) {
49          if (cal == null) {
50              return null;
51          }
52          // set to current timezone
53          cal.setTimeZone(TimeZone.getDefault());
54          return TimeParser.format(cal);
55      }
56  
57      /**
58       * Converts a space-separated list of strings to an ArrayOfString.
59       * 
60       * @param txt
61       *            A space-separated list of strings.
62       * @return An ArrayOfString object containing single string tokens.
63       */
64      public static ArrayOfString stringListToArray(final String txt) {
65          List<String> tokens = Arrays.asList(txt.split(" "));
66          ArrayOfString strings = new ArrayOfString();
67          strings.getString().addAll(tokens);
68          return strings;
69      }
70  
71      /**
72       * Implements a class that holds examples for the EPCIS Query Interface
73       * Client.
74       * 
75       * @author David Gubler
76       */
77      public static class ExampleQueries {
78  
79          private static List<Query> examples = null;
80  
81          /**
82           * Sets up the examples. Add examples here if you wish.
83           */
84          private static void initExamples() {
85              examples = new ArrayList<Query>();
86              Query ex = new Query();
87              ex.setDescription("Search for an aggregation onto a certain pallet");
88              ex.setReturnAggregationEvents(true);
89              QueryParam param = new QueryParam();
90              param.setName("EQ_action");
91              param.setValue("ADD");
92              ex.getQueryParameters().add(param);
93              param = new QueryParam();
94              param.setName("MATCH_parentID");
95              param.setValue("urn:epc:id:sscc:0614141.1234567890");
96              ex.getQueryParameters().add(param);
97              examples.add(ex);
98  
99              ex = new Query();
100             ex.setDescription("Return all events for a given EPC that ocurred after a given date");
101             ex.setReturnObjectEvents(true);
102             param = new QueryParam();
103             param.setName("GE_eventTime");
104             param.setValue("2006-01-01T05:20:31Z");
105             ex.getQueryParameters().add(param);
106             param = new QueryParam();
107             param.setName("MATCH_epc");
108             param.setValue("urn:epc:id:sgtin:0034000.987650.2686");
109             ex.getQueryParameters().add(param);
110             examples.add(ex);
111 
112             ex = new Query();
113             ex.setDescription("Return all events generated by a given reader");
114             ex.setReturnObjectEvents(true);
115             ex.setReturnAggregationEvents(true);
116             ex.setReturnQuantityEvents(true);
117             ex.setReturnTransactionEvents(true);
118             param = new QueryParam();
119             param.setName("EQ_readPoint");
120             param.setValue("urn:epc:id:sgln:0614141.00729.whatever215");
121             ex.getQueryParameters().add(param);
122             examples.add(ex);
123 
124             ex = new Query();
125             ex.setDescription("Retrieve an EPC's shipping date");
126             ex.setReturnObjectEvents(true);
127             param = new QueryParam();
128             param.setName("EQ_action");
129             param.setValue("OBSERVE");
130             ex.getQueryParameters().add(param);
131             param = new QueryParam();
132             param.setName("EQ_bizStep");
133             param.setValue("urn:epcglobal:cbv:bizstep:shipping");
134             ex.getQueryParameters().add(param);
135             param = new QueryParam();
136             param.setName("MATCH_epc");
137             param.setValue("urn:epc:id:sgtin:0057000.123430.2028");
138             ex.getQueryParameters().add(param);
139             examples.add(ex);
140 
141             ex = new Query();
142             ex.setDescription("Retrieve all EPCs that were returned in year 2006");
143             ex.setReturnObjectEvents(true);
144             param = new QueryParam();
145             param.setName("EQ_action");
146             param.setValue("OBSERVE");
147             ex.getQueryParameters().add(param);
148             param = new QueryParam();
149             param.setName("GE_eventTime");
150             param.setValue("2006-01-01T00:00:00Z");
151             ex.getQueryParameters().add(param);
152             param = new QueryParam();
153             param.setName("LT_eventTime");
154             param.setValue("2007-01-01T00:00:00Z");
155             ex.getQueryParameters().add(param);
156             param = new QueryParam();
157             param.setName("EQ_disposition");
158             param.setValue("urn:epcglobal:cbv:disp:returned");
159             ex.getQueryParameters().add(param);
160             examples.add(ex);
161         }
162 
163         /**
164          * @return A List of Query.
165          */
166         public static List<Query> getExamples() {
167             if (examples == null) {
168                 initExamples();
169             }
170             return examples;
171         }
172     }
173 }