PageRenderTime 109ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/Server/tomcat/WEB-INF/src/com/drhelper/common/db/MongoDB.java

https://github.com/weiganyi/dr-helper
Java | 593 lines | 429 code | 107 blank | 57 comment | 96 complexity | 2a4ca19db49d23d62b3c32acc5b9da20 MD5 | raw file
  1. package com.drhelper.common.db;
  2. import java.io.IOException;
  3. import java.net.UnknownHostException;
  4. import java.util.ArrayList;
  5. import java.util.Date;
  6. import java.util.Iterator;
  7. import java.util.Properties;
  8. import com.alibaba.fastjson.JSON;
  9. import com.drhelper.common.entity.Detail;
  10. import com.drhelper.common.entity.Order;
  11. import com.mongodb.BasicDBList;
  12. import com.mongodb.BasicDBObject;
  13. import com.mongodb.DB;
  14. import com.mongodb.DBCollection;
  15. import com.mongodb.DBCursor;
  16. import com.mongodb.DBObject;
  17. import com.mongodb.Mongo;
  18. import com.mongodb.MongoException;
  19. public class MongoDB implements DataBase {
  20. private String mongodb_host;
  21. private String mongodb_port;
  22. private String mongodb_db;
  23. private String mongodb_username;
  24. private String mongodb_password;
  25. private Mongo m = null;
  26. private DB db = null;
  27. @Override
  28. public boolean openConnect() {
  29. //get the config
  30. Properties prop = new Properties();
  31. try {
  32. prop.load(this.getClass().getClassLoader().getResourceAsStream("DBConfig.properties"));
  33. mongodb_host = prop.getProperty("mongodb_host");
  34. mongodb_port = prop.getProperty("mongodb_port");
  35. mongodb_db = prop.getProperty("mongodb_db");
  36. mongodb_username = prop.getProperty("mongodb_username");
  37. mongodb_password = prop.getProperty("mongodb_password");
  38. } catch (IOException e) {
  39. System.out.println("MongoDB.openConnect(): properties catch IOException: " + e.getMessage());
  40. return false;
  41. }
  42. //create the monogo client
  43. try {
  44. m = new Mongo(mongodb_host, Integer.valueOf(mongodb_port));
  45. } catch (UnknownHostException e) {
  46. System.out.println("MongoDB.openConnect(): create the mongo catch UnknownHostException: " + e.getMessage());
  47. return false;
  48. } catch (MongoException e) {
  49. System.out.println("MongoDB.openConnect(): create the mongo catch MongoException: " + e.getMessage());
  50. return false;
  51. }
  52. //create the connnect
  53. db = m.getDB(mongodb_db);
  54. //do the auth
  55. boolean auth = db.authenticate(mongodb_username, mongodb_password.toCharArray());
  56. if (auth) {
  57. return true;
  58. }else {
  59. return false;
  60. }
  61. }
  62. @Override
  63. public boolean closeConnect() {
  64. if (m != null) {
  65. m.close();
  66. }
  67. return true;
  68. }
  69. private DBObject findLastOne(DBCollection coll) {
  70. DBObject dbObj = null;
  71. DBCursor cr = coll.find();
  72. while(cr.hasNext()) {
  73. dbObj = cr.next();
  74. }
  75. return dbObj;
  76. }
  77. public int createOrder(String user, int table) {
  78. int order = 0;
  79. boolean pay = false;
  80. ArrayList<DBObject> detail = null;
  81. //get or create the collection
  82. DBCollection coll = db.getCollection("dr_order");
  83. //get the maxnium order
  84. DBObject dbObj = findLastOne(coll);
  85. if (dbObj != null) {
  86. Object obj = dbObj.get("order");
  87. if (obj != null) {
  88. order = Integer.valueOf(obj.toString()) + 1;
  89. }else {
  90. return order;
  91. }
  92. }else {
  93. order = 1;
  94. }
  95. //get the current date
  96. Date date = new Date();
  97. detail = new ArrayList<DBObject>();
  98. DBObject doc = new BasicDBObject();
  99. doc.put("order", order);
  100. doc.put("table", table);
  101. doc.put("waiter", user);
  102. doc.put("time", date);
  103. doc.put("admin", "");
  104. doc.put("pay", pay);
  105. doc.put("detail", detail);
  106. coll.insert(doc);
  107. return order;
  108. }
  109. public Order getOrderObjByOrder(int orderNum) {
  110. Order order = null;
  111. DBObject dbObj = null;
  112. //get the collection
  113. DBCollection coll = db.getCollection("dr_order");
  114. //get the order
  115. DBObject query = new BasicDBObject();
  116. query.put("order", orderNum);
  117. dbObj = coll.findOne(query);
  118. //serialize the obj to Order.class
  119. if (dbObj != null) {
  120. order = JSON.parseObject(dbObj.toString(), Order.class);
  121. }
  122. return order;
  123. }
  124. public int getOrderByTable(int tableNum) {
  125. Order order = null;
  126. DBObject dbObj = null;
  127. int orderNum = 0;
  128. //get the collection
  129. DBCollection coll = db.getCollection("dr_order");
  130. //get the order
  131. DBObject query = new BasicDBObject();
  132. query.put("table", tableNum);
  133. dbObj = coll.findOne(query);
  134. //serialize the obj to Order.class
  135. if (dbObj != null) {
  136. order = JSON.parseObject(dbObj.toString(), Order.class);
  137. }
  138. if (order != null) {
  139. orderNum = order.getOrder();
  140. }
  141. return orderNum;
  142. }
  143. public int getOrderByOrder(int orderNum) {
  144. Order order = null;
  145. DBObject dbObj = null;
  146. int newOrderNum = 0;
  147. //get the collection
  148. DBCollection coll = db.getCollection("dr_order");
  149. //get the order
  150. DBObject query = new BasicDBObject();
  151. query.put("order", orderNum);
  152. dbObj = coll.findOne(query);
  153. //serialize the obj to Order.class
  154. if (dbObj != null) {
  155. order = JSON.parseObject(dbObj.toString(), Order.class);
  156. }
  157. if (order != null) {
  158. newOrderNum = order.getOrder();
  159. }
  160. return newOrderNum;
  161. }
  162. public boolean submitOrder(Order order) {
  163. DBObject dbObj = null;
  164. boolean result = false;
  165. //get the collection
  166. DBCollection coll = db.getCollection("dr_order");
  167. //check if the order is exist
  168. DBObject query = new BasicDBObject();
  169. int orderNum = order.getOrder();
  170. query.put("order", orderNum);
  171. dbObj = coll.findOne(query);
  172. if (dbObj != null) {
  173. //if doc exist, update it
  174. ArrayList<DBObject> detail = new ArrayList<DBObject>();
  175. Iterator<Detail> it = order.getDetail().iterator();
  176. DBObject node;
  177. Detail detailItem;
  178. while (it.hasNext()) {
  179. detailItem = it.next();
  180. node = new BasicDBObject();
  181. node.put("menu", detailItem.getMenu());
  182. node.put("price", detailItem.getPrice());
  183. node.put("amount", detailItem.getAmount());
  184. node.put("chef", "");
  185. node.put("finish", detailItem.isFinish());
  186. node.put("remark", detailItem.getRemark());
  187. detail.add(node);
  188. }
  189. dbObj.put("detail", detail);
  190. coll.update(query, dbObj);
  191. result = true;
  192. }else {
  193. //if doc not exist, there is a fault
  194. result = false;
  195. }
  196. return result;
  197. }
  198. public boolean deleteOrder(Order order) {
  199. DBObject dbObj = null;
  200. boolean result = false;
  201. //get the collection
  202. DBCollection coll = db.getCollection("dr_order");
  203. //check if the order is exist
  204. DBObject query = new BasicDBObject();
  205. int orderNum = order.getOrder();
  206. query.put("order", orderNum);
  207. dbObj = coll.findOne(query);
  208. if (dbObj != null) {
  209. //if doc exist, delete it
  210. coll.remove(query);
  211. result = true;
  212. }else {
  213. //if doc not exist, there is a fault
  214. result = false;
  215. }
  216. return result;
  217. }
  218. public int changeTable(int tableNum1, int tableNum2) {
  219. DBObject dbObj = null;
  220. Order order = null;
  221. int orderNum = 0;
  222. //get the collection
  223. DBCollection coll = db.getCollection("dr_order");
  224. //check if the order is exist
  225. DBObject query = new BasicDBObject();
  226. query.put("table", tableNum1);
  227. dbObj = coll.findOne(query);
  228. if (dbObj != null) {
  229. order = JSON.parseObject(dbObj.toString(), Order.class);
  230. if (order != null) {
  231. orderNum = order.getOrder();
  232. }
  233. //if doc exist, update it
  234. dbObj.put("table", tableNum2);
  235. coll.update(query, dbObj);
  236. }
  237. return orderNum;
  238. }
  239. public int unionTable(int tableNum1, int tableNum2) {
  240. int orderNum = 0;
  241. String proc = "union_table(" + String.valueOf(tableNum1) + ", " + String.valueOf(tableNum2) + ")";
  242. //call the prepare_call
  243. BasicDBObject obj = db.doEval(proc);
  244. if (obj != null) {
  245. String orderStr = String.valueOf(obj.toMap().get("retval"));
  246. orderNum = Double.valueOf(orderStr).intValue();
  247. }
  248. return orderNum;
  249. }
  250. public int getFinishMenu(String user) {
  251. Order order = null;
  252. DBObject dbObj = null;
  253. ArrayList<Detail> detailList = null;
  254. int num = 0;
  255. //get the collection
  256. DBCollection coll = db.getCollection("dr_order");
  257. //get the order
  258. DBObject query = new BasicDBObject();
  259. query.put("waiter", user);
  260. DBCursor cr = coll.find(query);
  261. if (cr.hasNext()) {
  262. dbObj = cr.next();
  263. //find the finish menu
  264. if (dbObj != null) {
  265. order = JSON.parseObject(dbObj.toString(), Order.class);
  266. if (order != null) {
  267. detailList = order.getDetail();
  268. for (Detail detail : detailList) {
  269. if (detail.isFinish() == true) {
  270. num = order.getTable();
  271. return num;
  272. }
  273. }
  274. }
  275. }
  276. }
  277. return num;
  278. }
  279. public boolean updateOrderMenuFetch(int orderNum, String menu, String user) {
  280. DBObject dbObj = null;
  281. DBObject detail = null;
  282. String menu2 = null;
  283. String chef = null;
  284. //get the collection
  285. DBCollection coll = db.getCollection("dr_order");
  286. //check if the order is exist
  287. DBObject query = new BasicDBObject();
  288. query.put("order", orderNum);
  289. dbObj = coll.findOne(query);
  290. if (dbObj == null) {
  291. return false;
  292. }
  293. //get detail list
  294. BasicDBList detailList = (BasicDBList)dbObj.get("detail");
  295. if (detailList == null) {
  296. return false;
  297. }
  298. //iterator the detail list to get the menu
  299. Iterator<Object> it = detailList.iterator();
  300. while (it.hasNext()) {
  301. detail = (DBObject)it.next();
  302. menu2 = (String)detail.get("menu");
  303. if (menu2.equals(menu) == true) {
  304. chef = (String)detail.get("chef");
  305. //if chef isn't exist, set it
  306. if (chef.equals("") == true) {
  307. detail.put("chef", user);
  308. //otherwise, clear it
  309. }else {
  310. detail.put("chef", "");
  311. }
  312. }
  313. }
  314. coll.update(query, dbObj);
  315. return true;
  316. }
  317. public boolean updateOrderMenuFinish(int orderNum, String menu) {
  318. DBObject dbObj = null;
  319. DBObject detail = null;
  320. String menu2 = null;
  321. Boolean finish;
  322. //get the collection
  323. DBCollection coll = db.getCollection("dr_order");
  324. //check if the order is exist
  325. DBObject query = new BasicDBObject();
  326. query.put("order", orderNum);
  327. dbObj = coll.findOne(query);
  328. if (dbObj == null) {
  329. return false;
  330. }
  331. //get detail list
  332. BasicDBList detailList = (BasicDBList)dbObj.get("detail");
  333. if (detailList == null) {
  334. return false;
  335. }
  336. //iterator the detail list to get the menu
  337. Iterator<Object> it = detailList.iterator();
  338. while (it.hasNext()) {
  339. detail = (DBObject)it.next();
  340. menu2 = (String)detail.get("menu");
  341. if (menu2.equals(menu) == true) {
  342. finish = (Boolean)detail.get("finish");
  343. //if finish isn't set, set it
  344. if (finish == false) {
  345. detail.put("finish", true);
  346. //otherwise, clear it
  347. }else {
  348. detail.put("finish", false);
  349. }
  350. }
  351. }
  352. coll.update(query, dbObj);
  353. return true;
  354. }
  355. public ArrayList<Order> getOrderListNotPay() {
  356. Order order = null;
  357. DBObject dbObj = null;
  358. ArrayList<Order> orderList = null;
  359. //get the collection
  360. DBCollection coll = db.getCollection("dr_order");
  361. //get the order
  362. DBObject query = new BasicDBObject();
  363. query.put("pay", false);
  364. DBCursor cr = coll.find(query);
  365. if (cr.hasNext()) {
  366. orderList = new ArrayList<Order>();
  367. }else {
  368. return orderList;
  369. }
  370. //construct the order list
  371. while (cr.hasNext()) {
  372. dbObj = cr.next();
  373. if (dbObj != null) {
  374. order = JSON.parseObject(dbObj.toString(), Order.class);
  375. if (order != null) {
  376. orderList.add(order);
  377. }
  378. }
  379. }
  380. return orderList;
  381. }
  382. public boolean getOrderIsPay(int orderNum) {
  383. DBObject dbObj = null;
  384. Boolean pay;
  385. //get the collection
  386. DBCollection coll = db.getCollection("dr_order");
  387. //check if the order is exist
  388. DBObject query = new BasicDBObject();
  389. query.put("order", orderNum);
  390. dbObj = coll.findOne(query);
  391. if (dbObj == null) {
  392. return false;
  393. }
  394. pay = (Boolean)dbObj.get("pay");
  395. return pay;
  396. }
  397. public boolean updateAdminOrderPay(int orderNum, String user) {
  398. DBObject dbObj = null;
  399. Boolean pay;
  400. //get the collection
  401. DBCollection coll = db.getCollection("dr_order");
  402. //check if the order is exist
  403. DBObject query = new BasicDBObject();
  404. query.put("order", orderNum);
  405. dbObj = coll.findOne(query);
  406. if (dbObj == null) {
  407. return false;
  408. }
  409. pay = (Boolean)dbObj.get("pay");
  410. //if pay isn't set, set it
  411. if (pay == false) {
  412. dbObj.put("pay", true);
  413. dbObj.put("admin", user);
  414. //otherwise, clear it
  415. }else {
  416. dbObj.put("pay", false);
  417. dbObj.put("admin", "");
  418. }
  419. coll.update(query, dbObj);
  420. return true;
  421. }
  422. public boolean deleteAdminOrderItem(int orderNum,
  423. int startOrderNum,
  424. int endOrderNum,
  425. int tableNum) {
  426. //get the collection
  427. DBCollection coll = db.getCollection("dr_order");
  428. //delete the order
  429. DBObject query = new BasicDBObject();
  430. if (orderNum != 0) {
  431. query.put("order", orderNum);
  432. }else if (startOrderNum != 0 && endOrderNum != 0) {
  433. query.put("order", new BasicDBObject("$gte", startOrderNum).append("$lte", endOrderNum));
  434. }else if (startOrderNum != 0) {
  435. query.put("order", new BasicDBObject("$gte", startOrderNum));
  436. }else if (endOrderNum != 0) {
  437. query.put("order", new BasicDBObject("$lte", endOrderNum));
  438. }else if (tableNum != 0) {
  439. query.put("table", tableNum);
  440. }else {
  441. return false;
  442. }
  443. coll.remove(query);
  444. return true;
  445. }
  446. public ArrayList<Order> getOrderList(int orderNum,
  447. int startOrderNum,
  448. int endOrderNum,
  449. int tableNum) {
  450. Order order = null;
  451. DBObject dbObj = null;
  452. ArrayList<Order> orderList = null;
  453. //get the collection
  454. DBCollection coll = db.getCollection("dr_order");
  455. //get the order
  456. DBObject query = new BasicDBObject();
  457. if (orderNum != 0) {
  458. query.put("order", orderNum);
  459. }else if (startOrderNum != 0 && endOrderNum != 0) {
  460. query.put("order", new BasicDBObject("$gte", startOrderNum).append("$lte", endOrderNum));
  461. }else if (startOrderNum != 0) {
  462. query.put("order", new BasicDBObject("$gte", startOrderNum));
  463. }else if (endOrderNum != 0) {
  464. query.put("order", new BasicDBObject("$lte", endOrderNum));
  465. }else if (tableNum != 0) {
  466. query.put("table", tableNum);
  467. }
  468. DBCursor cr = coll.find(query);
  469. if (cr.hasNext()) {
  470. orderList = new ArrayList<Order>();
  471. }else {
  472. return orderList;
  473. }
  474. //construct the order list
  475. while (cr.hasNext()) {
  476. dbObj = cr.next();
  477. if (dbObj != null) {
  478. order = JSON.parseObject(dbObj.toString(), Order.class);
  479. if (order != null) {
  480. orderList.add(order);
  481. }
  482. }
  483. }
  484. return orderList;
  485. }
  486. }