Skip to content

Commit ad60fa2

Browse files
saif-ericssonvasile-baluta
authored andcommitted
Subscription for Individual Events (#80)
Added rules to create an aggregated object for each received event.
1 parent dc8cd61 commit ad60fa2

File tree

12 files changed

+1939
-73
lines changed

12 files changed

+1939
-73
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,4 +282,4 @@
282282

283283
</plugins>
284284
</build>
285-
</project>
285+
</project>

src/main/java/com/ericsson/ei/controller/SubscriptionControllerImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ public ResponseEntity<SubscriptionResponse> updateSubscriptions(@RequestBody Lis
144144
}
145145

146146
}
147+
147148
@Override
148149
@CrossOrigin
149150
@ApiOperation(value = "Removes the subscription from the database")

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ public class ObjectHandler {
6666
@Setter
6767
@Autowired
6868
private SubscriptionHandler subscriptionHandler;
69+
70+
@Getter
71+
@Value("${aggregated.collection.ttlValue}")
72+
private int ttlValue;
6973

7074
public boolean insertObject(String aggregatedObject, RulesObject rulesObject, String event, String id) {
7175
if (id == null) {
@@ -75,7 +79,10 @@ public boolean insertObject(String aggregatedObject, RulesObject rulesObject, St
7579
}
7680
JsonNode document = prepareDocumentForInsertion(id, aggregatedObject);
7781
log.debug("ObjectHandler: Aggregated Object document to be inserted: " + document.toString());
78-
82+
83+
mongoDbHandler.createTTLIndex(databaseName, collectionName, "Time", ttlValue);
84+
85+
7986
boolean result = mongoDbHandler.insertDocument(databaseName, collectionName, document.toString());
8087
if (result)
8188
eventToObjectMap.updateEventToObjectMapInMemoryDB(rulesObject, event, id);

src/main/java/com/ericsson/ei/mongodbhandler/MongoDBHandler.java

Lines changed: 72 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.ericsson.ei.mongodbhandler;
1818

1919
import java.util.ArrayList;
20+
import java.util.List;
2021
import java.util.concurrent.TimeUnit;
2122

2223
import javax.annotation.PostConstruct;
@@ -87,21 +88,22 @@ public void createConnection(String host, int port) {
8788
public boolean insertDocument(String dataBaseName, String collectionName, String input) {
8889
try {
8990
MongoCollection<Document> collection = getMongoCollection(dataBaseName, collectionName);
90-
final Document dbObjectInput = Document.parse(input);
91-
collection.insertOne(dbObjectInput);
92-
log.info("Object : " + input);
93-
log.info("inserted successfully in ");
94-
log.info("collection : " + collectionName + "and db : " + dataBaseName);
95-
return true;
91+
if (collection != null) {
92+
final Document dbObjectInput = Document.parse(input);
93+
collection.insertOne(dbObjectInput);
94+
log.debug("Object : " + input);
95+
log.debug("inserted successfully in ");
96+
log.debug("collection : " + collectionName + "and db : " + dataBaseName);
97+
return true;
98+
}
9699
} catch (MongoWriteException e) {
97100
log.error(e.getMessage(), e);
98101
}
99102
return false;
100103
}
101104

102105
/**
103-
* This method is used for the retrieve the all documents from the
104-
* collection
106+
* This method is used for the retrieve the all documents from the collection
105107
*
106108
* @param dataBaseName
107109
* @param collectionName
@@ -111,15 +113,17 @@ public ArrayList<String> getAllDocuments(String dataBaseName, String collectionN
111113
ArrayList<String> result = new ArrayList<>();
112114
try {
113115
MongoCollection<Document> collection = getMongoCollection(dataBaseName, collectionName);
114-
collection.find(new BasicDBObject()).forEach((Block<Document>) document -> {
115-
result.add(JSON.serialize(document));
116-
});
117-
if (result.size() != 0) {
118-
log.debug("getAllDocuments() :: database: " + dataBaseName + " and collection: " + collectionName
119-
+ " fetched No of :" + result.size());
120-
} else {
121-
log.debug("getAllDocuments() :: database: " + dataBaseName + "and collection: " + collectionName
122-
+ " documents are not found");
116+
if (collection != null) {
117+
collection.find(new BasicDBObject()).forEach((Block<Document>) document -> {
118+
result.add(JSON.serialize(document));
119+
});
120+
if (result.size() != 0) {
121+
log.debug("getAllDocuments() :: database: " + dataBaseName + " and collection: " + collectionName
122+
+ " fetched No of :" + result.size());
123+
} else {
124+
log.debug("getAllDocuments() :: database: " + dataBaseName + "and collection: " + collectionName
125+
+ " documents are not found");
126+
}
123127
}
124128
} catch (Exception e) {
125129
log.error(e.getMessage(), e);
@@ -143,15 +147,17 @@ public ArrayList<String> find(String dataBaseName, String collectionName, String
143147

144148
try {
145149
MongoCollection<Document> collection = getMongoCollection(dataBaseName, collectionName);
146-
collection.find(BasicDBObject.parse(condition)).forEach((Block<Document>) document -> {
147-
result.add(JSON.serialize(document));
148-
});
149-
if (result.size() != 0) {
150-
log.debug("find() :: database: " + dataBaseName + " and collection: " + collectionName
151-
+ " fetched No of :" + result.size());
152-
} else {
153-
log.debug("find() :: database: " + dataBaseName + " and collection: " + collectionName
154-
+ " documents are not found");
150+
if (collection != null) {
151+
collection.find(BasicDBObject.parse(condition)).forEach((Block<Document>) document -> {
152+
result.add(JSON.serialize(document));
153+
});
154+
if (result.size() != 0) {
155+
log.debug("find() :: database: " + dataBaseName + " and collection: " + collectionName
156+
+ " fetched No of :" + result.size());
157+
} else {
158+
log.debug("find() :: database: " + dataBaseName + " and collection: " + collectionName
159+
+ " documents are not found");
160+
}
155161
}
156162
} catch (Exception e) {
157163
log.error(e.getMessage(), e);
@@ -161,8 +167,8 @@ public ArrayList<String> find(String dataBaseName, String collectionName, String
161167
}
162168

163169
/**
164-
* This method is used for update the document in collection and remove the
165-
* lock in one query. Lock is needed for multi process execution
170+
* This method is used for update the document in collection and remove the lock
171+
* in one query. Lock is needed for multi process execution
166172
*
167173
* @param dataBaseName
168174
* @param collectionName
@@ -175,12 +181,14 @@ public ArrayList<String> find(String dataBaseName, String collectionName, String
175181
public boolean updateDocument(String dataBaseName, String collectionName, String input, String updateInput) {
176182
try {
177183
MongoCollection<Document> collection = getMongoCollection(dataBaseName, collectionName);
178-
final Document dbObjectInput = Document.parse(input);
179-
final Document dbObjectUpdateInput = Document.parse(updateInput);
180-
UpdateResult updateMany = collection.replaceOne(dbObjectInput, dbObjectUpdateInput);
181-
log.debug("updateDocument() :: database: " + dataBaseName + " and collection: " + collectionName
182-
+ " is document Updated :" + updateMany.wasAcknowledged());
183-
return updateMany.wasAcknowledged();
184+
if (collection != null) {
185+
final Document dbObjectInput = Document.parse(input);
186+
final Document dbObjectUpdateInput = Document.parse(updateInput);
187+
UpdateResult updateMany = collection.replaceOne(dbObjectInput, dbObjectUpdateInput);
188+
log.debug("updateDocument() :: database: " + dataBaseName + " and collection: " + collectionName
189+
+ " is document Updated :" + updateMany.wasAcknowledged());
190+
return updateMany.wasAcknowledged();
191+
}
184192
} catch (Exception e) {
185193
log.error(e.getMessage(), e);
186194
}
@@ -189,9 +197,9 @@ public boolean updateDocument(String dataBaseName, String collectionName, String
189197
}
190198

191199
/**
192-
* This method is used for lock and return the document that matches the
193-
* input condition in one query. Lock is needed for multi process execution.
194-
* This method is executed in a loop.
200+
* This method is used for lock and return the document that matches the input
201+
* condition in one query. Lock is needed for multi process execution. This
202+
* method is executed in a loop.
195203
*
196204
* @param dataBaseName
197205
* @param collectionName
@@ -204,13 +212,15 @@ public boolean updateDocument(String dataBaseName, String collectionName, String
204212
public Document findAndModify(String dataBaseName, String collectionName, String input, String updateInput) {
205213
try {
206214
MongoCollection<Document> collection = getMongoCollection(dataBaseName, collectionName);
207-
final Document dbObjectInput = Document.parse(input);
208-
final Document dbObjectUpdateInput = Document.parse(updateInput);
209-
Document result = collection.findOneAndUpdate(dbObjectInput, dbObjectUpdateInput);
210-
if (result != null) {
211-
log.debug("updateDocument() :: database: " + dataBaseName + " and collection: " + collectionName
212-
+ " updated successfully");
213-
return result;
215+
if (collection != null) {
216+
final Document dbObjectInput = Document.parse(input);
217+
final Document dbObjectUpdateInput = Document.parse(updateInput);
218+
Document result = collection.findOneAndUpdate(dbObjectInput, dbObjectUpdateInput);
219+
if (result != null) {
220+
log.debug("updateDocument() :: database: " + dataBaseName + " and collection: " + collectionName
221+
+ " updated successfully");
222+
return result;
223+
}
214224
}
215225
} catch (Exception e) {
216226
log.error(e.getMessage(), e);
@@ -231,16 +241,18 @@ public Document findAndModify(String dataBaseName, String collectionName, String
231241
public boolean dropDocument(String dataBaseName, String collectionName, String condition) {
232242
try {
233243
MongoCollection<Document> collection = getMongoCollection(dataBaseName, collectionName);
234-
final Document dbObjectCondition = Document.parse(condition);
235-
DeleteResult deleteMany = collection.deleteMany(dbObjectCondition);
236-
if (deleteMany.getDeletedCount() > 0) {
237-
log.debug("database" + dataBaseName + " and collection: " + collectionName + " deleted No.of records "
238-
+ deleteMany.getDeletedCount());
239-
return true;
240-
} else {
241-
log.debug("database " + dataBaseName + " and collection: " + collectionName
242-
+ " No documents found to delete");
243-
return false;
244+
if (collection != null) {
245+
final Document dbObjectCondition = Document.parse(condition);
246+
DeleteResult deleteMany = collection.deleteMany(dbObjectCondition);
247+
if (deleteMany.getDeletedCount() > 0) {
248+
log.debug("database" + dataBaseName + " and collection: " + collectionName
249+
+ " deleted No.of records " + deleteMany.getDeletedCount());
250+
return true;
251+
} else {
252+
log.debug("database " + dataBaseName + " and collection: " + collectionName
253+
+ " No documents found to delete");
254+
return false;
255+
}
244256
}
245257
} catch (Exception e) {
246258
log.error(e.getMessage(), e);
@@ -265,12 +277,15 @@ public void createTTLIndex(String dataBaseName, String collectionName, String fi
265277
}
266278

267279
private MongoCollection<Document> getMongoCollection(String dataBaseName, String collectionName) {
280+
if (mongoClient == null)
281+
return null;
268282
MongoDatabase db = mongoClient.getDatabase(dataBaseName);
269-
if (!db.listCollectionNames().into(new ArrayList<String>()).contains(collectionName)) {
270-
log.info("The requested database(" + dataBaseName + ") / collection(" + collectionName
283+
List<String> collectionList = db.listCollectionNames().into(new ArrayList<String>());
284+
if (!collectionList.contains(collectionName)) {
285+
log.debug("The requested database(" + dataBaseName + ") / collection(" + collectionName
271286
+ ") not available in mongodb, Creating ........");
272287
db.createCollection(collectionName);
273-
log.info("done....");
288+
log.debug("done....");
274289
}
275290
MongoCollection<Document> collection = db.getCollection(collectionName);
276291
return collection;

0 commit comments

Comments
 (0)