17
17
package com .ericsson .ei .mongodbhandler ;
18
18
19
19
import java .util .ArrayList ;
20
+ import java .util .List ;
20
21
import java .util .concurrent .TimeUnit ;
21
22
22
23
import javax .annotation .PostConstruct ;
@@ -87,21 +88,22 @@ public void createConnection(String host, int port) {
87
88
public boolean insertDocument (String dataBaseName , String collectionName , String input ) {
88
89
try {
89
90
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
+ }
96
99
} catch (MongoWriteException e ) {
97
100
log .error (e .getMessage (), e );
98
101
}
99
102
return false ;
100
103
}
101
104
102
105
/**
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
105
107
*
106
108
* @param dataBaseName
107
109
* @param collectionName
@@ -111,15 +113,17 @@ public ArrayList<String> getAllDocuments(String dataBaseName, String collectionN
111
113
ArrayList <String > result = new ArrayList <>();
112
114
try {
113
115
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
+ }
123
127
}
124
128
} catch (Exception e ) {
125
129
log .error (e .getMessage (), e );
@@ -143,15 +147,17 @@ public ArrayList<String> find(String dataBaseName, String collectionName, String
143
147
144
148
try {
145
149
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
+ }
155
161
}
156
162
} catch (Exception e ) {
157
163
log .error (e .getMessage (), e );
@@ -161,8 +167,8 @@ public ArrayList<String> find(String dataBaseName, String collectionName, String
161
167
}
162
168
163
169
/**
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
166
172
*
167
173
* @param dataBaseName
168
174
* @param collectionName
@@ -175,12 +181,14 @@ public ArrayList<String> find(String dataBaseName, String collectionName, String
175
181
public boolean updateDocument (String dataBaseName , String collectionName , String input , String updateInput ) {
176
182
try {
177
183
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
+ }
184
192
} catch (Exception e ) {
185
193
log .error (e .getMessage (), e );
186
194
}
@@ -189,9 +197,9 @@ public boolean updateDocument(String dataBaseName, String collectionName, String
189
197
}
190
198
191
199
/**
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.
195
203
*
196
204
* @param dataBaseName
197
205
* @param collectionName
@@ -204,13 +212,15 @@ public boolean updateDocument(String dataBaseName, String collectionName, String
204
212
public Document findAndModify (String dataBaseName , String collectionName , String input , String updateInput ) {
205
213
try {
206
214
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
+ }
214
224
}
215
225
} catch (Exception e ) {
216
226
log .error (e .getMessage (), e );
@@ -231,16 +241,18 @@ public Document findAndModify(String dataBaseName, String collectionName, String
231
241
public boolean dropDocument (String dataBaseName , String collectionName , String condition ) {
232
242
try {
233
243
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
+ }
244
256
}
245
257
} catch (Exception e ) {
246
258
log .error (e .getMessage (), e );
@@ -265,12 +277,15 @@ public void createTTLIndex(String dataBaseName, String collectionName, String fi
265
277
}
266
278
267
279
private MongoCollection <Document > getMongoCollection (String dataBaseName , String collectionName ) {
280
+ if (mongoClient == null )
281
+ return null ;
268
282
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
271
286
+ ") not available in mongodb, Creating ........" );
272
287
db .createCollection (collectionName );
273
- log .info ("done...." );
288
+ log .debug ("done...." );
274
289
}
275
290
MongoCollection <Document > collection = db .getCollection (collectionName );
276
291
return collection ;
0 commit comments