11
11
See the License for the specific language governing permissions and
12
12
limitations under the License.
13
13
*/
14
- package com .ericsson .ei .mongodbhandler ;
14
+ package com .ericsson .ei .handlers ;
15
15
16
16
import java .util .ArrayList ;
17
17
import java .util .Collections ;
21
21
import javax .annotation .PostConstruct ;
22
22
import javax .annotation .PreDestroy ;
23
23
24
+ import com .mongodb .*;
24
25
import org .apache .commons .lang3 .StringUtils ;
25
26
import org .bson .Document ;
26
27
import org .slf4j .Logger ;
30
31
import org .springframework .stereotype .Component ;
31
32
32
33
import com .fasterxml .jackson .annotation .JsonIgnore ;
33
- import com .mongodb .BasicDBObject ;
34
- import com .mongodb .Block ;
35
- import com .mongodb .MongoClient ;
36
- import com .mongodb .MongoCommandException ;
37
- import com .mongodb .MongoCredential ;
38
- import com .mongodb .MongoWriteException ;
39
- import com .mongodb .ServerAddress ;
40
34
import com .mongodb .client .MongoCollection ;
41
35
import com .mongodb .client .MongoDatabase ;
42
36
import com .mongodb .client .model .IndexOptions ;
50
44
51
45
@ Component
52
46
public class MongoDBHandler {
53
- static Logger log = LoggerFactory .getLogger (MongoDBHandler .class );
47
+ private static Logger LOGGER = LoggerFactory .getLogger (MongoDBHandler .class );
54
48
55
49
@ Autowired
56
50
private MongoProperties mongoProperties ;
@@ -66,7 +60,7 @@ public class MongoDBHandler {
66
60
public void init () {
67
61
createConnection ();
68
62
}
69
-
63
+
70
64
@ PreDestroy
71
65
public void close () {
72
66
mongoClient .close ();
@@ -93,21 +87,14 @@ private void createConnection() {
93
87
* @param input json String
94
88
* @return
95
89
*/
96
- public boolean insertDocument (String dataBaseName , String collectionName , String input ) {
97
- try {
98
- MongoCollection <Document > collection = getMongoCollection (dataBaseName , collectionName );
99
- if (collection != null ) {
100
- final Document dbObjectInput = Document .parse (input );
101
- collection .insertOne (dbObjectInput );
102
- log .debug ("Object : " + input );
103
- log .debug ("inserted successfully in " );
104
- log .debug ("collection : " + collectionName + "and db : " + dataBaseName );
105
- return true ;
106
- }
107
- } catch (MongoWriteException e ) {
108
- log .error (e .getMessage (), e );
90
+ public void insertDocument (String dataBaseName , String collectionName , String input ) throws MongoWriteException {
91
+ MongoCollection <Document > collection = getMongoCollection (dataBaseName , collectionName );
92
+ if (collection != null ) {
93
+ final Document dbObjectInput = Document .parse (input );
94
+ collection .insertOne (dbObjectInput );
95
+ LOGGER .debug ("Object: " + input + "\n was inserted successfully in collection: \n "
96
+ + collectionName + " and database " + dataBaseName + "." );
109
97
}
110
- return false ;
111
98
}
112
99
113
100
/**
@@ -128,12 +115,12 @@ public ArrayList<String> getAllDocuments(String dataBaseName, String collectionN
128
115
if (result .size () != 0 ) {
129
116
// This will pass about 10 times/second and most of the times DB will be empty,
130
117
// this is normal, no need to log
131
- log .debug ("getAllDocuments() :: database: " + dataBaseName + " and collection: " + collectionName
118
+ LOGGER .debug ("getAllDocuments() :: database: " + dataBaseName + " and collection: " + collectionName
132
119
+ " fetched No of :" + result .size ());
133
120
}
134
121
}
135
122
} catch (Exception e ) {
136
- log .error (e .getMessage (), e );
123
+ LOGGER .error (e .getMessage (), e );
137
124
}
138
125
return result ;
139
126
}
@@ -149,7 +136,7 @@ public ArrayList<String> getAllDocuments(String dataBaseName, String collectionN
149
136
public ArrayList <String > find (String dataBaseName , String collectionName , String condition ) {
150
137
ArrayList <String > result = new ArrayList <>();
151
138
152
- log .debug ("Find and retrieve data from database." + "\n Database: " + dataBaseName + "\n Collection: "
139
+ LOGGER .debug ("Find and retrieve data from database." + "\n Database: " + dataBaseName + "\n Collection: "
153
140
+ collectionName + "\n Condition/Query: " + condition );
154
141
155
142
try {
@@ -159,17 +146,17 @@ public ArrayList<String> find(String dataBaseName, String collectionName, String
159
146
result .add (JSON .serialize (document ));
160
147
});
161
148
if (result .size () != 0 ) {
162
- log .debug ("find() :: database: " + dataBaseName + " and collection: " + collectionName
149
+ LOGGER .debug ("find() :: database: " + dataBaseName + " and collection: " + collectionName
163
150
+ " fetched No of :" + result .size ());
164
151
} else {
165
- log .debug ("find() :: database: " + dataBaseName + " and collection: " + collectionName
152
+ LOGGER .debug ("find() :: database: " + dataBaseName + " and collection: " + collectionName
166
153
+ " documents are not found" );
167
154
}
168
155
} else {
169
- log .debug ("Collection " + collectionName + " is empty in database " + dataBaseName );
156
+ LOGGER .debug ("Collection " + collectionName + " is empty in database " + dataBaseName );
170
157
}
171
158
} catch (Exception e ) {
172
- log .error (e .getMessage (), e );
159
+ LOGGER .error (e .getMessage (), e );
173
160
}
174
161
175
162
return result ;
@@ -192,12 +179,12 @@ public boolean updateDocument(String dataBaseName, String collectionName, String
192
179
final Document dbObjectInput = Document .parse (input );
193
180
final Document dbObjectUpdateInput = Document .parse (updateInput );
194
181
UpdateResult updateMany = collection .replaceOne (dbObjectInput , dbObjectUpdateInput );
195
- log .debug ("updateDocument() :: database: " + dataBaseName + " and collection: " + collectionName
182
+ LOGGER .debug ("updateDocument() :: database: " + dataBaseName + " and collection: " + collectionName
196
183
+ " is document Updated :" + updateMany .wasAcknowledged ());
197
184
return updateMany .wasAcknowledged ();
198
185
}
199
186
} catch (Exception e ) {
200
- log .error (e .getMessage (), e );
187
+ LOGGER .error (e .getMessage (), e );
201
188
}
202
189
203
190
return false ;
@@ -221,13 +208,13 @@ public Document findAndModify(String dataBaseName, String collectionName, String
221
208
final Document dbObjectUpdateInput = Document .parse (updateInput );
222
209
Document result = collection .findOneAndUpdate (dbObjectInput , dbObjectUpdateInput );
223
210
if (result != null ) {
224
- log .debug ("updateDocument() :: database: " + dataBaseName + " and collection: " + collectionName
211
+ LOGGER .debug ("updateDocument() :: database: " + dataBaseName + " and collection: " + collectionName
225
212
+ " updated successfully" );
226
213
return result ;
227
214
}
228
215
}
229
216
} catch (Exception e ) {
230
- log .error (e .getMessage (), e );
217
+ LOGGER .error (e .getMessage (), e );
231
218
}
232
219
return null ;
233
220
}
@@ -247,17 +234,17 @@ public boolean dropDocument(String dataBaseName, String collectionName, String c
247
234
final Document dbObjectCondition = Document .parse (condition );
248
235
DeleteResult deleteMany = collection .deleteMany (dbObjectCondition );
249
236
if (deleteMany .getDeletedCount () > 0 ) {
250
- log .debug ("database" + dataBaseName + " and collection: " + collectionName
237
+ LOGGER .debug ("database" + dataBaseName + " and collection: " + collectionName
251
238
+ " deleted No.of records " + deleteMany .getDeletedCount ());
252
239
return true ;
253
240
} else {
254
- log .debug ("database " + dataBaseName + " and collection: " + collectionName
241
+ LOGGER .debug ("database " + dataBaseName + " and collection: " + collectionName
255
242
+ " No documents found to delete" );
256
243
return false ;
257
244
}
258
245
}
259
246
} catch (Exception e ) {
260
- log .error (e .getMessage (), e );
247
+ LOGGER .error (e .getMessage (), e );
261
248
}
262
249
return false ;
263
250
}
@@ -282,19 +269,19 @@ private MongoCollection<Document> getMongoCollection(String dataBaseName, String
282
269
MongoDatabase db = mongoClient .getDatabase (dataBaseName );
283
270
List <String > collectionList = db .listCollectionNames ().into (new ArrayList <String >());
284
271
if (!collectionList .contains (collectionName )) {
285
- log .debug ("The requested database(" + dataBaseName + ") / collection(" + collectionName
272
+ LOGGER .debug ("The requested database(" + dataBaseName + ") / collection(" + collectionName
286
273
+ ") not available in mongodb, Creating ........" );
287
274
try {
288
275
db .createCollection (collectionName );
289
276
} catch (MongoCommandException e ) {
290
277
String message = "collection '" + dataBaseName + "." + collectionName + "' already exists" ;
291
278
if (e .getMessage ().contains (message )) {
292
- log .warn ("A " + message + "." );
279
+ LOGGER .warn ("A " + message + "." );
293
280
} else {
294
281
throw e ;
295
282
}
296
283
}
297
- log .debug ("done...." );
284
+ LOGGER .debug ("done...." );
298
285
}
299
286
MongoCollection <Document > collection = db .getCollection (collectionName );
300
287
return collection ;
0 commit comments