@@ -43,11 +43,13 @@ public class ObjectHandler {
4343
4444 static Logger log = (Logger ) LoggerFactory .getLogger (ObjectHandler .class );
4545
46- @ Getter @ Setter
46+ @ Getter
47+ @ Setter
4748 @ Value ("${aggregated.collection.name}" )
4849 private String collectionName ;
4950
50- @ Getter @ Setter
51+ @ Getter
52+ @ Setter
5153 @ Value ("${database.name}" )
5254 private String databaseName ;
5355
@@ -66,7 +68,7 @@ public class ObjectHandler {
6668 @ Setter
6769 @ Autowired
6870 private SubscriptionHandler subscriptionHandler ;
69-
71+
7072 @ Getter
7173 @ Value ("${aggregated.collection.ttlValue}" )
7274 private int ttlValue ;
@@ -81,12 +83,11 @@ public boolean insertObject(String aggregatedObject, RulesObject rulesObject, St
8183 log .debug ("ObjectHandler: Aggregated Object document to be inserted: " + document .toString ());
8284
8385 mongoDbHandler .createTTLIndex (databaseName , collectionName , "Time" , ttlValue );
84-
8586
8687 boolean result = mongoDbHandler .insertDocument (databaseName , collectionName , document .toString ());
8788 if (result )
8889 eventToObjectMap .updateEventToObjectMapInMemoryDB (rulesObject , event , id );
89- subscriptionHandler .checkSubscriptionForObject (aggregatedObject );
90+ subscriptionHandler .checkSubscriptionForObject (aggregatedObject );
9091 return result ;
9192 }
9293
@@ -95,12 +96,18 @@ public boolean insertObject(JsonNode aggregatedObject, RulesObject rulesObject,
9596 }
9697
9798 /**
98- * This method uses previously locked in database aggregatedObject (lock was set in lockDocument method)
99- * and modifies this document with the new values and removes the lock in one query
100- * @param aggregatedObject String to insert in database
101- * @param rulesObject used for fetching id
102- * @param event String to fetch id if it was not specified
103- * @param id String
99+ * This method uses previously locked in database aggregatedObject (lock was
100+ * set in lockDocument method) and modifies this document with the new
101+ * values and removes the lock in one query
102+ *
103+ * @param aggregatedObject
104+ * String to insert in database
105+ * @param rulesObject
106+ * used for fetching id
107+ * @param event
108+ * String to fetch id if it was not specified
109+ * @param id
110+ * String
104111 * @return true if operation succeed
105112 */
106113 public boolean updateObject (String aggregatedObject , RulesObject rulesObject , String event , String id ) {
@@ -109,15 +116,15 @@ public boolean updateObject(String aggregatedObject, RulesObject rulesObject, St
109116 JsonNode idNode = jmespathInterface .runRuleOnEvent (idRules , event );
110117 id = idNode .textValue ();
111118 }
112- log .debug ("ObjectHandler: Updating Aggregated Object:\n " + aggregatedObject +
113- "\n Event:\n " + event );
119+ log .debug ("ObjectHandler: Updating Aggregated Object:\n " + aggregatedObject + "\n Event:\n " + event );
114120 JsonNode document = prepareDocumentForInsertion (id , aggregatedObject );
115121 String condition = "{\" _id\" : \" " + id + "\" }" ;
116122 String documentStr = document .toString ();
117123 boolean result = mongoDbHandler .updateDocument (databaseName , collectionName , condition , documentStr );
118- if (result )
124+ if (result ) {
119125 eventToObjectMap .updateEventToObjectMapInMemoryDB (rulesObject , event , id );
120126 subscriptionHandler .checkSubscriptionForObject (aggregatedObject );
127+ }
121128 return result ;
122129 }
123130
@@ -132,9 +139,9 @@ public List<String> findObjectsByCondition(String condition) {
132139 public String findObjectById (String id ) {
133140 String condition = "{\" _id\" : \" " + id + "\" }" ;
134141 String document = findObjectsByCondition (condition ).get (0 );
135- // JsonNode result = getAggregatedObject(document);
136- // if (result != null)
137- // return result.asText();
142+ // JsonNode result = getAggregatedObject(document);
143+ // if (result != null)
144+ // return result.asText();
138145 return document ;
139146 }
140147
@@ -158,45 +165,49 @@ public JsonNode prepareDocumentForInsertion(String id, String object) {
158165
159166 return jsonNode ;
160167 } catch (Exception e ) {
161- log .info (e .getMessage (),e );
168+ log .info (e .getMessage (), e );
162169 }
163170 return null ;
164171 }
165172
166173 public JsonNode getAggregatedObject (String dbDocument ) {
167- ObjectMapper mapper = new ObjectMapper ();
168- try {
169- JsonNode documentJson = mapper .readValue (dbDocument , JsonNode .class );
170- JsonNode objectDoc = documentJson .get ("aggregatedObject" );
171- return objectDoc ;
172- } catch (Exception e ) {
173- log .info (e .getMessage (),e );
174- }
175- return null ;
174+ ObjectMapper mapper = new ObjectMapper ();
175+ try {
176+ JsonNode documentJson = mapper .readValue (dbDocument , JsonNode .class );
177+ JsonNode objectDoc = documentJson .get ("aggregatedObject" );
178+ return objectDoc ;
179+ } catch (Exception e ) {
180+ log .info (e .getMessage (), e );
181+ }
182+ return null ;
176183 }
177184
178185 public String extractObjectId (JsonNode aggregatedDbObject ) {
179186 return aggregatedDbObject .get ("_id" ).textValue ();
180187 }
181188
182189 /**
183- * Locks the document in database to achieve pessimistic locking. Method findAndModify is used to optimize
184- * the quantity of requests towards database.
185- * @param id String to search
190+ * Locks the document in database to achieve pessimistic locking. Method
191+ * findAndModify is used to optimize the quantity of requests towards
192+ * database.
193+ *
194+ * @param id
195+ * String to search
186196 * @return String aggregated document
187197 */
188- public String lockDocument (String id ){
198+ public String lockDocument (String id ) {
189199 boolean documentLocked = true ;
190200 String conditionId = "{\" _id\" : \" " + id + "\" }" ;
191201 String conditionLock = "[ { \" lock\" : null } , { \" lock\" : \" 0\" }]" ;
192202 String setLock = "{ \" $set\" : { \" lock\" : \" 1\" }}" ;
193203 ObjectMapper mapper = new ObjectMapper ();
194- while (documentLocked == true ){
204+ while (documentLocked == true ) {
195205 try {
196206 JsonNode documentJson = mapper .readValue (setLock , JsonNode .class );
197207 JsonNode queryCondition = mapper .readValue (conditionId , JsonNode .class );
198208 ((ObjectNode ) queryCondition ).set ("$or" , mapper .readValue (conditionLock , JsonNode .class ));
199- Document result = mongoDbHandler .findAndModify (databaseName , collectionName , queryCondition .toString (), documentJson .toString ());
209+ Document result = mongoDbHandler .findAndModify (databaseName , collectionName , queryCondition .toString (),
210+ documentJson .toString ());
200211 if (result != null ) {
201212 log .info ("DB locked by " + Thread .currentThread ().getId () + " thread" );
202213 documentLocked = false ;
@@ -205,7 +216,8 @@ public String lockDocument(String id){
205216 // To Remove
206217 log .info ("Waiting by " + Thread .currentThread ().getId () + " thread" );
207218 } catch (Exception e ) {
208- log .info (e .getMessage (),e ); }
219+ log .info (e .getMessage (), e );
220+ }
209221 }
210222 return null ;
211223 }
0 commit comments