Advertisement
Guest User

Untitled

a guest
May 11th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 37.22 KB | None | 0 0
  1. package engine.sql;
  2.  
  3. import java.sql.*;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.ArrayList;
  7. import java.util.Map;
  8. import java.util.logging.Logger;
  9.  
  10. import util.Util;
  11.  
  12.  
  13. import engine.CryptTask;
  14. import engine.containers.CurrentEmployeeDataContainer;
  15. import engine.containers.EmployeeScheduleContainer;
  16. import engine.containers.lists.EmployeeLists;
  17. import engine.containers.lists.PositionsInfos;
  18. import engine.containers.lists.StaticInfos;
  19.  
  20. /**
  21. *
  22. * @author Stephane Brousseau
  23. *
  24. */
  25. public class DatabaseFactory
  26. {
  27. private static DatabaseFactory dbf;
  28.  
  29.  
  30.  
  31. private String mydatabase = "payrolldb";
  32. private String url = "jdbc:mysql://localhost:3306/" + mydatabase; // a JDBC url
  33. private String username = "root";
  34. private String password = "";
  35.  
  36. /**
  37. * Logger for this class
  38. */
  39. private static final Logger log = Logger.getLogger(DatabaseFactory.class.getName());
  40.  
  41. public static DatabaseFactory getInstance()
  42. {
  43. if(dbf == null)
  44. {
  45. log.info("Initiating Database Factory");
  46. dbf = new DatabaseFactory();
  47. dbf.testConnection();
  48. }
  49. return dbf;
  50. }
  51.  
  52. private void testConnection()
  53. {
  54. Connection con = getConnection();
  55. if(con != null)
  56. log.info("Connection to the database established with database: "+con.toString());
  57. else
  58. log.info("Connection to the database could not be established");
  59. }
  60.  
  61. public Connection getConnection()
  62. {
  63. Connection connection = null;
  64. try { // Load the JDBC driver
  65. String driverName = "org.gjt.mm.mysql.Driver"; // MySQL MM JDBC driver
  66. Class.forName(driverName); // Create a connection to the database
  67. connection = DriverManager.getConnection(url, username, password);
  68. }
  69. catch (ClassNotFoundException e)
  70. {
  71. log.severe("Could not find the database driver.");
  72. }
  73. catch (SQLException e)
  74. { log.severe("Could not connect to the database.");
  75. }
  76. return connection;
  77. }
  78.  
  79. public List<String> getFreePositionsFor(String name)
  80. {
  81. List<String> currentPositions = EmployeeLists.getAllPositionsFor(name);
  82. if(currentPositions == null)
  83. {
  84. currentPositions = new ArrayList<String>();
  85. }
  86. boolean addAll = false;
  87. List<String> freePositions = new ArrayList<String>();
  88. if(PositionsInfos.getInstance().getAllPositions() == null || PositionsInfos.getInstance().getAllPositions().isEmpty())
  89. addAll = true;
  90. for(String pos: PositionsInfos.getInstance().getAllPositions())
  91. {
  92. if(addAll || !currentPositions.contains(pos))
  93. freePositions.add(pos);
  94. }
  95. if(freePositions.isEmpty())
  96. return null;
  97. return freePositions;
  98. }
  99.  
  100. public List<String> getPositionsFor(String name)
  101. {
  102. CryptTask cTask = CryptTask.getInstance();
  103. List<String> currentPositions = new ArrayList<String>();
  104. Connection con = getConnection();
  105. try
  106. {
  107. ResultSet result;
  108. PreparedStatement statement;
  109. statement = con.prepareStatement("SELECT * FROM work_positions WHERE name=?");
  110. statement.setString(1, cTask.scramble(name));
  111. result = statement.executeQuery();
  112. while(result.next())
  113. {
  114. String position = result.getString("position");
  115. currentPositions.add(position);
  116. }
  117. statement.close();
  118. }
  119. catch (SQLException e)
  120. {
  121. log.info("Error retrieving Staff positions for:"+name);
  122. }
  123. try {
  124. con.close();
  125. } catch (SQLException e) {
  126. e.printStackTrace();
  127. }
  128. return currentPositions;
  129.  
  130. }
  131.  
  132. public List<String> getSalaryEmployees()
  133. {
  134. List<String> salaryEmployees = new ArrayList<String>();
  135. CryptTask cTask = CryptTask.getInstance();
  136. Connection con = getConnection();
  137. try
  138. {
  139. ResultSet result;
  140. PreparedStatement statement;
  141. statement = con.prepareStatement("SELECT * FROM work_positions");
  142. result = statement.executeQuery();
  143. while(result.next())
  144. {
  145. String name = cTask.deScramble(result.getString("name"));
  146. String payType = cTask.decryptText(result.getString("pay_type"));
  147. if(payType.equalsIgnoreCase("w") || payType.equalsIgnoreCase("y"))
  148. {
  149. if(!salaryEmployees.contains(name))
  150. salaryEmployees.add(name);
  151. }
  152. }
  153. statement.close();
  154. }
  155. catch (SQLException e)
  156. {
  157. log.info("Error retrieving salary employees from the database");
  158. }
  159. try {
  160. con.close();
  161. } catch (SQLException e) {
  162. e.printStackTrace();
  163. }
  164. return salaryEmployees;
  165. }
  166.  
  167. public Map<String,Integer> getEmployeeNames()
  168. {
  169. Connection con = getConnection();
  170. Map<String,Integer> employees = new HashMap<String,Integer>();
  171. int lvl = 100;
  172. try
  173. {
  174. ResultSet result;
  175. PreparedStatement statement;
  176. statement = con.prepareStatement("SELECT * FROM employee_list");
  177. result = statement.executeQuery();
  178. while(result.next())
  179. {
  180. String name = result.getString("name");
  181. int id = result.getInt("id");
  182. String newName = CryptTask.getInstance().deScramble(name);
  183. String level = CryptTask.getInstance().decryptText(result.getString("access_level"));
  184. if(!newName.equalsIgnoreCase("Super Administrator") && !newName.equalsIgnoreCase("superadministrator") && (!newName.contains("super") && !newName.contains("administrator")) && id != 1)
  185. {
  186. lvl = getIntValueForAccessLevel(level);
  187. employees.put(newName, lvl);
  188. }
  189. }
  190. statement.close();
  191. }
  192. catch (SQLException e)
  193. {
  194. log.info("Error retrieving Staff names and IDs from the database");
  195. }
  196. try {
  197. con.close();
  198. } catch (SQLException e) {
  199. e.printStackTrace();
  200. }
  201. return employees;
  202. }
  203.  
  204. public List<String> getAllEmployeeNames()
  205. {
  206. Connection con = getConnection();
  207. List<String> employees = new ArrayList<String>();
  208. try
  209. {
  210. ResultSet result;
  211. PreparedStatement statement;
  212. statement = con.prepareStatement("SELECT * FROM employee_list");
  213. result = statement.executeQuery();
  214. while(result.next())
  215. {
  216. String name = result.getString("name");
  217. String newName = CryptTask.getInstance().deScramble(name);
  218. if(!newName.equalsIgnoreCase("Super Administrator") && !newName.equalsIgnoreCase("superadministrator") && (!newName.contains("super") && !newName.contains("administrator")))
  219. {
  220. employees.add(newName);
  221. }
  222. }
  223. statement.close();
  224. }
  225. catch (SQLException e)
  226. {
  227. log.info("Error retrieving Staff names and IDs from the database");
  228. }
  229. try {
  230. con.close();
  231. } catch (SQLException e) {
  232. e.printStackTrace();
  233. }
  234. return employees;
  235. }
  236.  
  237. public Map<String,Object[]> getAllStaffPositions()
  238. {
  239. Connection con = getConnection();
  240. Map<String,Object[]> positions = new HashMap<String,Object[]>();
  241. try
  242. {
  243. ResultSet result;
  244. PreparedStatement statement;
  245. statement = con.prepareStatement("SELECT * FROM positions");
  246. result = statement.executeQuery();
  247. while(result.next())
  248. {
  249. String name = result.getString("position");
  250. double tip_pool_percent = result.getDouble("tip_pool_percent");
  251. int order = result.getInt("order");
  252. int on_schedule = result.getInt("on_schedule");
  253. int on_reports = result.getInt("on_reports");
  254. Object[] newObj = {tip_pool_percent,order,on_schedule,on_reports};
  255. positions.put(name, newObj);
  256. }
  257. statement.close();
  258. }
  259. catch (SQLException e)
  260. {
  261. log.info("Error retrieving Staff positions from the database");
  262. }
  263. try {
  264. con.close();
  265. } catch (SQLException e) {
  266. e.printStackTrace();
  267. }
  268. if(positions.isEmpty())
  269. return null;
  270. return positions;
  271. }
  272.  
  273. public double getTipPercentForPosition(String pos)
  274. {
  275. Connection con = getConnection();
  276. double tipPercent = 0;
  277. try
  278. {
  279. ResultSet result;
  280. PreparedStatement statement;
  281. statement = con.prepareStatement("SELECT * FROM positions WHERE position=?");
  282. statement.setString(1, pos);
  283. result = statement.executeQuery();
  284. while(result.next())
  285. {
  286. tipPercent = result.getDouble("tip_pool_percent");
  287. }
  288. statement.close();
  289. }
  290. catch (SQLException e)
  291. {
  292. log.info("Error retrieving tip percent from the database");
  293. e.printStackTrace();
  294. }
  295. try {
  296. con.close();
  297. } catch (SQLException e) {
  298. e.printStackTrace();
  299. }
  300. return tipPercent;
  301. }
  302.  
  303. public Map<Object[],Map<String,Integer>> getBaseInfosForDate(Double dblDate)
  304. {
  305. return getBaseInfosForDate(Util.doubleToDate(dblDate));
  306. }
  307.  
  308. public Map<Object[],Map<String,Integer>> getBaseInfosForDate(String date)
  309. {
  310. double dblDate = Util.dateToDouble(date);
  311. Connection con = getConnection();
  312. CryptTask cTask = CryptTask.getInstance();
  313. Map<String,Integer> tip_share = new HashMap<String,Integer>();
  314. double totalTips = 0;
  315. double totalSales = 0;
  316. double cashSales = 0;
  317. try
  318. {
  319. ResultSet result;
  320. PreparedStatement statement;
  321. statement = con.prepareStatement("SELECT * FROM payroll WHERE date=?");
  322. statement.setDouble(1, dblDate);
  323. result = statement.executeQuery();
  324. while(result.next())
  325. {
  326. int num = 1;
  327. String position = cTask.deScramble(result.getString("position"));
  328. if(tip_share.containsKey(position))
  329. {
  330. num = tip_share.get(position) + 1;
  331. }
  332. tip_share.put(position, num);
  333. }
  334. statement.close();
  335. }
  336. catch (SQLException e)
  337. {
  338. log.info("Error retrieving base infos from the database");
  339. }
  340. try
  341. {
  342. ResultSet result;
  343. PreparedStatement statement;
  344. statement = con.prepareStatement("SELECT * FROM payroll_base_infos WHERE date=?");
  345. statement.setDouble(1, dblDate);
  346. result = statement.executeQuery();
  347. while(result.next())
  348. {
  349. String newTips = "";
  350. String newSales = "";
  351. String newCashSales = "";
  352. newTips = cTask.decryptText(result.getString("tips")).replace(",", ".");
  353. newSales = cTask.decryptText(result.getString("sales")).replace(",", ".");
  354. newCashSales = cTask.decryptText(result.getString("cash_sales")).replace(",", ".");
  355. if(newTips.isEmpty() || newTips.equals(""))
  356. totalTips = 0;
  357. else
  358. totalTips = Double.valueOf(newTips);
  359. if(newSales.isEmpty() || newSales.equals(""))
  360. totalSales = 0;
  361. else
  362. totalSales = Double.valueOf(newSales);
  363. if(newCashSales.isEmpty() || newCashSales.equals(""))
  364. cashSales = 0;
  365. else
  366. cashSales = Double.valueOf(newCashSales);
  367. }
  368. statement.close();
  369. }
  370. catch (SQLException e)
  371. {
  372. log.info("Error retrieving base infos from the payroll_base_infos table");
  373. }
  374. Object[] baseInfos = {totalTips,totalSales,cashSales};
  375. try {
  376. con.close();
  377. } catch (SQLException e) {
  378. e.printStackTrace();
  379. }
  380. Map<Object[],Map<String,Integer>> resultMap = new HashMap<Object[],Map<String,Integer>>();
  381. resultMap.put(baseInfos, tip_share);
  382. return resultMap;
  383. }
  384.  
  385. public void deletePosition(String pos)
  386. {
  387. Connection con = getConnection();
  388. try
  389. {
  390. PreparedStatement statement;
  391. statement = con.prepareStatement("DELETE FROM positions WHERE position=?");
  392. statement.setString(1, pos);
  393. statement.execute();
  394. statement.close();
  395. }
  396. catch (SQLException e)
  397. {
  398. log.info("Error deleting data from positions in the database for position: "+pos);
  399. }
  400. try {
  401. con.close();
  402. } catch (SQLException e) {
  403. e.printStackTrace();
  404. }
  405. }
  406.  
  407. public void addPosition(String pos, double tipPercent, int indexOrder, int onReports, int onSchedule)
  408. {
  409. deletePosition(pos);
  410. Connection con = getConnection();
  411. try
  412. {
  413. PreparedStatement statement = null;
  414. statement = con.prepareStatement("INSERT INTO positions values (?,?,?,?,?)");
  415. statement.setString(1, pos);
  416. statement.setDouble(2, tipPercent);
  417. statement.setInt(3,indexOrder);
  418. statement.setInt(4,onReports);
  419. statement.setInt(5,onSchedule);
  420. statement.execute();
  421. statement.close();
  422. }
  423. catch (Exception e)
  424. {
  425. log.info("Error inserting positions data in the database for position: "+pos);
  426. e.printStackTrace();
  427. }
  428. try {
  429. con.close();
  430. } catch (SQLException e) {
  431. e.printStackTrace();
  432. }
  433. }
  434.  
  435. public void saveBaseInfosForDate(double dblDate, String[] infos)
  436. {
  437. saveBaseInfosForDate(Util.doubleToDate(dblDate), infos);
  438. }
  439.  
  440. public void saveBaseInfosForDate(String date, String[] infos)
  441. {
  442. double dblDate = Util.dateToDouble(date);
  443. Connection con = getConnection();
  444. CryptTask cTask = CryptTask.getInstance();
  445. try
  446. {
  447. PreparedStatement statement;
  448. statement = con.prepareStatement("DELETE FROM payroll_base_infos WHERE date=?");
  449. statement.setDouble(1, dblDate);
  450. statement.execute();
  451. statement.close();
  452. }
  453. catch (SQLException e)
  454. {
  455. log.info("Error deleting global_data from the database");
  456. }
  457. try
  458. {
  459. PreparedStatement statement = null;
  460. statement = con.prepareStatement("INSERT INTO payroll_base_infos (date,tips,sales,cash_sales) values (?,?,?,?)");
  461. statement.setDouble(1, dblDate);
  462. statement.setString(2, cTask.encryptText(infos[0]));
  463. statement.setString(3, cTask.encryptText(infos[1]));
  464. statement.setString(4, cTask.encryptText(infos[2]));
  465. statement.execute();
  466. statement.close();
  467. }
  468. catch (Exception e)
  469. {
  470. log.info("Error inserting payroll base infos data in the database for date: "+date);
  471. e.printStackTrace();
  472. }
  473. try {
  474. con.close();
  475. } catch (SQLException e) {
  476. e.printStackTrace();
  477. }
  478. }
  479.  
  480. public int getIdForEmployee(String name)
  481. {
  482. int ID = -1;
  483. Connection con = getConnection();
  484. CryptTask cTask = CryptTask.getInstance();
  485. try
  486. {
  487. ResultSet result;
  488. PreparedStatement statement = null;
  489. statement = con.prepareStatement("SELECT * FROM employee_list WHERE name=?");
  490. statement.setString(1, cTask.scramble(name));
  491. result = statement.executeQuery();
  492. while(result.next())
  493. {
  494. ID = result.getInt("id");
  495. }
  496. statement.close();
  497. }
  498. catch (Exception e)
  499. {
  500. log.warning("Exception: Retrieve employee ID: " + e.getMessage());
  501. e.printStackTrace();
  502. }
  503. try {
  504. con.close();
  505. } catch (SQLException e) {
  506. e.printStackTrace();
  507. }
  508. return ID;
  509. }
  510.  
  511. public void deleteAccessLevel(String name)
  512. {
  513. if(!getAllAccessLevels().contains(name))
  514. return;
  515. Connection con = getConnection();
  516. try
  517. {
  518. PreparedStatement statement;
  519. statement = con.prepareStatement("DELETE FROM access_levels WHERE name=?");
  520. statement.setString(1, name);
  521. statement.execute();
  522. statement.close();
  523. }
  524. catch (SQLException e)
  525. {
  526. log.info("Error deleting access level from the database for name:"+name);
  527. e.printStackTrace();
  528. }
  529. try {
  530. con.close();
  531. } catch (SQLException e) {
  532. e.printStackTrace();
  533. }
  534. }
  535.  
  536. public List<String> getAllAccessLevels()
  537. {
  538. Connection con = getConnection();
  539. List<String> accessLevels = new ArrayList<String>();
  540. try
  541. {
  542. ResultSet result;
  543. PreparedStatement statement;
  544. statement = con.prepareStatement("SELECT * FROM access_levels");
  545. result = statement.executeQuery();
  546. while(result.next())
  547. {
  548. String curName = result.getString("name");
  549. if(!accessLevels.contains(curName))
  550. {
  551. accessLevels.add(curName);
  552. }
  553.  
  554. }
  555. statement.close();
  556. }
  557. catch (SQLException e)
  558. {
  559. log.info("Error retrieving access levels from the database");
  560. }
  561. try {
  562. con.close();
  563. } catch (SQLException e) {
  564. e.printStackTrace();
  565. }
  566. return accessLevels;
  567. }
  568.  
  569. public int getIntValueForAccessLevel(String name)
  570. {
  571. int value = 100;
  572. Connection con = getConnection();
  573. try
  574. {
  575. ResultSet result;
  576. PreparedStatement statement;
  577. statement = con.prepareStatement("SELECT * FROM access_levels WHERE name=?");
  578. statement.setString(1, name);
  579. result = statement.executeQuery();
  580. while(result.next())
  581. {
  582. value = result.getInt("level");
  583. }
  584. statement.close();
  585. }
  586. catch (SQLException e)
  587. {
  588. log.info("Error retrieving access levels from the database");
  589. }
  590. try {
  591. con.close();
  592. } catch (SQLException e) {
  593. e.printStackTrace();
  594. }
  595. return value;
  596. }
  597.  
  598. public int getAccessLevelValueFor(String name)
  599. {
  600. int level = -1;
  601. Connection con = getConnection();
  602. try
  603. {
  604. ResultSet result;
  605. PreparedStatement statement = null;
  606. statement = con.prepareStatement("SELECT * FROM access_levels WHERE name=?");
  607. statement.setString(1, name);
  608. result = statement.executeQuery();
  609. while(result.next())
  610. {
  611. level = result.getInt("level");
  612. }
  613. statement.close();
  614. }
  615. catch (Exception e)
  616. {
  617. log.warning("Exception: Retrieve access level value: " + e.getMessage());
  618. e.printStackTrace();
  619. }
  620. try {
  621. con.close();
  622. } catch (SQLException e) {
  623. e.printStackTrace();
  624. }
  625. return level;
  626. }
  627. public String getAccessLevelFor(String name)
  628. {
  629. String level = "";
  630. CryptTask cTask = CryptTask.getInstance();
  631. Connection con = getConnection();
  632. try
  633. {
  634. ResultSet result;
  635. PreparedStatement statement = null;
  636. statement = con.prepareStatement("SELECT * FROM employee_list WHERE name=?");
  637. statement.setString(1, cTask.scramble(name));
  638. result = statement.executeQuery();
  639. while(result.next())
  640. {
  641. level = cTask.decryptText(result.getString("access_level"));
  642. }
  643. statement.close();
  644. }
  645. catch (Exception e)
  646. {
  647. log.warning("Exception: Retrieve access level: " + e.getMessage());
  648. e.printStackTrace();
  649. }
  650. try {
  651. con.close();
  652. } catch (SQLException e) {
  653. e.printStackTrace();
  654. }
  655. return level;
  656. }
  657.  
  658. public void saveNewAccessLevel(String name, int level)
  659. {
  660. Connection con = getConnection();
  661. deleteAccessLevel(name);
  662. try
  663. {
  664.  
  665. PreparedStatement statement = null;
  666. statement = con.prepareStatement("INSERT INTO access_levels (name,level) values (?,?)");
  667. statement.setString(1, name);
  668. statement.setInt(2, level);
  669. statement.execute();
  670. statement.close();
  671.  
  672. }
  673. catch (Exception e)
  674. {
  675. log.info("Error inserting new access level:"+name);
  676. e.printStackTrace();
  677. }
  678. try {
  679. con.close();
  680. } catch (SQLException e) {
  681. e.printStackTrace();
  682. }
  683. log.info("Added access level name:"+name+" - level:"+level);
  684. }
  685.  
  686. public Map<Integer,String[]> getUserAndPass()
  687. {
  688. Connection con = getConnection();
  689. Map<Integer,String[]> logInfos = new HashMap<Integer,String[]>();
  690. CryptTask cTask = CryptTask.getInstance();
  691. try
  692. {
  693. ResultSet result;
  694. PreparedStatement statement;
  695. statement = con.prepareStatement("SELECT * FROM global_data");
  696. result = statement.executeQuery();
  697. while(result.next())
  698. {
  699. int id = result.getInt("id");
  700. String uName = (cTask.decryptText(result.getString("cname")));
  701. String pName = (cTask.decryptText(result.getString("log")));
  702. String[] logInfo = {uName,pName};
  703. logInfos.put(id, logInfo);
  704. }
  705. statement.close();
  706. }
  707. catch (SQLException e)
  708. {
  709. log.info("Error retrieving log infos from the database");
  710. }
  711. if(logInfos.isEmpty())
  712. return logInfos;
  713. try {
  714. con.close();
  715. } catch (SQLException e) {
  716. e.printStackTrace();
  717. }
  718. return logInfos;
  719. }
  720.  
  721. public void deleteUserPass(int ID)
  722. {
  723. Connection con = getConnection();
  724. try
  725. {
  726. PreparedStatement statement;
  727. statement = con.prepareStatement("DELETE FROM global_data WHERE id>=?");
  728. statement.setInt(1, ID);
  729. statement.execute();
  730. statement.close();
  731. }
  732. catch (SQLException e)
  733. {
  734. log.info("Error deleting global_data from the database for userID:"+ID);
  735. }
  736. log.info("Deleted the old username and password from database successfully.");
  737. try {
  738. con.close();
  739. } catch (SQLException e) {
  740. e.printStackTrace();
  741. }
  742. }
  743.  
  744. public void saveUserPass(int ID, String cname, String l)
  745. {
  746. Connection con = getConnection();
  747. deleteUserPass(ID);
  748. log.info("Deleted the old username and password from database successfully.");
  749. CryptTask cTask = CryptTask.getInstance();
  750. try
  751. {
  752.  
  753. PreparedStatement statement = null;
  754. statement = con.prepareStatement("INSERT INTO global_data (id,cname,log) values (?,?,?)");
  755. statement.setInt(1, ID);
  756. statement.setString(2, cTask.encryptText(cname));
  757. statement.setString(3, cTask.encryptText(l));
  758. statement.execute();
  759. statement.close();
  760.  
  761. }
  762. catch (Exception e)
  763. {
  764. log.info("Error inserting log infos in the database for userID:"+ID);
  765. e.printStackTrace();
  766. }
  767. try {
  768. con.close();
  769. } catch (SQLException e) {
  770. e.printStackTrace();
  771. }
  772. }
  773.  
  774. public List<Double> getSavedDates()
  775. {
  776. List<Double> savedDates = new ArrayList<Double>();
  777. Connection con = getConnection();
  778. try
  779. {
  780. ResultSet result;
  781. PreparedStatement statement;
  782. statement = con.prepareStatement("SELECT date FROM payroll");
  783. result = statement.executeQuery();
  784. while(result.next())
  785. {
  786. double date = result.getDouble("date");
  787. if(!savedDates.contains(date))
  788. savedDates.add(date);
  789. }
  790. statement.close();
  791. }
  792. catch (SQLException e)
  793. {
  794. log.info("Error with getAllInfosForDate from the database");
  795. }
  796. try {
  797. con.close();
  798. } catch (SQLException e) {
  799. e.printStackTrace();
  800. }
  801. return savedDates;
  802. }
  803.  
  804. public Map<String,List<CurrentEmployeeDataContainer>> getAllInfosForDate(double dblDate)
  805. {
  806. return getAllInfosForDate(Util.doubleToDate(dblDate));
  807. }
  808.  
  809. public Map<String,List<CurrentEmployeeDataContainer>> getAllInfosForDate(String date)
  810. {
  811. double dblDate = Util.dateToDouble(date);
  812. if(getSavedDates() != null && getSavedDates().contains(dblDate))
  813. {
  814. Connection con = getConnection();
  815. CryptTask cTask = CryptTask.getInstance();
  816. Map<String,List<CurrentEmployeeDataContainer>> container = new HashMap<String,List<CurrentEmployeeDataContainer>>();
  817. for(String pos: getAllStaffPositions().keySet())
  818. {
  819. String cryptPos = cTask.scramble(pos);
  820. List<CurrentEmployeeDataContainer> containerList = new ArrayList<CurrentEmployeeDataContainer>();
  821. try
  822. {
  823. ResultSet result;
  824. PreparedStatement statement;
  825. statement = con.prepareStatement("SELECT * FROM payroll WHERE date=? AND position=?");
  826. statement.setDouble(1, dblDate);
  827. statement.setString(2, cryptPos);
  828. result = statement.executeQuery();
  829. while(result.next())
  830. {
  831. CurrentEmployeeDataContainer newContainer = new CurrentEmployeeDataContainer();
  832. String name = result.getString("name");
  833. String startTime = cTask.decryptText(result.getString("start_time"));
  834. String endTime = cTask.decryptText(result.getString("end_time"));
  835. double tips = Double.parseDouble(cTask.decryptText(result.getString("tips")));
  836. double cashTips = Double.parseDouble(cTask.decryptText(result.getString("cash_tips")));
  837. String newName = CryptTask.getInstance().deScramble(name);
  838. newContainer.name = newName;
  839. newContainer.startTime = startTime;
  840. newContainer.endTime = endTime;
  841. newContainer.tips = tips;
  842. newContainer.cashTips = cashTips;
  843. newContainer.position = pos;
  844. containerList.add(newContainer);
  845. }
  846. container.put(pos, containerList);
  847. statement.close();
  848. }
  849. catch (SQLException e)
  850. {
  851. log.info("Error with getAllInfosForDate from the database");
  852. }
  853. }
  854. try {
  855. con.close();
  856. } catch (SQLException e) {
  857. e.printStackTrace();
  858. }
  859. return container;
  860. }
  861. else
  862. return null;
  863.  
  864. }
  865.  
  866. public void updateInfosForDate(String employeeName, double dblDate, Object[] data) //{startTime,endTime,tips,position}
  867. {
  868. updateInfosForDate(employeeName, Util.doubleToDate(dblDate), data);
  869. }
  870.  
  871. public void updateInfosForDate(String employeeName, String date, Object[] data) //{startTime,endTime,tips,position}
  872. {
  873. double dblDate = Util.dateToDouble(date);
  874. Connection con = getConnection();
  875. CryptTask cTask = CryptTask.getInstance();
  876. removeEmployeeFromPayrollForDate(employeeName, date);
  877. try
  878. {
  879. PreparedStatement statement = null;
  880. statement = con.prepareStatement("INSERT INTO payroll (date,name,position,start_time,end_time,tips,cash_tips) values (?,?,?,?,?,?,?)");
  881. statement.setDouble(1, dblDate);
  882. statement.setString(2, cTask.scramble(employeeName));
  883. statement.setString(3, cTask.scramble(String.valueOf(data[3])));
  884. statement.setString(4, cTask.encryptText(String.valueOf(data[0])));
  885. statement.setString(5, cTask.encryptText(String.valueOf(data[1])));
  886. statement.setString(6, cTask.encryptText(String.valueOf(data[2])));
  887. statement.setString(7, cTask.encryptText(String.valueOf(data[4])));
  888. statement.execute();
  889. statement.close();
  890. con.close();
  891. }
  892. catch (Exception e)
  893. {
  894. log.info("Error inserting payroll data in the database for employee "+employeeName+" and date: "+date);
  895. e.printStackTrace();
  896. }
  897. try {
  898. con.close();
  899. } catch (SQLException e) {
  900. e.printStackTrace();
  901. }
  902. }
  903.  
  904. public void saveEmployee(String curName,String access_level,double hireDate)
  905. {
  906. Connection con = getConnection();
  907. CryptTask cTask = CryptTask.getInstance();
  908. try
  909. {
  910. PreparedStatement statement = null;
  911. statement = con.prepareStatement("INSERT INTO employee_list (name,access_level,hire_date) values (?,?,?)");
  912. //statement.setInt(1, 0);
  913. statement.setString(1, cTask.scramble(curName));
  914. statement.setString(2, cTask.encryptText(access_level));
  915. statement.setDouble(3, hireDate);
  916. statement.execute();
  917. statement.close();
  918. }
  919. catch (Exception e)
  920. {
  921. log.warning("Exception: Save new employee: " + e.getMessage());
  922. e.printStackTrace();
  923. }
  924. try {
  925. con.close();
  926. } catch (SQLException e) {
  927. e.printStackTrace();
  928. }
  929. }
  930. public void updateEmployeeAccessLevel(String curName,String access_level)
  931. {
  932. Connection con = getConnection();
  933. CryptTask cTask = CryptTask.getInstance();
  934. try
  935. {
  936. PreparedStatement statement = null;
  937. statement = con.prepareStatement("UPDATE employee_list SET access_level=? WHERE name=?");
  938. statement.setString(1, cTask.encryptText(access_level));
  939. statement.setString(2, cTask.scramble(curName));
  940. statement.execute();
  941. statement.close();
  942. }
  943. catch (Exception e)
  944. {
  945. log.warning("Exception: Update access level: " + e.getMessage());
  946. e.printStackTrace();
  947. }
  948. try {
  949. con.close();
  950. } catch (SQLException e) {
  951. e.printStackTrace();
  952. }
  953. }
  954.  
  955. public void saveSuperAdmin()
  956. {
  957. Connection con = getConnection();
  958. CryptTask cTask = CryptTask.getInstance();
  959. try
  960. {
  961. PreparedStatement statement = null;
  962. statement = con.prepareStatement("INSERT INTO employee_list (id,name,access_level) values (?,?,?)");
  963. statement.setInt(1, 1);
  964. statement.setString(2, cTask.scramble("SuperAdministrator"));
  965. statement.setString(3, cTask.encryptText(String.valueOf(0)));
  966. statement.execute();
  967. statement.close();
  968. }
  969. catch (Exception e)
  970. {
  971. log.warning("Exception: Save super administrator: " + e.getMessage());
  972. e.printStackTrace();
  973. }
  974. try {
  975. con.close();
  976. } catch (SQLException e) {
  977. e.printStackTrace();
  978. }
  979. }
  980.  
  981. public String getEmployeeNameFromId(int ID)
  982. {
  983. String name = "";
  984. Connection con = getConnection();
  985. CryptTask cTask = CryptTask.getInstance();
  986. try
  987. {
  988. ResultSet result;
  989. PreparedStatement statement = null;
  990. statement = con.prepareStatement("SELECT * FROM employee_list WHERE id=?");
  991. statement.setInt(1, ID);
  992. result = statement.executeQuery();
  993. while(result.next())
  994. {
  995. name = cTask.deScramble(result.getString("name"));
  996. }
  997. statement.close();
  998. }
  999. catch (Exception e)
  1000. {
  1001. log.warning("Exception: Save new employee: " + e.getMessage());
  1002. e.printStackTrace();
  1003. }
  1004. try {
  1005. con.close();
  1006. } catch (SQLException e) {
  1007. e.printStackTrace();
  1008. }
  1009. return name;
  1010. }
  1011.  
  1012. public double getHireDateForEmployee(String name)
  1013. {
  1014. double date = 0;
  1015. Connection con = getConnection();
  1016. CryptTask cTask = CryptTask.getInstance();
  1017. try
  1018. {
  1019. ResultSet result;
  1020. PreparedStatement statement = null;
  1021. statement = con.prepareStatement("SELECT * FROM employee_list WHERE name=?");
  1022. statement.setString(1, cTask.scramble(name));
  1023. result = statement.executeQuery();
  1024. while(result.next())
  1025. {
  1026. date = result.getDouble("hire_date");
  1027. }
  1028. statement.close();
  1029. }
  1030. catch (Exception e)
  1031. {
  1032. log.warning("Exception: Save new employee: " + e.getMessage());
  1033. e.printStackTrace();
  1034. }
  1035. try {
  1036. con.close();
  1037. } catch (SQLException e) {
  1038. e.printStackTrace();
  1039. }
  1040. return date;
  1041. }
  1042.  
  1043. public String getEmployeeNameFromUserName(String username)
  1044. {
  1045. String name = "";
  1046. int ID = 0;
  1047. Connection con = getConnection();
  1048. CryptTask cTask = CryptTask.getInstance();
  1049. try
  1050. {
  1051. ResultSet result;
  1052. PreparedStatement statement = null;
  1053. statement = con.prepareStatement("SELECT * FROM global_data");
  1054. result = statement.executeQuery();
  1055. while(result.next())
  1056. {
  1057. String uname = cTask.decryptText(result.getString("cname"));
  1058. if(uname.equals(username))
  1059. ID = result.getInt("id");
  1060. }
  1061. statement.close();
  1062. }
  1063. catch (Exception e)
  1064. {
  1065. e.printStackTrace();
  1066. }
  1067. try
  1068. {
  1069. ResultSet result;
  1070. PreparedStatement statement = null;
  1071. statement = con.prepareStatement("SELECT * FROM employee_list WHERE id=?");
  1072. statement.setInt(1, ID);
  1073. result = statement.executeQuery();
  1074. while(result.next())
  1075. {
  1076. name = cTask.deScramble(result.getString("name"));
  1077. }
  1078. statement.close();
  1079. }
  1080. catch (Exception e)
  1081. {
  1082. e.printStackTrace();
  1083. }
  1084. try {
  1085. con.close();
  1086. } catch (SQLException e) {
  1087. e.printStackTrace();
  1088. }
  1089. return name;
  1090. }
  1091.  
  1092. public void removeEmployeeByName(String name)
  1093. {
  1094. Connection con = getConnection();
  1095. CryptTask cTask = CryptTask.getInstance();
  1096. try
  1097. {
  1098. PreparedStatement statement;
  1099. statement = con.prepareStatement("DELETE FROM employee_list WHERE name=?");
  1100. statement.setString(1, cTask.scramble(name));
  1101. statement.execute();
  1102. statement.close();
  1103. }
  1104. catch (SQLException e)
  1105. {
  1106. log.info("Error deleting employee "+name+" from the database");
  1107. }
  1108. try
  1109. {
  1110. PreparedStatement statement;
  1111. statement = con.prepareStatement("DELETE FROM work_positions WHERE name=?");
  1112. statement.setString(1, cTask.scramble(name));
  1113. statement.execute();
  1114. statement.close();
  1115. }
  1116. catch (SQLException e)
  1117. {
  1118. log.info("Error deleting staff positions for employee "+name+" from the database");
  1119. }
  1120. try {
  1121. con.close();
  1122. } catch (SQLException e) {
  1123. e.printStackTrace();
  1124. }
  1125. }
  1126.  
  1127. public void deleteEmployeePosition(String curName, String position)
  1128. {
  1129. Connection con = getConnection();
  1130. CryptTask cTask = CryptTask.getInstance();
  1131. try
  1132. {
  1133. PreparedStatement statement;
  1134. statement = con.prepareStatement("DELETE FROM work_positions WHERE name=? AND position=?");
  1135. statement.setString(1, cTask.scramble(curName));
  1136. statement.setString(2, position);
  1137. statement.execute();
  1138. statement.close();
  1139. }
  1140. catch (SQLException e)
  1141. {
  1142. log.info("Error deleting staff position for employee "+curName+" from the database");
  1143. }
  1144. try {
  1145. con.close();
  1146. } catch (SQLException e) {
  1147. e.printStackTrace();
  1148. }
  1149. }
  1150.  
  1151. public void addEmployeePosition(String curName, String position, double salary, String pay_type)
  1152. {
  1153. Connection con = getConnection();
  1154. CryptTask cTask = CryptTask.getInstance();
  1155. deleteEmployeePosition(curName,position);
  1156. try
  1157. {
  1158. PreparedStatement statement = null;
  1159. statement = con.prepareStatement("INSERT INTO work_positions (name,position,salary,pay_type) values (?,?,?,?)");
  1160. statement.setString(1, cTask.scramble(curName));
  1161. statement.setString(2, position);
  1162. statement.setString(3, cTask.encryptText(String.valueOf(salary)));
  1163. statement.setString(4, cTask.encryptText(pay_type));
  1164. statement.execute();
  1165.  
  1166. statement.close();
  1167. }
  1168. catch (Exception e)
  1169. {
  1170. log.warning("Exception: Save staff positions: " + e.getMessage());
  1171. e.printStackTrace();
  1172. }
  1173. try {
  1174. con.close();
  1175. } catch (SQLException e) {
  1176. e.printStackTrace();
  1177. }
  1178. }
  1179.  
  1180. public String[] getSalaryForEmployee(String name, String position)
  1181. {
  1182. String salary = "";
  1183. String payType = "";
  1184. Connection con = getConnection();
  1185. CryptTask cTask = CryptTask.getInstance();
  1186. try
  1187. {
  1188. ResultSet result;
  1189. PreparedStatement statement = null;
  1190. statement = con.prepareStatement("SELECT * FROM work_positions WHERE name=? AND position=?");
  1191. statement.setString(1, cTask.scramble(name));
  1192. statement.setString(2, position);
  1193. result = statement.executeQuery();
  1194. while(result.next())
  1195. {
  1196. salary = cTask.decryptText(result.getString("salary"));
  1197. payType = cTask.decryptText(result.getString("pay_type"));
  1198. }
  1199. statement.close();
  1200. }
  1201. catch (Exception e)
  1202. {
  1203. log.warning("Exception: Retrieve salary: " + e.getMessage());
  1204. e.printStackTrace();
  1205. }
  1206. try {
  1207. con.close();
  1208. } catch (SQLException e) {
  1209. e.printStackTrace();
  1210. }
  1211. String[] sal = {salary,payType};
  1212. return sal;
  1213. }
  1214.  
  1215. public void removeEmployeeFromPayrollForDate(String name, double dblDate)
  1216. {
  1217. removeEmployeeFromPayrollForDate(name, Util.doubleToDate(dblDate));
  1218. }
  1219.  
  1220. public void removeEmployeeFromPayrollForDate(String name, String date)
  1221. {
  1222. double dblDate = Util.dateToDouble(date);
  1223. Connection con = getConnection();
  1224. CryptTask cTask = CryptTask.getInstance();
  1225. try
  1226. {
  1227. PreparedStatement statement;
  1228. statement = con.prepareStatement("DELETE FROM payroll WHERE name=? AND date=?");
  1229. statement.setString(1, cTask.scramble(name));
  1230. statement.setDouble(2, dblDate);
  1231. statement.execute();
  1232. statement.close();
  1233. }
  1234. catch (SQLException e)
  1235. {
  1236. log.info("Error deleting payroll data for employee "+name+" and date: "+date+" from the database");
  1237. }
  1238. try {
  1239. con.close();
  1240. } catch (SQLException e) {
  1241. e.printStackTrace();
  1242. }
  1243. }
  1244.  
  1245. public double getScheduleStartDate(List<Double> datesList)
  1246. {
  1247. Connection con = getConnection();
  1248. CryptTask cTask = CryptTask.getInstance();
  1249. double correctDate = 0;
  1250. for(double curDate:datesList)
  1251. {
  1252. try
  1253. {
  1254. ResultSet result;
  1255. PreparedStatement statement = null;
  1256. statement = con.prepareStatement("SELECT * FROM schedule WHERE date=?");
  1257. statement.setDouble(1, curDate);
  1258. result = statement.executeQuery();
  1259. while(result.next())
  1260. {
  1261. correctDate = result.getDouble("date");
  1262. }
  1263. statement.close();
  1264. }
  1265. catch (Exception e)
  1266. {
  1267. log.warning("Exception: Retrieve schedule start date: " + e.getMessage());
  1268. e.printStackTrace();
  1269. }
  1270. }
  1271. try {
  1272. con.close();
  1273. } catch (SQLException e) {
  1274. e.printStackTrace();
  1275. }
  1276. if(correctDate != 0)
  1277. return correctDate;
  1278. else
  1279. return 0;
  1280. }
  1281.  
  1282. public List<EmployeeScheduleContainer> getScheduleFor(double startDate)
  1283. {
  1284. List<EmployeeScheduleContainer> newSchedule = new ArrayList<EmployeeScheduleContainer>();
  1285. Connection con = getConnection();
  1286. try
  1287. {
  1288. ResultSet result;
  1289. PreparedStatement statement = null;
  1290. statement = con.prepareStatement("SELECT * FROM schedule WHERE date=?");
  1291. statement.setDouble(1, startDate);
  1292. result = statement.executeQuery();
  1293. while(result.next())
  1294. {
  1295. EmployeeScheduleContainer container = new EmployeeScheduleContainer();
  1296. container.setName(result.getString("name"));
  1297. String str1 = result.getString("sunday");
  1298. String str2 = result.getString("monday");
  1299. String str3 = result.getString("tuesday");
  1300. String str4 = result.getString("wednesday");
  1301. String str5 = result.getString("thursday");
  1302. String str6 = result.getString("friday");
  1303. String str7 = result.getString("saturday");
  1304. String[] sched = {str1,str2,str3,str4,str5,str6,str7};
  1305. container.setSchedule(sched);
  1306. container.setStartDay(startDate);
  1307. newSchedule.add(container);
  1308. }
  1309. statement.close();
  1310. }
  1311. catch (Exception e)
  1312. {
  1313. log.warning("Exception: Retrieve schedule: " + e.getMessage());
  1314. e.printStackTrace();
  1315. }
  1316. try {
  1317. con.close();
  1318. } catch (SQLException e) {
  1319. e.printStackTrace();
  1320. }
  1321.  
  1322. return newSchedule;
  1323. }
  1324.  
  1325. public void saveScheduleForDate(double startDate,List<EmployeeScheduleContainer> scheduleList)
  1326. {
  1327. deleteScheduleForDate(startDate);
  1328. Connection con = getConnection();
  1329. for(EmployeeScheduleContainer container:scheduleList)
  1330. {
  1331. try
  1332. {
  1333. PreparedStatement statement = null;
  1334. statement = con.prepareStatement("INSERT INTO schedule (date,name,monday,tuesday,wednesday,thursday,friday,saturday) values (?,?,?,?,?,?,?,?)");
  1335. statement.setDouble(1, startDate);
  1336. statement.setString(2, container.getEmployeeName());
  1337. statement.setString(3, container.getScheduleForDayOfWeek(0));
  1338. statement.setString(4, container.getScheduleForDayOfWeek(1));
  1339. statement.setString(5, container.getScheduleForDayOfWeek(2));
  1340. statement.setString(6, container.getScheduleForDayOfWeek(3));
  1341. statement.setString(7, container.getScheduleForDayOfWeek(4));
  1342. statement.setString(8, container.getScheduleForDayOfWeek(5));
  1343. statement.setString(9, container.getScheduleForDayOfWeek(6));
  1344. statement.execute();
  1345.  
  1346. statement.close();
  1347. }
  1348. catch (Exception e)
  1349. {
  1350. log.warning("Exception: Save schedule: " + e.getMessage());
  1351. e.printStackTrace();
  1352. } }
  1353.  
  1354. try {
  1355. con.close();
  1356. } catch (SQLException e) {
  1357. e.printStackTrace();
  1358. }
  1359.  
  1360. }
  1361.  
  1362. public void deleteScheduleForDate(double startDate)
  1363. {
  1364. Connection con = getConnection();
  1365. try
  1366. {
  1367. PreparedStatement statement;
  1368. statement = con.prepareStatement("DELETE FROM schedule WHERE date=?");
  1369. statement.setDouble(1, startDate);
  1370. statement.execute();
  1371. statement.close();
  1372. }
  1373. catch (SQLException e)
  1374. {
  1375. log.info("Error deleting schedule for start date "+startDate+" from the database");
  1376. }
  1377. try {
  1378. con.close();
  1379. } catch (SQLException e) {
  1380. e.printStackTrace();
  1381. }
  1382. }
  1383.  
  1384. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement