Java tutorial
/** * @author Dilshad Mustafa * Copyright (c) Dilshad Mustafa * All Rights Reserved. * @since 11-Feb-2016 * File Name : DTableOld.java */ /** Copyright (c) Dilshad Mustafa 2016. All Rights Reserved. User and Developer License 1. You can use this Software for both Personal use as well as Commercial use, with or without paying fee to Dilshad Mustafa. Please read fully below for the terms and conditions. You may use this Software only if you comply fully with all the terms and conditions of this License. 2. If you want to redistribute this Software, you should redistribute only the original source code from Dilshad Mustafa's project and/or the compiled object binary form of the original source code and you should ensure to bundle and display this original license text and Dilshad Mustafa's copyright notice along with it as well as in each source code file of this Software. 3. If you want to embed this Software within your work, you should embed only the original source code from Dilshad Mustafa's project and/or the compiled object binary form of the original source code and you should ensure to bundle and display this original license text and Dilshad Mustafa's copyright notice along with it as well as in each source code file of this Software. 4. You should not modify this Software source code and/or its compiled object binary form in any way. 5. You should not redistribute any modified source code of this Software and/or its compiled object binary form with any changes, additions, enhancements, updates or modifications, any modified works of this Software, any straight forward translation and/or implementation to same and/or another programming language and embedded modified versions of this Software source code and/or its compiled object binary in any form, both within as well as outside your organization, company, legal entity and/or individual. 6. You should not embed any modification of this Software source code and/or its compiled object binary form in any way, either partially or fully. 7. Under differently named or renamed software, you should not redistribute this Software and/or any modified works of this Software, including its source code and/or its compiled object binary form. Under your name or your company name or your product name, you should not publish this Software, including its source code and/or its compiled object binary form, modified or original. 8. You agree to use the original source code from Dilshad Mustafa's project only and/or the compiled object binary form of the original source code. 9. You agree fully to the terms and conditions of this License of this software product, under same software name and/or if it is renamed in future. 10. This software is created and programmed by Dilshad Mustafa and Dilshad holds the copyright for this Software and all its source code. You agree that you will not infringe or do any activity that will violate Dilshad's copyright of this software and all its source code. 11. The Copyright holder of this Software reserves the right to change the terms and conditions of this license without giving prior notice. 12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR OR COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package com.dilmus.dilshad.scabi.deprecated; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.Set; import org.bson.BasicBSONObject; import org.bson.conversions.Bson; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.dilmus.dilshad.scabi.common.DMJson; import com.dilmus.dilshad.scabi.common.DScabiException; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; import com.mongodb.MapReduceCommand; import com.mongodb.MapReduceOutput; import com.mongodb.WriteResult; /** * @author Dilshad Mustafa * */ public class DTableOld { final static Logger log = LoggerFactory.getLogger(DTableOld.class); private DDBOld m_ddb = null; private DBCollection m_table = null; private String m_tableName; private boolean m_firstTime; private ArrayList<String> m_fieldNames; private DResultSetOld m_dcursor = null; public DTableOld(DDBOld ddb, DBCollection table) { m_ddb = ddb; m_table = table; m_tableName = table.getName(); m_firstTime = true; m_fieldNames = null; m_dcursor = new DResultSetOld(); } public DTableOld(DDBOld ddb, String tableName) throws DScabiException { m_ddb = null; m_table = null; m_tableName = null; m_firstTime = true; m_fieldNames = null; DB db = ddb.getDB(); if (false == db.collectionExists(tableName)) { throw new DScabiException("Table name doesn't exist : " + tableName, "DBT.DBT.1"); } m_table = db.getCollection(tableName); m_tableName = tableName; m_ddb = ddb; m_dcursor = new DResultSetOld(); } public DTableOld(DDBOld ddb) { m_ddb = ddb; m_table = null; m_tableName = null; m_firstTime = true; m_fieldNames = null; m_dcursor = new DResultSetOld(); } public int setTableName(String tableName) throws DScabiException { m_tableName = null; m_firstTime = true; m_fieldNames = null; m_table = null; DB db = m_ddb.getDB(); if (false == db.collectionExists(tableName)) { throw new DScabiException("Table name doesn't exist : " + tableName, "DBT.STN.1"); } m_table = db.getCollection(tableName); m_tableName = tableName; return 0; } public long count() { return m_table.count(); } public ArrayList<String> fieldNames() throws DScabiException { if (null == m_tableName) { throw new DScabiException("Table name is null", "DBT.FNS.1"); } if (null == m_table) { throw new DScabiException("Table is null", "DBT.FNS.2"); } log.debug("fieldNames() firstTime is {}", m_firstTime); log.debug("fieldNames() table.count() is {}", m_table.count()); if (m_table.count() <= 0) { return null; } if (m_firstTime) { String map = "function() { for (var key in this) { emit(key, null); } }"; String reduce = "function(key, s) { return null; }"; MapReduceCommand cmd = new MapReduceCommand(m_table, map, reduce, null, MapReduceCommand.OutputType.INLINE, null); MapReduceOutput out = m_table.mapReduce(cmd); //if 4th param output collection name is used above, String s = out.getOutputCollection().distinct("_id").toString(); //if 4th param output collection name is used above, System.out.println("out.getOutputCollection().distinct " + s); m_fieldNames = new ArrayList<String>(); for (DBObject o : out.results()) { log.debug("fieldNames() Key, value is : {}", o.toString()); log.debug("fieldNames() Key name is : {}", o.get("_id").toString()); if (false == o.get("_id").toString().equals("_id")) m_fieldNames.add(o.get("_id").toString()); } m_firstTime = false; return m_fieldNames; } return m_fieldNames; } public ArrayList<String> fieldNamesUsingFindOne() throws DScabiException { if (null == m_tableName) { throw new DScabiException("Table name is null", "DBT.FNU.1"); } if (null == m_table) { throw new DScabiException("Table is null", "DBT.FNU.2"); } log.debug("fieldNamesUsingFindOne() firstTime is {}", m_firstTime); log.debug("fieldNamesUsingFindOne() table.count() is {}", m_table.count()); if (m_table.count() <= 0) { return null; } if (m_firstTime) { DBObject out = m_table.findOne(); m_fieldNames = new ArrayList<String>(); Set<String> st = out.keySet(); for (String s : st) { log.debug("fieldNamesUsingFindOne() value is : {}", out.get(s)); log.debug("fieldNamesUsingFindOne() Key name is : {}", s); if (false == s.equals("_id")) m_fieldNames.add(s); } m_firstTime = false; return m_fieldNames; } return m_fieldNames; } public DBCollection getCollection() { return m_table; } public DResultSetOld find(DObject document) { DBCursor cursor = m_table.find(document.getDocument()); m_dcursor.set(cursor); return m_dcursor; } /* public DResultSet find(Bson document) { DBCursor cursor = m_table.find(document); m_dcursor.set(cursor); return m_dcursor; } */ public DResultSetOld findDirect(DObject document) { return new DResultSetOld(m_table.find(document.getDocument())); } public DObject findOne(DObject document) { DBObject ob = m_table.findOne(document.getDocument()); return new DObject(ob); } public int insert(DObject dob) throws DScabiException { WriteResult result = m_table.insert(dob.getDocument()); log.debug("insert() result is : {}", result.getN()); if (result.getN() < 0) throw new DScabiException("Insert failed for DBackObject : " + dob.toString(), "DBT.INT.1"); return result.getN(); } public int update(DObject dob, DObject updateObj) throws DScabiException { WriteResult result = m_table.updateMulti(dob.getDocument(), updateObj.getDocument()); log.debug("update() result is : {}", result.getN()); if (result.getN() <= 0) throw new DScabiException("Update failed for DBackObject dob : " + dob.toString() + " updateObj : " + updateObj.toString(), "DBT.UPE.1"); return result.getN(); } public int remove(DObject dob) throws DScabiException { WriteResult result = m_table.remove(dob.getDocument()); log.debug("remove() result is : {}", result.getN()); if (result.getN() <= 0) throw new DScabiException("Remove failed for DBackObject : " + dob.toString(), "DBT.REE.1"); return result.getN(); } private boolean isEmpty(ArrayList<String> fieldList) { if (null == fieldList) return true; if (fieldList.isEmpty()) return true; return false; } public int insertRow(String jsonRow, String jsonCheck) throws DScabiException, IOException { log.debug("insertRow() firstTime is {}", m_firstTime); ArrayList<String> fieldList = fieldNamesUsingFindOne(); // fieldNames(); DMJson djson = new DMJson(jsonRow); Set<String> st = djson.keySet(); BasicDBObject document = new BasicDBObject(); int n = 0; WriteResult result = null; DMJson djsonCheck = new DMJson(jsonCheck); Set<String> stCheck = djsonCheck.keySet(); BasicDBObject documentCheck = new BasicDBObject(); if (false == isEmpty(fieldList)) { if (false == fieldList.containsAll(st)) { throw new DScabiException( "One or more field name in jsonRow doesn't exist in fieldNames list. jsonRow : " + jsonRow + " Field Names list : " + fieldList, "DBT.IRW.1"); } if (false == fieldList.containsAll(stCheck)) { throw new DScabiException( "One or more field name in jsonCheck doesn't exist in fieldNames list. jsonCheck : " + jsonCheck + " Field Names list : " + fieldList, "DBT.IRW.2"); } if (false == st.containsAll(fieldList)) { throw new DScabiException( "One or more field name in fieldNames doesn't exist in jsonRow key set. jsonRow : " + jsonRow + " Field Names list : " + fieldList, "DBT.IRW.3"); } if (fieldList.size() != st.size()) { throw new DScabiException("Fields count doesn't match. fieldNames : " + fieldList.toString() + " with jsonRow : " + jsonRow, "DBT.IRW.4"); } } if (false == isEmpty(fieldList)) { for (String fieldName : st) { // create a document to store key and value String f = djson.getString(fieldName); if (null == f) { throw new DScabiException("Field name " + fieldName + " doesn't exist in jsonRow : " + jsonRow + " Field Names list : " + fieldList, "DBT.IRW.5"); } document.put(fieldName, f); } for (String keyCheck : stCheck) { // create a document to store key and value String f2 = djsonCheck.getString(keyCheck); if (null == f2) { throw new DScabiException( "Field name " + keyCheck + " doesn't exist in jsonCheck : " + jsonCheck, "DBT.IRW.6"); } documentCheck.put(keyCheck, f2); } DBCursor cursorExist = m_table.find(documentCheck); n = cursorExist.count(); if (0 == n) { log.debug("insertRow() Inside 0 == n"); result = m_table.insert(document); log.debug("insertRow() result is : {}", result.getN()); if (result.getN() < 0) throw new DScabiException("Insert failed for document : " + document.toString(), "DBT.IRW.7"); } else if (1 == n) { throw new DScabiException("Row already exists. jsonCheck : " + jsonCheck, "DBT.IRW.8"); // already found } else { throw new DScabiException("Row already exists, multiple matches. jsonCheck : " + jsonCheck, "DBT.IRW.9"); // already found } } else { for (String key : st) { // create a document to store key and value String f3 = djson.getString(key); if (null == f3) { throw new DScabiException("Field name " + key + " doesn't exist in jsonRow : " + jsonRow, "DBT.IRW.10"); } document.put(key, djson.getString(key)); } for (String keyCheck : stCheck) { // create a document to store key and value String f4 = djsonCheck.getString(keyCheck); if (null == f4) { throw new DScabiException( "Field name " + keyCheck + " doesn't exist in jsonCheck : " + jsonCheck, "DBT.IRW.11"); } documentCheck.put(keyCheck, djsonCheck.getString(keyCheck)); } DBCursor cursorExist = m_table.find(documentCheck); n = cursorExist.count(); if (0 == n) { log.debug("insertRow() Inside 0 == n"); result = m_table.insert(document); log.debug("insertRow() result is : {}", result.getN()); if (result.getN() < 0) throw new DScabiException("Insert failed for document : " + document.toString(), "DBT.IRW.12"); } else if (1 == n) { throw new DScabiException("Row already exists. jsonCheck : " + jsonCheck, "DBT.IRW.13"); // already found } else { throw new DScabiException("Row already exists, multiple matches. jsonCheck : " + jsonCheck, "DBT.IRW.14"); // already found } } return result.getN(); } public String executeQuery(String jsonQuery) throws DScabiException, IOException { ArrayList<String> fieldList = fieldNamesUsingFindOne(); // fieldNames(); DMJson djson = new DMJson(jsonQuery); Set<String> st = djson.keySet(); BasicDBObject document = new BasicDBObject(); ArrayList<String> finalList = new ArrayList<String>(); HashMap<String, String> hmap = new HashMap<String, String>(); DMJson djson3 = null; if (false == isEmpty(fieldList)) { if (false == fieldList.containsAll(st)) { throw new DScabiException( "One or more field name in jsonQuery doesn't exist in fieldNames list. jsonQuery : " + jsonQuery + " Field Names list : " + fieldList, "DBT.EQY.1"); } } for (String key : st) { // create a document to store key and value document.put(key, djson.getString(key)); } DBCursor cursorExist = m_table.find(document); while (cursorExist.hasNext()) { hmap.clear(); DBObject ob = cursorExist.next(); Set<String> obkeys = ob.keySet(); obkeys.remove("_id"); // exclude _id field //log.debug("executeQuery() result from ob {}", ob.toString()); if (false == isEmpty(fieldList)) { if (false == obkeys.containsAll(fieldList)) { throw new DScabiException( "One or more field name in fieldList doesn't exist in obkeys key set. obkeys : " + obkeys + " Field Names list : " + fieldList, "DBT.EQY.2"); } for (String field : obkeys) { //if (field.equals("_id")) // continue; String f = (String) ((BasicBSONObject) ob).getString(field); if (null == f) { throw new DScabiException( "Field name " + field + " doesn't exist in dbobject in dbcursor. jsonQuery : " + jsonQuery + " Field Names list : " + fieldList, "DBT.EQY.3"); } //log.debug("executeQuery() field is {}", field); //log.debug("executeQuery() f is {}", f); hmap.put(field, f); } } else { for (String key : obkeys) { //if (key.equals("_id")) // continue; String f2 = (String) ((BasicBSONObject) ob).getString(key); if (null == f2) { throw new DScabiException("Field name " + key + " doesn't exist in dbobject in dbcursor. jsonQuery : " + jsonQuery, "DBT.EQY.4"); } //log.debug("executeQuery() key is {}", key); //log.debug("executeQuery() f2 is {}", f2); hmap.put(key, f2); } } DMJson djson2 = null; //if (false == fieldList.isEmpty()) // djson2 = DJson.createDJsonList(hmap, fieldList); //else if (false == st.isEmpty()) // djson2 = DJson.createDJsonSet(hmap, st); if (false == obkeys.isEmpty()) djson2 = DMJson.createDJsonSet(hmap, obkeys); if (null == djson2) { throw new DScabiException("djson2 is null. jsonQuery : " + jsonQuery, "DBT.EQY.5"); } finalList.add(djson2.toString()); } djson3 = DMJson.createDJsonWithCount(finalList); return djson3.toString(); } public DResultSetOld executeQueryCursorResult(String jsonQuery) throws IOException, DScabiException { ArrayList<String> fieldList = fieldNamesUsingFindOne(); // fieldNames(); DMJson djson = new DMJson(jsonQuery); Set<String> st = djson.keySet(); BasicDBObject document = new BasicDBObject(); if (false == isEmpty(fieldList)) { if (false == fieldList.containsAll(st)) { throw new DScabiException( "One or more field name in jsonQuery doesn't exist in fieldNames list. jsonQuery : " + jsonQuery + " Field Names list : " + fieldList, "DBT.EQC.1"); } } for (String key : st) { // create a document to store key and value document.put(key, djson.getString(key)); } DBCursor cursorExist = m_table.find(document); return new DResultSetOld(cursorExist); } public int executeUpdate(String jsonUpdate, String jsonWhere) throws IOException, DScabiException { ArrayList<String> fieldList = fieldNamesUsingFindOne(); // fieldNames(); DMJson djsonWhere = new DMJson(jsonWhere); Set<String> stWhere = djsonWhere.keySet(); BasicDBObject documentWhere = new BasicDBObject(); DMJson djsonUpdate = new DMJson(jsonUpdate); Set<String> stUpdate = djsonUpdate.keySet(); BasicDBObject documentUpdate = new BasicDBObject(); if (false == isEmpty(fieldList)) { if (false == fieldList.containsAll(stWhere)) { throw new DScabiException( "One or more field name in jsonWhere doesn't exist in fieldNames list. jsonWhere : " + jsonWhere + " Field Names list : " + fieldList, "DBT.EUE.1"); } if (false == fieldList.containsAll(stUpdate)) { throw new DScabiException( "One or more field name in jsonUpdate doesn't exist in fieldNames list. jsonUpdate : " + jsonUpdate + " Field Names list : " + fieldList, "DBT.EUE.2"); } } for (String keyWhere : stWhere) { // create a document to store key and value documentWhere.put(keyWhere, djsonWhere.getString(keyWhere)); } for (String keyUpdate : stUpdate) { // create a document to store key and value documentUpdate.put(keyUpdate, djsonUpdate.getString(keyUpdate)); } BasicDBObject updateObj = new BasicDBObject(); updateObj.put("$set", documentUpdate); WriteResult result = m_table.updateMulti(documentWhere, updateObj); log.debug("executeUpdate() result is : {}", result.getN()); if (result.getN() <= 0) throw new DScabiException("Update failed for documentWhere : " + documentWhere.toString() + " updateObj : " + updateObj.toString(), "DBT.EUE.3"); return result.getN(); } public int executeRemove(String jsonWhere) throws IOException, DScabiException { ArrayList<String> fieldList = fieldNamesUsingFindOne(); // fieldNames(); DMJson djsonWhere = new DMJson(jsonWhere); Set<String> stWhere = djsonWhere.keySet(); BasicDBObject documentWhere = new BasicDBObject(); if (false == isEmpty(fieldList)) { if (false == fieldList.containsAll(stWhere)) { throw new DScabiException( "One or more field name in jsonWhere doesn't exist in fieldNames list. jsonWhere : " + jsonWhere + " Field Names list : " + fieldList, "DBT.ERE.1"); } } for (String keyWhere : stWhere) { // create a document to store key and value documentWhere.put(keyWhere, djsonWhere.getString(keyWhere)); } // DBObject result = table.findAndRemove(documentWhere); WriteResult result = m_table.remove(documentWhere); log.debug("executeRemove() result is : {}", result.getN()); if (result.getN() <= 0) throw new DScabiException("Remove failed for documentWhere : " + documentWhere.toString(), "DBT.ERE.2"); return result.getN(); } }