26
26
import lombok .Getter ;
27
27
import lombok .Setter ;
28
28
29
- import org .json .JSONObject ;
30
29
import org .slf4j .Logger ;
31
30
import org .slf4j .LoggerFactory ;
32
31
import org .springframework .beans .factory .annotation .Autowired ;
@@ -63,19 +62,36 @@ public class WaitListStorageHandler {
63
62
@ Autowired
64
63
private JmesPathInterface jmesPathInterface ;
65
64
66
- public void addEventToWaitList (String event , RulesObject rulesObject ) {
65
+ /**
66
+ * Adds event to the wait-list database if it does not already exists.
67
+ *
68
+ * @param event The event that will be added to database
69
+ * @param rulesObject Rules for extracting a unique identifier from an event object to be used as document id
70
+ */
71
+ public void addEventToWaitListIfNotExisting (String event , RulesObject rulesObject ) {
67
72
try {
68
- String condition = "{\" _id\" : \" " + new JSONObject (event ).getJSONObject ("meta" ).getString ("id" ) + "\" }" ;
69
- List <String > foundEventsInWaitList = mongoDbHandler .find (databaseName , collectionName , condition );
70
- if (foundEventsInWaitList .isEmpty ()) {
71
- String input = addPropertiesToEvent (event , rulesObject );
72
- mongoDbHandler .insertDocument (databaseName , collectionName , input );
73
+ JsonNode id = extractIdFromEventUsingRules (event , rulesObject );
74
+ String foundEvent = findEventInWaitList (id );
75
+ if (foundEvent .isEmpty ()) {
76
+ Date date = createCurrentTimeStamp ();
77
+ BasicDBObject document = createWaitListDocument (event , id , date );
78
+ mongoDbHandler .insertDocument (databaseName , collectionName , document .toString ());
73
79
}
74
80
} catch (MongoWriteException e ) {
75
81
LOGGER .debug ("Failed to insert event into waitlist." , e );
76
82
}
77
83
}
78
84
85
+ private String findEventInWaitList (JsonNode id ) {
86
+ String condition = "{\" _id\" : \" " + id + "\" }" ;
87
+ List <String > foundEventsInWaitList = mongoDbHandler .find (databaseName , collectionName , condition );
88
+ if (foundEventsInWaitList .isEmpty ()) {
89
+ return "" ;
90
+ }
91
+ String foundEvent = foundEventsInWaitList .get (0 );
92
+ return foundEvent ;
93
+ }
94
+
79
95
public boolean dropDocumentFromWaitList (String document ) {
80
96
return mongoDbHandler .dropDocument (databaseName , collectionName , document );
81
97
}
@@ -84,9 +100,16 @@ public List<String> getWaitList() {
84
100
return mongoDbHandler .getAllDocuments (databaseName , collectionName );
85
101
}
86
102
87
- private String addPropertiesToEvent (String event , RulesObject rulesObject ) {
88
- String idRule = rulesObject .getIdRule ();
89
- JsonNode id = jmesPathInterface .runRuleOnEvent (idRule , event );
103
+ private BasicDBObject createWaitListDocument (String event , JsonNode id , Date date ) {
104
+ BasicDBObject document = new BasicDBObject ();
105
+ document .put ("_id" , id .textValue ());
106
+ document .put ("Time" , date );
107
+ document .put ("Event" , event );
108
+ mongoDbHandler .createTTLIndex (databaseName , collectionName , "Time" , ttlValue );
109
+ return document ;
110
+ }
111
+
112
+ private Date createCurrentTimeStamp () {
90
113
DateFormat dateFormat = new SimpleDateFormat ("yyyy/MM/dd HH:mm:ss" );
91
114
Date date = new Date ();
92
115
String time = dateFormat .format (date );
@@ -95,12 +118,13 @@ private String addPropertiesToEvent(String event, RulesObject rulesObject) {
95
118
} catch (ParseException e ) {
96
119
LOGGER .error ("Failed to parse time from date object." , e );
97
120
}
98
- BasicDBObject document = new BasicDBObject ();
99
- document .put ("_id" , id .textValue ());
100
- document .put ("Time" , date );
101
- document .put ("Event" , event );
102
- mongoDbHandler .createTTLIndex (databaseName , collectionName , "Time" , ttlValue );
103
- return document .toString ();
121
+ return date ;
122
+ }
123
+
124
+ private JsonNode extractIdFromEventUsingRules (String event , RulesObject rulesObject ) {
125
+ String idRule = rulesObject .getIdRule ();
126
+ JsonNode id = jmesPathInterface .runRuleOnEvent (idRule , event );
127
+ return id ;
104
128
}
105
129
106
130
}
0 commit comments