/src/getdat/getDatabase.java

https://bitbucket.org/Khomkrit_V/gpstrackerproject · Java · 579 lines · 523 code · 56 blank · 0 comment · 26 complexity · 96d95e26f4118046ba64deaaa88ebfaf MD5 · raw file

  1. package getdat;
  2. import com.mongodb.*;
  3. import java.net.UnknownHostException;
  4. import java.sql.Connection;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.*;
  9. public class getDatabase {
  10. public static String getUserdata(String usd,String pwd){ //get role forn user data
  11. String role="";
  12. DBCollection coll;
  13. BasicDBObject query;
  14. Mongo m;
  15. DB db;
  16. try{
  17. m = getMongoConnection();
  18. db = m.getDB( "gts" );
  19. coll = db.getCollection("userdata");
  20. query = new BasicDBObject();
  21. query.put("username", usd);
  22. DBCursor cur = coll.find(query);
  23. boolean isrsnull=cur.hasNext();
  24. if(!isrsnull){
  25. System.out.println("not find data for username : "+usd);
  26. }else{
  27. DBObject myDoc =cur.next();
  28. String pass =(String) myDoc.get("password");
  29. if(pwd.equals(pass)){
  30. role = (String) myDoc.get("role");
  31. }else{
  32. System.out.println("wrong password");
  33. }
  34. }
  35. m.close();
  36. cur.close();
  37. }catch(Exception e){
  38. e.printStackTrace();
  39. }
  40. return role;
  41. }
  42. public static DBObject getCompanyData(String username){ //get DBobject company collection by username
  43. DBObject comdat=null;
  44. DBCollection coll;
  45. BasicDBObject query;
  46. Mongo m;
  47. DB db;
  48. try{
  49. m = getMongoConnection();
  50. db = m.getDB( "gts" );
  51. coll = db.getCollection("company");
  52. query = new BasicDBObject();
  53. query.put("username", username);
  54. DBCursor cur = coll.find(query);
  55. boolean isrsnull=cur.hasNext();
  56. if(!isrsnull){
  57. System.out.println("not find data for username : "+username);
  58. }else{
  59. comdat =cur.next();
  60. }
  61. m.close();
  62. cur.close();
  63. }catch(Exception e){
  64. e.printStackTrace();
  65. }
  66. return comdat;
  67. }
  68. public static DBObject[] getVehicleCompany(String company_id,int row){ // get vehicle data by company ID one data for one array of DBobject
  69. DBCollection coll;
  70. BasicDBObject query;
  71. Mongo m;
  72. DB db;
  73. DBObject[] Vdat =new DBObject[row+1];
  74. try{
  75. m = getMongoConnection();
  76. db = m.getDB( "gts" );
  77. coll = db.getCollection("vehicle");
  78. query = new BasicDBObject();
  79. query.put("Company_ID", company_id);
  80. DBCursor cur = coll.find(query);
  81. boolean isrsnull=cur.hasNext();
  82. if(!isrsnull){
  83. System.out.println("not find data for Company ID : "+company_id);
  84. }else{
  85. for(int i=1;i<=row;i++){
  86. Vdat[i]=cur.next();
  87. }
  88. }
  89. m.close();
  90. cur.close();
  91. }catch(Exception e){
  92. e.printStackTrace();
  93. }
  94. return Vdat;
  95. }
  96. public static DBObject[] getDriverCompany(String company_id,int row){ // get Driver data by company ID one data for one array of DBobject
  97. DBCollection coll;
  98. BasicDBObject query;
  99. Mongo m;
  100. DB db;
  101. DBObject[] Vdat =new DBObject[row+1];
  102. try{
  103. m = getMongoConnection();
  104. db = m.getDB( "gts" );
  105. coll = db.getCollection("driver");
  106. query = new BasicDBObject();
  107. query.put("Company_ID", company_id);
  108. DBCursor cur = coll.find(query);
  109. boolean isrsnull=cur.hasNext();
  110. if(!isrsnull){
  111. System.out.println("not find data for Company ID : "+company_id);
  112. }else{
  113. for(int i=1;i<=row;i++){
  114. Vdat[i]=cur.next();
  115. }
  116. }
  117. m.close();
  118. cur.close();
  119. }catch(Exception e){
  120. e.printStackTrace();
  121. }
  122. return Vdat;
  123. }
  124. public static DBObject[] getDeviceCompany(String company_id,int row){ // get Driver data by company ID one data for one array of DBobject
  125. DBCollection coll;
  126. BasicDBObject query;
  127. Mongo m;
  128. DB db;
  129. DBObject[] Vdat =new DBObject[row+1];
  130. try{
  131. m = getMongoConnection();
  132. db = m.getDB( "gts" );
  133. coll = db.getCollection("device");
  134. query = new BasicDBObject();
  135. query.put("Company_ID", company_id);
  136. DBCursor cur = coll.find(query);
  137. boolean isrsnull=cur.hasNext();
  138. if(!isrsnull){
  139. System.out.println("not find data for Company ID : "+company_id);
  140. }else{
  141. for(int i=1;i<=row;i++){
  142. Vdat[i]=cur.next();
  143. }
  144. }
  145. m.close();
  146. cur.close();
  147. }catch(Exception e){
  148. e.printStackTrace();
  149. }
  150. return Vdat;
  151. }
  152. public static DBObject[] getCollData(String collection,String fild,String dat,int row){ // get Driver data by company ID one data for one array of DBobject
  153. DBCollection coll;
  154. BasicDBObject query;
  155. Mongo m;
  156. DB db;
  157. DBObject[] Vdat =new DBObject[row+1];
  158. try{
  159. m = getMongoConnection();
  160. db = m.getDB( "gts" );
  161. coll = db.getCollection(collection);
  162. query = new BasicDBObject();
  163. query.put(fild, dat);
  164. DBCursor cur = coll.find(query);
  165. boolean isrsnull=cur.hasNext();
  166. if(!isrsnull){
  167. System.out.println("not find data in collection : "+collection);
  168. }else{
  169. for(int i=1;i<=row;i++){
  170. Vdat[i]=cur.next();
  171. }
  172. }
  173. m.close();
  174. cur.close();
  175. }catch(Exception e){
  176. e.printStackTrace();
  177. }
  178. return Vdat;
  179. }
  180. public static DBObject[] getCollData(String collection,String fild,String dat,int row ,boolean sort,String sortby,int order){ // get Driver data by company ID one data for one array of DBobject
  181. DBCollection coll;
  182. BasicDBObject query;
  183. BasicDBObject query2 = null;
  184. Mongo m;
  185. DB db;
  186. DBObject[] Vdat =new DBObject[row+1];
  187. try{
  188. m = getMongoConnection();
  189. db = m.getDB( "gts" );
  190. coll = db.getCollection(collection);
  191. query = new BasicDBObject();
  192. query2 = new BasicDBObject();
  193. query.put(fild, dat);
  194. DBCursor cur=null;
  195. if(sort){
  196. query2.put(sortby, order);
  197. cur = coll.find(query).sort(query2);
  198. }else{
  199. cur = coll.find(query);
  200. }
  201. boolean isrsnull=cur.hasNext();
  202. if(!isrsnull){
  203. System.out.println("not find data in collection : "+collection);
  204. }else{
  205. for(int i=1;i<=row;i++){
  206. Vdat[i]=cur.next();
  207. }
  208. }
  209. m.close();
  210. cur.close();
  211. }catch(Exception e){
  212. e.printStackTrace();
  213. }
  214. return Vdat;
  215. }
  216. public static DBObject[] getCollData(String collection,BasicDBObject query,int row){ // get Driver data by company ID one data for one array of DBobject
  217. DBCollection coll;
  218. Mongo m;
  219. DB db;
  220. DBObject[] Vdat =new DBObject[row+1];
  221. try{
  222. m = getMongoConnection();
  223. db = m.getDB( "gts" );
  224. coll = db.getCollection(collection);
  225. DBCursor cur = coll.find(query);
  226. boolean isrsnull=cur.hasNext();
  227. if(!isrsnull){
  228. System.out.println("not find data in collection : "+collection);
  229. Vdat=null;
  230. }else{
  231. for(int i=1;i<=row;i++){
  232. Vdat[i]=cur.next();
  233. }
  234. }
  235. m.close();
  236. cur.close();
  237. }catch(Exception e){
  238. e.printStackTrace();
  239. }
  240. return Vdat;
  241. }
  242. public static DBObject getCollData(String collection,String fild,String dat){ // get Driver data by company ID one data for one array of DBobject
  243. DBCollection coll;
  244. BasicDBObject query;
  245. Mongo m;
  246. DB db;
  247. DBObject Vdat =null;
  248. try{
  249. m = getMongoConnection();
  250. db = m.getDB( "gts" );
  251. coll = db.getCollection(collection);
  252. query = new BasicDBObject();
  253. query.put(fild, dat);
  254. DBCursor cur = coll.find(query);
  255. boolean isrsnull=cur.hasNext();
  256. if(!isrsnull){
  257. System.out.println("not find data in collection : "+collection);
  258. }else{
  259. Vdat=cur.next();
  260. }
  261. m.close();
  262. cur.close();
  263. }catch(Exception e){
  264. e.printStackTrace();
  265. }
  266. return Vdat;
  267. }
  268. public static int getVehicleCompanyRow(String company_id){ //get number of vehicle by company ID
  269. DBCollection coll;
  270. BasicDBObject query;
  271. Mongo m;
  272. DB db;
  273. int row =0;
  274. try{
  275. m = getMongoConnection();
  276. db = m.getDB( "gts" );
  277. coll = db.getCollection("vehicle");
  278. query = new BasicDBObject();
  279. query.put("Company_ID", company_id);
  280. DBCursor cur = coll.find(query);
  281. boolean isrsnull=cur.hasNext();
  282. if(!isrsnull){
  283. System.out.println("not find data for company ID in row : "+company_id);
  284. }else{
  285. row =cur.count();
  286. }
  287. m.close();
  288. cur.close();
  289. }catch(Exception e){
  290. e.printStackTrace();
  291. }
  292. return row;
  293. }
  294. public static int getRowCount(String collection){// get row count of collection
  295. DBCollection coll;
  296. Mongo m;
  297. DB db;
  298. int row =0;
  299. try{
  300. m = getMongoConnection();
  301. db = m.getDB( "gts" );
  302. coll = db.getCollection(collection);
  303. DBCursor cur = coll.find();
  304. boolean isrsnull=cur.hasNext();
  305. if(!isrsnull){
  306. System.out.println("no row");
  307. }else{
  308. row =cur.count();
  309. }
  310. m.close();
  311. cur.close();
  312. }catch(Exception e){
  313. e.printStackTrace();
  314. }
  315. return row;
  316. }
  317. public static int getRowCount(String collection,String fild,String dat){ // get rowcount of collection have that data in a fild
  318. DBCollection coll;
  319. Mongo m;
  320. DB db;
  321. int row =0;
  322. BasicDBObject query= new BasicDBObject();
  323. try{
  324. m = getMongoConnection();
  325. db = m.getDB( "gts" );
  326. coll = db.getCollection(collection);
  327. query.put(fild, dat);
  328. DBCursor cur = coll.find(query);
  329. boolean isrsnull=cur.hasNext();
  330. if(!isrsnull){
  331. System.out.println("no row");
  332. }else{
  333. row =cur.count();
  334. }
  335. m.close();
  336. cur.close();
  337. }catch(Exception e){
  338. e.printStackTrace();
  339. }
  340. return row;
  341. }
  342. public static int getRowCount(String collection,BasicDBObject query){ // get rowcount of collection have that data in a fild
  343. DBCollection coll;
  344. Mongo m;
  345. DB db;
  346. int row =0;
  347. try{
  348. m = getMongoConnection();
  349. db = m.getDB( "gts" );
  350. coll = db.getCollection(collection);
  351. DBCursor cur = coll.find(query);
  352. boolean isrsnull=cur.hasNext();
  353. if(!isrsnull){
  354. System.out.println("no row");
  355. }else{
  356. row =cur.count();
  357. }
  358. m.close();
  359. cur.close();
  360. }catch(Exception e){
  361. e.printStackTrace();
  362. }
  363. return row;
  364. }
  365. public static boolean checkDupdata(String collection,String n_dat,String dat){// check dup in collection
  366. boolean flag=false;
  367. DBCollection coll;
  368. BasicDBObject query;
  369. Mongo m;
  370. DB db;
  371. int row =0;
  372. try{
  373. m = getMongoConnection();
  374. db = m.getDB( "gts" );
  375. coll = db.getCollection(collection);
  376. query = new BasicDBObject();
  377. query.put(n_dat, dat);
  378. DBCursor cur = coll.find(query);
  379. flag=cur.hasNext();
  380. m.close();
  381. cur.close();
  382. }catch(Exception e){
  383. e.printStackTrace();
  384. }
  385. return flag;
  386. }
  387. public static boolean getDevice_company(String usn,String pwd,String[] device_id,String company_id,String stat){// use for add device only
  388. DBCollection coll;
  389. Mongo m;
  390. DB db;
  391. int row =0;
  392. boolean flag=false;
  393. String user_stat = getUserdata(usn,pwd);
  394. BasicDBObject query= new BasicDBObject();
  395. if(user_stat.equals("null")){
  396. System.out.println("invalid usn and pwd");
  397. }else{
  398. try{
  399. m = getMongoConnection();
  400. db = m.getDB( "gts" );
  401. coll = db.getCollection("device");
  402. query.put("Company_ID", company_id);
  403. query.put("stat", "0");
  404. DBCursor cur = coll.find(query);
  405. boolean isrs2null=cur.hasNext();
  406. if(!isrs2null){
  407. System.out.println("don't find vehicle with that company id");
  408. }else{
  409. int i=1;
  410. while(cur.hasNext()){
  411. device_id[i] = (String) cur.next().get("Device_ID");
  412. i=i+1;
  413. }
  414. flag= true;
  415. }
  416. m.close();
  417. cur.close();
  418. }catch(Exception e){
  419. e.printStackTrace();
  420. }
  421. }
  422. return flag;
  423. }
  424. public static String getActiveDeliverID(String vehicle_id){
  425. String deliver_id="";
  426. DBCollection coll;
  427. Mongo m;
  428. DB db;
  429. int row =0;
  430. BasicDBObject query= new BasicDBObject();
  431. try{
  432. m = getMongoConnection();
  433. db = m.getDB( "gts" );
  434. coll = db.getCollection("deliver");
  435. query.put("Vehicle_ID", vehicle_id);
  436. query.put("stat", "1");
  437. DBCursor cur = coll.find(query);
  438. boolean isrs2null=cur.hasNext();
  439. if(!isrs2null){
  440. System.out.println("don't find deliver with that vehicle id");
  441. deliver_id="notactive";
  442. }else{
  443. while(cur.hasNext()){
  444. deliver_id = (String) cur.next().get("Deliver_ID");
  445. }
  446. }
  447. m.close();
  448. cur.close();
  449. }catch(Exception e){
  450. e.printStackTrace();
  451. }
  452. return deliver_id;
  453. }
  454. public static Mongo getMongoConnection() throws UnknownHostException, MongoException{
  455. Mongo m = new Mongo( "localhost" , 8081 );
  456. return m;
  457. }
  458. public static String[] geteventdata(double[] lat,double[] lon,String deliver_id,String vehicle_id){
  459. DBCollection coll;
  460. Mongo m;
  461. DB db;
  462. BasicDBObject query= new BasicDBObject();
  463. BasicDBObject query2= new BasicDBObject();
  464. DBObject eventData =null;
  465. String[] time= new String[lat.length];
  466. try{
  467. m = getMongoConnection();
  468. db = m.getDB( "gts" );
  469. coll = db.getCollection("eventdata");
  470. query.put("Deliver_ID", deliver_id);
  471. query.put("Vehicle_ID", vehicle_id);
  472. query2.put("timestamp", 1);
  473. DBCursor cur = coll.find(query).sort(query2);
  474. if(!cur.hasNext()){
  475. System.out.println("no eventdata");
  476. }else{
  477. int i=1;
  478. while(cur.hasNext()){
  479. eventData=cur.next();
  480. time[i]= (String) eventData.get("timestamp");
  481. lat[i]=Double.parseDouble((String)eventData.get("lat"));
  482. lon[i]=Double.parseDouble((String)eventData.get("lon"));
  483. i++;
  484. }
  485. }
  486. m.close();
  487. cur.close();
  488. }catch (Exception e) {
  489. e.printStackTrace();
  490. }
  491. return time;
  492. }
  493. public static DBObject[] geteventdata(String vehicle_id,int row){
  494. DBCollection coll;
  495. Mongo m;
  496. DB db;
  497. BasicDBObject query= new BasicDBObject();
  498. BasicDBObject query2= new BasicDBObject();
  499. DBObject[] eventData =new DBObject[row+1];
  500. try{
  501. m = getMongoConnection();
  502. db = m.getDB( "gts" );
  503. coll = db.getCollection("eventdata");
  504. query.put("Vehicle_ID", vehicle_id);
  505. query2.put("timestamp", -1);
  506. DBCursor cur = coll.find(query).sort(query2);
  507. if(!cur.hasNext()){
  508. System.out.println("no eventdata");
  509. }else{
  510. int i=1;
  511. while(cur.hasNext()){
  512. eventData[i]=cur.next();
  513. i++;
  514. }
  515. }
  516. m.close();
  517. cur.close();
  518. }catch (Exception e) {
  519. e.printStackTrace();
  520. }
  521. return eventData;
  522. }
  523. }