Skip to content

Commit 2b1cca1

Browse files
authored
Refactor WaitListStorageHandler (#323)
1 parent 9dee65d commit 2b1cca1

File tree

4 files changed

+43
-19
lines changed

4 files changed

+43
-19
lines changed

src/main/java/com/ericsson/ei/handlers/DownstreamIdRulesHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void runIdRules(RulesObject rulesObject, String event) {
6262
downstreamExtractionHandler.runExtraction(rulesObject, id, event, object);
6363
}
6464
if (objects.size() == 0) {
65-
waitListStorageHandler.addEventToWaitList(event, rulesObject);
65+
waitListStorageHandler.addEventToWaitListIfNotExisting(event, rulesObject);
6666
}
6767
}
6868
}

src/main/java/com/ericsson/ei/handlers/IdRulesHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void runIdRules(RulesObject rulesObject, String event) {
6262
if (rulesObject.isStartEventRules()) {
6363
extractionHandler.runExtraction(rulesObject, id, event, (JsonNode) null);
6464
} else {
65-
waitListStorageHandler.addEventToWaitList(event, rulesObject);
65+
waitListStorageHandler.addEventToWaitListIfNotExisting(event, rulesObject);
6666
}
6767
}
6868
}

src/main/java/com/ericsson/ei/waitlist/WaitListStorageHandler.java

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import lombok.Getter;
2727
import lombok.Setter;
2828

29-
import org.json.JSONObject;
3029
import org.slf4j.Logger;
3130
import org.slf4j.LoggerFactory;
3231
import org.springframework.beans.factory.annotation.Autowired;
@@ -63,19 +62,36 @@ public class WaitListStorageHandler {
6362
@Autowired
6463
private JmesPathInterface jmesPathInterface;
6564

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) {
6772
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());
7379
}
7480
} catch (MongoWriteException e) {
7581
LOGGER.debug("Failed to insert event into waitlist.", e);
7682
}
7783
}
7884

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+
7995
public boolean dropDocumentFromWaitList(String document) {
8096
return mongoDbHandler.dropDocument(databaseName, collectionName, document);
8197
}
@@ -84,9 +100,16 @@ public List<String> getWaitList() {
84100
return mongoDbHandler.getAllDocuments(databaseName, collectionName);
85101
}
86102

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() {
90113
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
91114
Date date = new Date();
92115
String time = dateFormat.format(date);
@@ -95,12 +118,13 @@ private String addPropertiesToEvent(String event, RulesObject rulesObject) {
95118
} catch (ParseException e) {
96119
LOGGER.error("Failed to parse time from date object.", e);
97120
}
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;
104128
}
105129

106130
}

src/test/java/com/ericsson/ei/waitlist/TestWaitListStorageHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void testAddEventToWaitList(){
7171
ObjectMapper objectMapper = new ObjectMapper();
7272
eventFile = FileUtils.readFileToString(new File(eventPath), "UTF-8");
7373
rulesObject = new RulesObject(objectMapper.readTree(jsonRule));
74-
waitListStorageHandler.addEventToWaitList(eventFile, rulesObject);
74+
waitListStorageHandler.addEventToWaitListIfNotExisting(eventFile, rulesObject);
7575
assertTrue(true);
7676
} catch (Exception e) {
7777
assertFalse(true);

0 commit comments

Comments
 (0)