1 package wrappers;
2 
3 //import java.math.BigDecimal;
4 import java.math.BigInteger;
5 import java.util.ArrayList;
6 import java.util.Date;
7 import java.util.List;
8 import java.util.Set;
9 
10 import org.bson.BsonArray;
11 import org.bson.BsonBoolean;
12 import org.bson.BsonDateTime;
13 //import org.bson.BsonDecimal128;
14 import org.bson.BsonDocument;
15 import org.bson.BsonDouble;
16 import org.bson.BsonInt32;
17 import org.bson.BsonInt64;
18 import org.bson.BsonNull;
19 import org.bson.BsonString;
20 import org.bson.BsonValue;
21 import org.bson.Document;
22 import org.bson.conversions.Bson;
23 //import org.bson.types.Decimal128;
24 
25 import com.mongodb.MongoClient;
26 import com.mongodb.MongoClientURI;
27 import com.mongodb.MongoException;
28 import com.mongodb.client.AggregateIterable;
29 import com.mongodb.client.FindIterable;
30 import com.mongodb.client.MongoCollection;
31 import com.mongodb.client.MongoCursor;
32 import com.mongodb.client.MongoDatabase;
33 import com.mongodb.client.model.Filters;
34 import com.mongodb.client.result.DeleteResult;
35 import com.mongodb.client.result.UpdateResult;
36 
37 public class Mongo3Interface {
38 	boolean DEBUG = false;
39 	String Errmsg = "No error";
40 	String bvalName = null;
41 	Set<String> Colnames = null;
42 	MongoClient client = null;
43 	MongoDatabase db = null;
44 	MongoCollection<BsonDocument> coll = null;
45 	FindIterable<BsonDocument> finditer = null;
46 	AggregateIterable<BsonDocument> aggiter = null;
47 	MongoCursor<BsonDocument> cursor = null;
48 	BsonDocument doc = null;
49 	BsonDocument util = null;
50 	BsonNull bsonull = new BsonNull();
51 
52 	// === Constructors/finalize =========================================
53 	public Mongo3Interface() {
54 		this(false);
55 	} // end of default constructor
56 
57 	public Mongo3Interface(boolean b) {
58 		DEBUG = b;
59 	} // end of constructor
60 
61 	protected void SetErrmsg(String str) {
62 		if (DEBUG)
63 			System.out.println(str);
64 
65 		Errmsg = str;
66 	} // end of SetErrmsg
67 
68 	protected void SetErrmsg(Exception e) {
69 		if (DEBUG)
70 			System.out.println(e.getMessage());
71 
72 		Errmsg = e.toString();
73 	} // end of SetErrmsg
74 
75 	public String GetErrmsg() {
76 		String err = Errmsg;
77 
78 		Errmsg = "No error";
79 		return err;
80 	} // end of GetErrmsg
81 
82 	public int MongoConnect(String[] parms) {
83 		int rc = 0;
84 
85 		if (DEBUG)
86 			System.out.println("Mongo3: URI=" + parms[0] + " DB=" + parms[1]);
87 
88 		try {
89 			MongoClientURI uri = new MongoClientURI(parms[0]);
90 
91 			client = new MongoClient(uri);
92 
93 			if (DEBUG)
94 				System.out.println("Connection " + client.toString() + " established");
95 
96 			// Now connect to your databases
97 			db = client.getDatabase(parms[1]);
98 
99 			// if (parms[2] != null && !parms[2].isEmpty()) {
100 			// if (DEBUG)
101 			// System.out.println("user=" + parms[2] + " pwd=" + parms[3]);
102 
103 			// @SuppressWarnings("deprecation")
104 			// boolean auth = db.authenticate(parms[2], parms[3].toCharArray());
105 
106 			// if (DEBUG)
107 			// System.out.println("Authentication: " + auth);
108 
109 			// } // endif user
110 
111 		} catch (MongoException me) {
112 			SetErrmsg(me);
113 			rc = -1;
114 		} catch (Exception e) {
115 			SetErrmsg(e);
116 			rc = -3;
117 		} // end try/catch
118 
119 		return rc;
120 	} // end of MongoConnect
121 
122 	public int MongoDisconnect() {
123 		int rc = 0;
124 
125 		try {
126 			if (cursor != null) {
127 				if (DEBUG)
128 					System.out.println("Closing cursor");
129 
130 				cursor.close();
131 				cursor = null;
132 			} // endif client
133 
134 			if (client != null) {
135 				if (DEBUG)
136 					System.out.println("Closing connection");
137 
138 				client.close();
139 				client = null;
140 			} // endif client
141 
142 		} catch (MongoException se) {
143 			SetErrmsg(se);
144 			rc += 8;
145 		} // end try/catch
146 
147 		return rc;
148 	} // end of MongoDisconnect
149 
150 	public boolean GetCollection(String name) {
151 		if (DEBUG)
152 			System.out.println("GetCollection: name=" + name);
153 
154 		try {
155 			coll = db.getCollection(name).withDocumentClass(BsonDocument.class);
156 		} catch (Exception e) {
157 			SetErrmsg(e);
158 			return true;
159 		} // end try/catch
160 
161 		return false;
162 	} // end of GetCollection
163 
164 	public long GetCollSize() {
165 		return (coll != null) ? coll.count() : 0;
166 	} // end of GetCollSize
167 
168 	public boolean FindColl(String query, String fields) {
169 		if (DEBUG)
170 			System.out.println("FindColl: query=" + query + " fields=" + fields);
171 
172 		try {
173 			if (query != null) {
174 				Bson dbq = Document.parse(query);
175 				finditer = coll.find(dbq);
176 			} else
177 				finditer = coll.find();
178 
179 			if (fields != null) {
180 				Bson dbf = BsonDocument.parse(fields);
181 				finditer = finditer.projection(dbf);
182 			} // endif fields
183 
184 			cursor = finditer.iterator();
185 		} catch (Exception e) {
186 			SetErrmsg(e);
187 			return true;
188 		} // end try/catch
189 
190 		return false;
191 	} // end of FindColl
192 
193 	@SuppressWarnings("unchecked")
194 	public boolean AggregateColl(String pipeline) {
195 		if (DEBUG)
196 			System.out.println("AggregateColl: pipeline=" + pipeline);
197 
198 			try {
199 				Document pipe = Document.parse(pipeline);
200 				ArrayList<?> pip = (ArrayList<?>) pipe.get("pipeline");
201 
202 			aggiter = coll.aggregate((List<? extends Bson>) pip);
203 			cursor = aggiter.iterator();
204 			} catch (MongoException me) {
205 				SetErrmsg(me);
206 			return true;
207 			} // end try/catch
208 
209 		return false;
210 	} // end of AggregateColl
211 
212 	public boolean Rewind() {
213 		if (cursor != null)
214 			cursor.close();
215 
216 		if (finditer != null)
217 			cursor = finditer.iterator();
218 		else if (aggiter != null)
219 			cursor = aggiter.iterator();
220 
221 		return (cursor == null);
222 	} // end of Rewind
223 
224 	public int ReadNext() {
225 		try {
226 			if (cursor.hasNext()) {
227 				doc = cursor.next();
228 
229 				if (DEBUG)
230 					System.out.println("Class doc = " + doc.getClass());
231 
232 				Colnames = doc.keySet();
233 				return Colnames.size();
234 			} else
235 				return 0;
236 
237 		} catch (MongoException mx) {
238 			SetErrmsg(mx);
239 		} // end try/catch
240 
241 		return -1;
242 	} // end of ReadNext
243 
244 	public boolean Fetch(int row) {
245 		if (cursor.hasNext()) {
246 			doc = cursor.next();
247 			Colnames = doc.keySet();
248 			return true;
249 		} else
250 			return false;
251 
252 	} // end of Fetch
253 
254 	public String GetDoc() {
255 		return (doc != null) ? doc.toJson() : null;
256 	} // end of GetDoc
257 
258 	public Set<String> GetColumns() {
259 		if (doc != null)
260 			return doc.keySet();
261 		else
262 			return null;
263 
264 	} // end of GetColumns
265 
266 	public String ColumnName(int n) {
267 		if (n < Colnames.size())
268 			return (String) Colnames.toArray()[n];
269 		else
270 			return null;
271 
272 	} // end of ColumnName
273 
274 	public int ColumnType(int n, String name) {
275 		// if (rsmd == null) {
276 		// System.out.println("No result metadata");
277 		// } else try {
278 		// if (n == 0)
279 		// n = rs.findColumn(name);
280 
281 		// return rsmd.getColumnType(n);
282 		// } catch (SQLException se) {
283 		// SetErrmsg(se);
284 		// } //end try/catch
285 
286 		return 666; // Not a type
287 	} // end of ColumnType
288 
289 	public Object ColumnDesc(Object obj, int n, int[] val, int lvl) {
290 		Object ret = null;
291 		BsonValue bval = (BsonValue) ((obj != null) ? obj : doc);
292 		BsonDocument dob = (bval instanceof BsonDocument) ? (BsonDocument) bval : null;
293 		BsonArray ary = (bval instanceof BsonArray) ? (BsonArray) bval : null;
294 
295 		try {
296 			if (ary != null) {
297 				bval = ary.get(n);
298 				bvalName = Integer.toString(n);
299 			} else if (dob != null) {
300 				// String[] k = dob.keySet().toArray(new String[0]);
301 				Object[] k = dob.keySet().toArray();
302 				bval = dob.get(k[n]);
303 				bvalName = (String) k[n];
304 			} else
305 				bvalName = "x" + Integer.toString(n);
306 
307 			val[0] = 0; // ColumnType
308 			val[1] = 0; // Precision
309 			val[2] = 0; // Scale
310 			val[3] = 0; // Nullable
311 			val[4] = 0; // ncol
312 
313 			if (bval.isString()) {
314 				val[0] = 1;
315 				val[1] = bval.asString().getValue().length();
316 			} else if (bval.isInt32()) {
317 				val[0] = 7;
318 				val[1] = Integer.toString(bval.asInt32().getValue()).length();
319 			} else if (bval.isInt64()) {
320 				val[0] = 5;
321 				val[1] = Long.toString(bval.asInt64().getValue()).length();
322 			} else if (bval.isObjectId()) {
323 				val[0] = 1;
324 				val[1] = bval.asObjectId().getValue().toString().length();
325 			} else if (bval.isDateTime()) {
326 				Long TS = (bval.asDateTime().getValue() / 1000);
327 				val[0] = 8;
328 				val[1] = TS.toString().length();
329 			} else if (bval.isDouble()) {
330 				String d = Double.toString(bval.asDouble().getValue());
331 				int i = d.indexOf('.') + 1;
332 
333 				val[0] = 2;
334 				val[1] = d.length();
335 				val[2] = (i > 0) ? val[1] - i : 0;
336 			} else if (bval.isBoolean()) {
337 				val[0] = 4;
338 				val[1] = 1;
339 			} else if (bval.isDocument()) {
340 				if (lvl > 0) {
341 					ret = bval;
342 					val[0] = 1;
343 					val[4] = bval.asDocument().keySet().size();
344 				} else if (lvl == 0) {
345 					val[0] = 1;
346 					val[1] = bval.asDocument().toJson().length();
347 				} // endif lvl
348 
349 			} else if (bval.isArray()) {
350 				if (lvl > 0) {
351 					ret = bval;
352 					val[0] = 2;
353 					val[4] = bval.asArray().size();
354 				} else if (lvl == 0) {
355 					val[0] = 1;
356 					util = new BsonDocument("arr", bval.asArray());
357 					String s = util.toJson();
358 					int i1 = s.indexOf('[');
359 					int i2 = s.lastIndexOf(']');
360 					val[1] = i2 - i1 + 1;
361 				} // endif lvl
362 
363 			} else if (bval.isDecimal128()) {
364 				val[0] = 9;
365 				val[1] = bval.asDecimal128().toString().length();
366 			} else if (bval.isNull()) {
367 				val[0] = 0;
368 				val[3] = 1;
369 			} else {
370 				SetErrmsg("Type " + bval.getBsonType() + " of " + bvalName + " not supported");
371 				val[0] = -1;
372 			} // endif's
373 
374 			return ret;
375 		} catch (Exception ex) {
376 			SetErrmsg(ex);
377 		} // end try/catch
378 
379 		val[0] = -1;
380 		return null;
381 	} // end of ColumnDesc
382 
383 	public String ColDescName() {
384 		return bvalName;
385 	} // end of ColDescName
386 
387 	protected BsonValue GetFieldObject(String path) {
388 		BsonValue o = doc;
389 		BsonDocument dob = null;
390 		BsonArray ary = null;
391 		String[] names = null;
392 
393 		if (path == null || path.equals("") || path.equals("*"))
394 			return doc;
395 		else if (o instanceof BsonDocument)
396 			dob = doc;
397 		else if (o instanceof BsonArray)
398 			ary = (BsonArray) o;
399 		else
400 			return doc;
401 
402 		try {
403 			names = path.split("\\.");
404 
405 			for (String name : names) {
406 				if (ary != null) {
407 					o = ary.get(Integer.parseInt(name));
408 				} else
409 					o = dob.get(name);
410 
411 				if (o == null)
412 					break;
413 
414 				if (DEBUG)
415 					System.out.println("Class o = " + o.getClass());
416 
417 				if (o instanceof BsonDocument) {
418 					dob = (BsonDocument) o;
419 					ary = null;
420 				} else if (o instanceof BsonArray) {
421 					ary = (BsonArray) o;
422 				} else
423 					break;
424 
425 			} // endfor name
426 
427 		} catch (IndexOutOfBoundsException x) {
428 			o = null;
429 		} catch (MongoException me) {
430 			SetErrmsg(me);
431 			o = null;
432 		} // end try/catch
433 
434 		return o;
435 	} // end of GetFieldObject
436 
437 	public String GetField(String path) {
438 		BsonValue o = GetFieldObject(path);
439 
440 		if (o != null) {
441 			if (o.isString()) {
442 				return o.asString().getValue();
443 			} else if (o.isInt32()) {
444 				return Integer.toString(o.asInt32().getValue());
445 			} else if (o.isInt64()) {
446 				return Long.toString(o.asInt64().getValue());
447 			} else if (o.isObjectId()) {
448 				return o.asObjectId().getValue().toString();
449 			} else if (o.isDateTime()) {
450 				Integer TS = (int) (o.asDateTime().getValue() / 1000);
451 				return TS.toString();
452 			} else if (o.isDouble()) {
453 				return Double.toString(o.asDouble().getValue());
454 			} else if (o.isBoolean()) {
455 				return o.asBoolean().getValue() ? "1" : "0";
456 			} else if (o.isDocument()) {
457 				return o.asDocument().toJson();
458 			} else if (o.isArray()) {
459 				util = new BsonDocument("arr", o.asArray());
460 				String s = util.toJson();
461 				int i1 = s.indexOf('[');
462 				int i2 = s.lastIndexOf(']');
463 				return s.substring(i1, i2 + 1);
464 			} else if (o.isDecimal128()) {
465 				return o.asDecimal128().toString();
466 			} else if (o.isNull()) {
467 				return null;
468 			} else
469 				return o.toString();
470 
471 		} else
472 			return null;
473 
474 	} // end of GetField
475 
476 	public Object MakeBson(String s, int json) {
477 		BsonValue bval;
478 
479 		if (json == 1)
480 			bval = BsonDocument.parse(s);
481 		else if (json == 2)
482 			bval = BsonArray.parse(s);
483 		else
484 			bval = null;
485 
486 		return bval;
487 	} // end of MakeBson
488 
489 	protected BsonValue ObjToBson(Object val, int json) {
490 		BsonValue bval = null;
491 
492 		if (val == null)
493 			bval = bsonull;
494 		else if (val.getClass() == String.class) {
495 			if (json == 1)
496 				bval = BsonDocument.parse((String) val);
497 			else if (json == 2)
498 				bval = BsonArray.parse((String) val);
499 			else
500 				bval = new BsonString((String) val);
501 
502 		} else if (val.getClass() == Integer.class)
503 			bval = new BsonInt32((int) val);
504 		else if (val.getClass() == Double.class)
505 			bval = new BsonDouble((double) val);
506 		else if (val.getClass() == BigInteger.class)
507 			bval = new BsonInt64((long) val);
508 		else if (val.getClass() == Boolean.class)
509 			bval = new BsonBoolean((Boolean) val);
510 		else if (val.getClass() == Date.class)
511 			bval = new BsonDateTime(((Date) val).getTime() * 1000);
512 		else if (val.getClass() == BsonDocument.class)
513 			bval = (BsonDocument) val;
514 		else if (val.getClass() == BsonArray.class)
515 			bval = (BsonArray) val;
516 		// else if (val.getClass() == BigDecimal.class)
517 		// bval = new BsonDecimal128((BigDecimal) val);
518 
519 		return bval;
520 	} // end of ObjToBson
521 
522 	public Object MakeDocument() {
523 		return new BsonDocument();
524 	} // end of MakeDocument
525 
526 	public boolean DocAdd(Object bdc, String key, Object val, int json) {
527 		try {
528 			((BsonDocument) bdc).append(key, ObjToBson(val, json));
529 		} catch (MongoException me) {
530 			SetErrmsg(me);
531 			return true;
532 		} // end try/catch
533 
534 		return false;
535 	} // end of DocAdd
536 
537 	public Object MakeArray() {
538 		return new BsonArray();
539 	} // end of MakeArray
540 
541 	public boolean ArrayAdd(Object bar, int n, Object val, int json) {
542 		try {
543 			for (int i = ((BsonArray) bar).size(); i < n; i++)
544 				((BsonArray) bar).add(bsonull);
545 
546 			((BsonArray) bar).add(ObjToBson(val, json));
547 		} catch (MongoException me) {
548 			SetErrmsg(me);
549 			return true;
550 		} catch (Exception ex) {
551 			SetErrmsg(ex);
552 			return true;
553 		} // end try/catch
554 
555 		return false;
556 	} // end of ArrayAdd
557 
558 	public boolean CollInsert(Object dob) {
559 		try {
560 			coll.insertOne((BsonDocument) dob);
561 		} catch (MongoException me) {
562 			SetErrmsg(me);
563 			return true;
564 		} catch (Exception ex) {
565 			SetErrmsg(ex);
566 			return true;
567 		} // end try/catch
568 
569 		return false;
570 	} // end of CollInsert
571 
572 	public long CollUpdate(Object upd) {
573 		long n = -1;
574 
575 		if (DEBUG)
576 			System.out.println("upd: " + upd.toString());
577 
578 		try {
579 			UpdateResult res = coll.updateOne(Filters.eq("_id", doc.get("_id")), (Bson) upd);
580 
581 			if (DEBUG)
582 				System.out.println("CollUpdate: " + res.toString());
583 
584 			n = res.getModifiedCount();
585 		} catch (MongoException me) {
586 			SetErrmsg(me);
587 		} catch (Exception ex) {
588 			SetErrmsg(ex);
589 		} // end try/catch
590 
591 		return n;
592 	} // end of CollUpdate
593 
594 	public long CollDelete(boolean all) {
595 		long n = -1;
596 
597 		try {
598 			DeleteResult res;
599 
600 			if (all)
601 				res = coll.deleteMany(new Document());
602 			else
603 				res = coll.deleteOne(Filters.eq("_id", doc.get("_id")));
604 
605 			if (DEBUG)
606 				System.out.println("CollDelete: " + res.toString());
607 
608 			n = res.getDeletedCount();
609 		} catch (MongoException me) {
610 			SetErrmsg(me);
611 		} catch (Exception ex) {
612 			SetErrmsg(ex);
613 		} // end try/catch
614 
615 		return n;
616 	} // end of CollDelete
617 
618 } // end of class Mongo3Interface
619