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.repository.query;
22
23 import java.util.Calendar;
24
25 import javax.management.Notification;
26 import javax.management.timer.Timer;
27
28 import org.fosstrak.epcis.model.ArrayOfString;
29 import org.fosstrak.epcis.model.Poll;
30 import org.fosstrak.epcis.model.QueryParam;
31 import org.fosstrak.epcis.model.QueryParams;
32 import org.fosstrak.epcis.model.QueryResults;
33 import org.fosstrak.epcis.soap.ImplementationExceptionResponse;
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36
37
38
39
40
41
42
43
44
45 public class QuerySubscriptionTriggered extends QuerySubscriptionScheduled {
46
47 private static final long serialVersionUID = 658402150914797471L;
48
49 private static final Log LOG = LogFactory.getLog(QuerySubscriptionTriggered.class);
50
51 private String trigger;
52
53 public QuerySubscriptionTriggered(final String subscriptionID, final QueryParams queryParams, final String dest,
54 final Boolean reportIfEmpty, final Calendar initialRecordTime,
55 final Calendar lastTimeExecuted, final String queryName, final String trigger,
56 final Schedule every10min) throws ImplementationExceptionResponse {
57 super(subscriptionID, queryParams, dest, reportIfEmpty, initialRecordTime, lastTimeExecuted, every10min,
58 queryName);
59 this.trigger = trigger;
60 }
61
62
63
64
65
66
67
68
69 @Override
70 public void handleNotification(final Notification pNotification, final Object pHandback) {
71 if (pHandback == null) {
72 LOG.error("The timer stating the next scheduled query execution time is null!");
73 return;
74 }
75 Timer timer = (Timer) pHandback;
76
77 if (!doItAgain.booleanValue()) {
78 timer.stop();
79 } else {
80 try {
81 LOG.debug("Checking trigger condition ...");
82 String queryName = "SimpleEventQuery";
83 QueryParams params = new QueryParams();
84
85
86 QueryParam param = new QueryParam();
87 param.setName("MATCH_anyEPC");
88 ArrayOfString strings = new ArrayOfString();
89 strings.getString().add(trigger);
90 param.setValue(strings);
91 params.getParam().add(param);
92
93
94 param = new QueryParam();
95 param.setName("GE_recordTime");
96 param.setValue(initialRecordTime);
97 params.getParam().add(param);
98
99
100 Poll poll = new Poll();
101 poll.setParams(params);
102 poll.setQueryName(queryName);
103 QueryResults results = executePoll(poll);
104 if (results != null && results.getResultsBody() != null
105 && results.getResultsBody().getEventList() != null) {
106 LOG.debug("Trigger condition fulfilled!");
107 LOG.debug("Executing subscribed query associated with trigger event ...");
108 super.executeQuery();
109 LOG.debug("Triggered query successfully executed!");
110 }
111 } catch (Exception e) {
112 String msg = "An error occurred while checking trigger condition for query with subscriptionID '"
113 + subscriptionID + "': " + e.getMessage();
114 LOG.error(msg, e);
115 }
116
117
118 setNextScheduledExecutionTime(timer);
119 }
120 }
121 }