Advertisement
Guest User

Untitled

a guest
Dec 16th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.19 KB | None | 0 0
  1. package rest;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.Driver;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.SQLIntegrityConstraintViolationException;
  9. import java.sql.Statement;
  10. import java.util.ArrayList;
  11. import java.util.Enumeration;
  12.  
  13. public class Database {
  14. static Connection conn;
  15.  
  16. public static void connect() {
  17. String user = "root";
  18. String password = user;
  19. try {
  20. Class.forName("com.mysql.jdbc.Driver").newInstance();
  21.  
  22. } catch (ClassNotFoundException ex) {
  23. System.out.println("Error: unable to load driver class!");
  24. } catch (IllegalAccessException ex) {
  25. System.out.println("Error: access problem while loading!");
  26. } catch (InstantiationException ex) {
  27. System.out.println("Error: unable to instantiate driver!");
  28. }
  29. try {
  30. conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/paldb", user, password);
  31. } catch (SQLException e) {
  32. // TODO Auto-generated catch block
  33. System.out.println(e.getMessage());
  34. }
  35.  
  36. if (conn != null) {
  37. System.out.println("Connected to the database");
  38. }
  39. }
  40.  
  41. public static ArrayList<Employee> getAllEmployes() {
  42. ArrayList<Employee> list = new ArrayList<Employee>();
  43. Statement stmt = null;
  44. try {
  45. if (conn != null) {
  46. stmt = (Statement) conn.createStatement();
  47. stmt.executeQuery("SELECT e.*, l.Name, max(a.TimeS), photo from employes e , photos p, locals l, accesses a where p.IdEmployee= e.SerialNumber AND e.Causal IS NULL and a.IdEmployee=e.SerialNumber and l.Id=a.IdLocal GROUP BY e.SerialNumber");
  48. ResultSet rs = stmt.getResultSet();
  49. while (rs.next()) {
  50. String serial = rs.getString("SerialNumber");
  51. String name = rs.getString("Name");
  52. String surname = rs.getString("Surname");
  53. String auth = rs.getString("AuthGrade");
  54. String position = rs.getString("l.Name");
  55. String photo = rs.getString("photo");
  56.  
  57. Employee temp = new Employee(serial, name, surname, auth, position);
  58. temp.setPhoto(photo);
  59. list.add(temp);
  60. }
  61. stmt.executeQuery("SELECT e.*, photo from employes e, photos p \r\n" +
  62. " where p.IdEmployee=e.SerialNumber and e.Causal IS NULL and e.SerialNumber not in(SELECT e.SerialNumber from employes e , locals l, accesses a \r\n" +
  63. " where a.IdEmployee=e.SerialNumber and l.Id=a.IdLocal GROUP BY e.SerialNumber)\r\n" +
  64. " GROUP BY e.SerialNumber");
  65. rs = stmt.getResultSet();
  66. while (rs.next()) {
  67. String serial = rs.getString("SerialNumber");
  68. String name = rs.getString("Name");
  69. String surname = rs.getString("Surname");
  70. String auth = rs.getString("AuthGrade");
  71. String position = "No position found";
  72. String photo = rs.getString("photo");
  73.  
  74. Employee temp = new Employee(serial, name, surname, auth, position);
  75. temp.setPhoto(photo);
  76. list.add(temp);
  77.  
  78. }
  79. return list;
  80. }
  81. } catch (SQLException ex) {
  82. System.out.println(ex.getMessage());
  83.  
  84. } finally {
  85. try {
  86. if (conn != null)
  87. conn.close();
  88. if (stmt != null)
  89. stmt.close();
  90. } catch (SQLException e) {
  91. System.out.println("Error closing " + e.getMessage());
  92. }
  93. }
  94. return list;
  95. }
  96. public static ArrayList<Visitor> getAllVisitors() {
  97. ArrayList<Visitor> list = new ArrayList<Visitor>();
  98. Statement stmt = null;
  99. try {
  100. if (conn != null) {
  101. stmt = (Statement) conn.createStatement();
  102. stmt.executeQuery("SELECT e.*, l.Name, max(a.TimeS)\r\n" + "from employes e , locals l, accesses a \r\n"
  103. + "where e.Causal IS NOT NULL and a.IdEmployee=e.SerialNumber and l.Id=a.IdLocal\r\n"
  104. + "GROUP BY e.SerialNumber");
  105. ResultSet rs = stmt.getResultSet();
  106. while (rs.next()) {
  107. String serial = rs.getString("SerialNumber");
  108. String name = rs.getString("Name");
  109. String surname = rs.getString("Surname");
  110. String causal = rs.getString("Causal");
  111. String expiration = rs.getString("Expiration");
  112. String position = rs.getString("l.Name");
  113.  
  114. Visitor temp = new Visitor(name, surname, causal, expiration);
  115. temp.setPosition(position);
  116. temp.setId(serial);
  117. list.add(temp);
  118. }
  119. stmt.executeQuery("SELECT e.* from employes e\r\n"
  120. + "where e.Causal IS NOT NULL and e.SerialNumber not in(SELECT e.SerialNumber from employes e , locals l, accesses a \r\n"
  121. + " where a.IdEmployee=e.SerialNumber and l.Id=a.IdLocal GROUP BY e.SerialNumber)\r\n"
  122. + "");
  123. rs = stmt.getResultSet();
  124. while (rs.next()) {
  125. String serial = rs.getString("SerialNumber");
  126. String name = rs.getString("Name");
  127. String surname = rs.getString("Surname");
  128. String causal = rs.getString("Causal");
  129. String expiration = rs.getString("Expiration");
  130. String position = "No position found";
  131. Visitor temp = new Visitor(name, surname, causal, expiration);
  132. temp.setPosition(position);
  133. temp.setId(serial);
  134. list.add(temp);
  135.  
  136. }
  137. }
  138. } catch (SQLException ex) {
  139. System.out.println("Error: access problem while loading!");
  140. } finally {
  141. try {
  142. if (conn != null)
  143. conn.close();
  144. if (stmt != null)
  145. stmt.close();
  146. } catch (SQLException e) {
  147. System.out.println("Error closing " + e.getMessage());
  148. }
  149. }
  150. return list;
  151. }
  152.  
  153. public static Employee getEmployee(String id) throws SQLException {
  154. Employee temp = null;
  155. Statement stmt = null;
  156. try {
  157. if (conn != null) {
  158. stmt = (Statement) conn.createStatement();
  159. stmt.executeQuery("SELECT e.*, l.Name, a.TimeS , photo from employes e , locals l, accesses a, photos p \r\n"
  160. + "where p.IdEmployee= e.SerialNumber and a.TimeS= (select max(TimeS) from accesses a where a.IdEmployee='" + id + "') \r\n"
  161. + "and a.IdEmployee=e.SerialNumber and a.Result='true' and l.Id=a.IdLocal\r\n" + "");
  162. ResultSet rs = stmt.getResultSet();
  163. while (rs.next()) {
  164. String serial = rs.getString("SerialNumber");
  165. String name = rs.getString("Name");
  166. String surname = rs.getString("Surname");
  167. String auth = rs.getString("AuthGrade");
  168. String position = rs.getString("l.Name");
  169. String photo = rs.getString("photo");
  170.  
  171. temp = new Employee(serial, name, surname, auth, position);
  172. temp.setPhoto(photo);
  173.  
  174. }
  175. if (temp != null)
  176. return temp;
  177. else {
  178. stmt.executeQuery("SELECT e.* , photo from employes e, photos p where p.IdEmployee= e.SerialNumber and e.SerialNumber='" + id
  179. + "' and e.SerialNumber not in(SELECT a.IdEmployee from accesses a) ");
  180. rs = stmt.getResultSet();
  181. while (rs.next()) {
  182. String serial = rs.getString("SerialNumber");
  183. String name = rs.getString("Name");
  184. String surname = rs.getString("Surname");
  185. String auth = rs.getString("AuthGrade");
  186. String position = "position not found";
  187. String photo = rs.getString("photo");
  188.  
  189. temp = new Employee(serial, name, surname, auth, position);
  190. temp.setCurrentPosition(position);
  191. temp.setPhoto(photo);
  192.  
  193. temp.setSerial(serial);
  194.  
  195. }
  196. }
  197. }
  198. } catch (SQLException ex) {
  199. System.out.println(ex.getMessage());
  200. } finally {
  201. try {
  202. if (conn != null)
  203. conn.close();
  204. if (stmt != null)
  205. stmt.close();
  206. } catch (SQLException e) {
  207. System.out.println("Error closing " + e.getMessage());
  208. }
  209. }
  210. return temp;
  211. }
  212.  
  213. public static boolean isAuth(String local, String code) {
  214. Integer serial = null;
  215. Statement stmt = null;
  216. int authGrade = 0, requestedGrade = 0;
  217. try {
  218. if (conn != null) {
  219. stmt = (Statement) conn.createStatement();
  220. stmt.executeQuery("select SerialNumber, e.AuthGrade , l.AuthGrade "
  221. + "from employes e , auth a , locals l" + " where a.Code ='" + code
  222. + "' and a.IdEmployee= e.SerialNumber and l.Id='" + local + "'");
  223. ResultSet rs = stmt.getResultSet();
  224. while (rs.next()) {
  225. serial = rs.getInt("SerialNumber");
  226. authGrade = rs.getInt("e.Authgrade");
  227. requestedGrade = rs.getInt("l.Authgrade");
  228. }
  229. if (serial == null)
  230. return false;
  231.  
  232. if (authGrade < requestedGrade) {
  233. stmt.executeUpdate("INSERT INTO accesses (IdEmployee,IdLocal,Result) VALUES ('" + serial + "','"
  234. + local + "', 'false')");
  235.  
  236. return false;
  237. } else {
  238. stmt.executeUpdate("INSERT INTO accesses (IdEmployee,IdLocal,Result) VALUES ('" + serial + "','"
  239. + local + "', 'true')");
  240. return true;
  241. }
  242. }
  243. } catch (SQLException ex) {
  244. System.out.println(ex.getMessage());
  245. } finally {
  246. try {
  247. if (conn != null)
  248. conn.close();
  249. if (stmt != null)
  250. stmt.close();
  251. } catch (SQLException e) {
  252. System.out.println("Error closing " + e.getMessage());
  253. }
  254. }
  255. return false;
  256. }
  257.  
  258. public static boolean deleteEmployee(String id) {
  259. Statement stmt = null;
  260. try {
  261. if (conn != null) {
  262. stmt = (Statement) conn.createStatement();
  263. stmt.executeUpdate("DELETE FROM employes WHERE SerialNumber='" + id + "'");
  264. return true;
  265. }
  266.  
  267. } catch (SQLException ex) {
  268. System.out.println(ex.getMessage());
  269. return false;
  270. } finally {
  271. try {
  272. if (conn != null)
  273. conn.close();
  274. if (stmt != null)
  275. stmt.close();
  276. } catch (SQLException e) {
  277. System.out.println("Error closing " + e.getMessage());
  278. }
  279. }
  280. return false;
  281.  
  282. }
  283.  
  284. public static String newCode(String id) {
  285. String image = null;
  286. Statement stmt = null;
  287. try {
  288. if (conn != null) {
  289. stmt = (Statement) conn.createStatement();
  290. ResultSet rs;
  291. String newCode;
  292. do {
  293. newCode = Utils.randomCodeGen();
  294. stmt.executeQuery("SELECT COUNT(*) as total FROM auth WHERE Code='" + newCode + "'");
  295. rs = stmt.getResultSet();
  296. rs.first();
  297. } while (rs.getInt("total") == 1);
  298. stmt.executeQuery("SELECT COUNT(*) as total FROM auth WHERE IdEmployee='" + id + "'");
  299. rs = stmt.getResultSet();
  300. rs.first();
  301.  
  302. if (rs.getInt("total") == 0) {
  303. stmt.executeUpdate("INSERT INTO auth (IdEmployee, Code) VALUES('" + id + "','" + newCode + "')");
  304. } else {
  305. stmt.executeUpdate("UPDATE auth SET Code='" + newCode + "' WHERE IdEmployee='" + id + "'");
  306. }
  307. image = Utils.writeQRCode(newCode, id);
  308. }
  309.  
  310. } catch (SQLException ex) {
  311. System.out.println(ex.getMessage());
  312. } finally {
  313. try {
  314. if (conn != null)
  315. conn.close();
  316. if (stmt != null)
  317. stmt.close();
  318. } catch (SQLException e) {
  319. System.out.println("Error closing " + e.getMessage());
  320. }
  321. }
  322.  
  323. return image;
  324.  
  325. }
  326.  
  327. public static ArrayList<Local> getAllLocals() {
  328. Statement stmt = null;
  329. ArrayList<Local> locals = new ArrayList<Local>();
  330. try {
  331. if (conn != null) {
  332. stmt = (Statement) conn.createStatement();
  333. stmt.executeQuery("SELECT * from locals");
  334. ResultSet rs = stmt.getResultSet();
  335. while (rs.next()) {
  336. String id = rs.getString("Id");
  337. String authGrade = rs.getString("AuthGrade");
  338. String name = rs.getString("Name");
  339. locals.add(new Local(id, name, authGrade));
  340. }
  341. }
  342. } catch (SQLException ex) {
  343. System.out.println("Error: access problem while loading!");
  344. System.out.println("ECCEZIONE " + ex.getMessage());
  345. return null;
  346. } finally {
  347. try {
  348. if (conn != null)
  349. conn.close();
  350. if (stmt != null)
  351. stmt.close();
  352. } catch (SQLException e) {
  353. System.out.println("Error closing " + e.getMessage());
  354. }
  355. }
  356. return locals;
  357. }
  358.  
  359. public static String createVisitor(Visitor visitor) {
  360. Integer visitorId = null;
  361. Statement stmt = null;
  362. try {
  363.  
  364. if (conn != null) {
  365. stmt = (Statement) conn.createStatement();
  366. stmt.executeUpdate("INSERT into employes (Name, Surname, Causal, expiration) values" + " ('"
  367. + visitor.getName() + "', '" + visitor.getSurname() + "', '" + visitor.getCausal() + "', '"
  368. + visitor.getExpiration() + "')", Statement.RETURN_GENERATED_KEYS);
  369.  
  370. try (ResultSet generatedKeys = stmt.getGeneratedKeys()) {
  371. if (generatedKeys.next()) {
  372. visitorId = generatedKeys.getInt(1);
  373.  
  374. ResultSet rs;
  375. String code;
  376. do {
  377. code = Utils.randomCodeGen();
  378. stmt.executeQuery("SELECT COUNT(*) as total FROM auth WHERE Code='" + code + "'");
  379. rs = stmt.getResultSet();
  380. rs.first();
  381. } while (rs.getInt("total") == 1);
  382.  
  383. stmt.executeUpdate("INSERT into auth (IdEmployee, Code) values" + " ('" + visitorId + "', '"
  384. + code + "')");
  385.  
  386. return Utils.writeQRCode(code, String.valueOf(visitorId));
  387. }
  388. }
  389.  
  390. }
  391. } catch (SQLException ex) {
  392. System.out.println(ex.getMessage());
  393. } finally {
  394. try {
  395. if (conn != null)
  396. conn.close();
  397. if (stmt != null)
  398. stmt.close();
  399. } catch (SQLException e) {
  400. System.out.println("Error closing " + e.getMessage());
  401. }
  402. }
  403. return null;
  404. }
  405.  
  406. public static int createNewLocal(Local local) {
  407. Statement stmt = null;
  408. try {
  409. if (conn != null) {
  410. stmt = (Statement) conn.createStatement();
  411. stmt.executeUpdate("INSERT into locals (Id, AuthGrade, Name) values ('" + local.getIdLocal() + "', '"
  412. + local.getAuthGrade() + "', '" + local.getName() + "' )");
  413. }
  414. } catch (SQLIntegrityConstraintViolationException ex) {
  415. System.out.println("SQLException " + ex.getMessage());
  416. return -1;
  417. } catch (SQLException ex) {
  418. System.out.println("SQLException " + ex.getMessage());
  419. return -2;
  420. } finally {
  421. try {
  422.  
  423.  
  424. if (conn != null)
  425. conn.close();
  426. if (stmt != null)
  427. stmt.close();
  428. } catch (SQLException e) {
  429. System.out.println("Error closing " + e.getMessage());
  430. }
  431. }
  432. return 0;
  433. }
  434.  
  435. public static EmployeeResponseClass createEmployee(EmployeeRequestClass temp) {
  436. String employeeId = null;
  437. Statement stmt = null;
  438. String code = null;
  439. try {
  440.  
  441. if (conn != null) {
  442. stmt = (Statement) conn.createStatement();
  443. stmt.executeUpdate("INSERT into employes (Name, Surname, AuthGrade) values" + " ('"
  444. + temp.getEmployee().getName() + "', '" + temp.getEmployee().getSurname() + "', '"
  445. + temp.getEmployee().getAuthLevel() + "')", Statement.RETURN_GENERATED_KEYS);
  446.  
  447. try (ResultSet generatedKeys = stmt.getGeneratedKeys()) {
  448. if (generatedKeys.next()) {
  449. employeeId = generatedKeys.getString(1);
  450. temp.getEmployee().setPhoto(Utils.StoreEmployeePhoto(temp.getPhoto(), employeeId));
  451.  
  452. ResultSet rs;
  453. do {
  454. code = Utils.randomCodeGen();
  455. stmt.executeQuery("SELECT COUNT(*) as total FROM auth WHERE Code='" + code + "'");
  456. rs = stmt.getResultSet();
  457. rs.first();
  458. } while (rs.getInt("total") == 1);
  459.  
  460. stmt.executeUpdate("INSERT into auth (IdEmployee, Code) values" + " ('" + employeeId + "', '"
  461. + code + "')");
  462. stmt.executeUpdate("INSERT into photos (IdEmployee, Photo) values" + " ('" + employeeId + "', '"
  463. + temp.getEmployee().getPhoto() + "')");
  464. }
  465. }
  466. if (employeeId != null) {
  467. temp.getEmployee().setSerial(employeeId);
  468. } else
  469. return null;
  470.  
  471. }
  472. } catch (SQLException ex) {
  473. System.out.println(ex.getMessage());
  474. return null;
  475. } finally {
  476. try {
  477. if (conn != null)
  478. conn.close();
  479. if (stmt != null)
  480. stmt.close();
  481. } catch (SQLException e) {
  482. System.out.println("Error closing " + e.getMessage());
  483. }
  484. }
  485. return new EmployeeResponseClass(temp.getEmployee(), Utils.writeQRCode(code, employeeId));
  486. }
  487.  
  488. public static ArrayList<Access> makeQuery(ComplexQuery query) {
  489. Statement stmt = null;
  490. ResultSet results;
  491. ArrayList<Access> accessResults = new ArrayList<Access>();
  492. try {
  493. if (conn != null) {
  494. stmt = (Statement) conn.createStatement();
  495. stmt.executeQuery(query.toValidSQLQuery());
  496. results = stmt.getResultSet();
  497.  
  498. Access temp;
  499.  
  500. while (results.next()) {
  501.  
  502. temp = new Access();
  503. temp.setEmployeeId(results.getString("e.SerialNumber"));
  504. temp.setEmployeeName(results.getString("e.Name"));
  505. temp.setEmployeeSurname(results.getString("e.Surname"));
  506. temp.setLocalName(results.getString("l.Name"));
  507. temp.setTime(results.getTimestamp("a.TimeS"));
  508. temp.setResult(results.getBoolean("a.Result"));
  509. accessResults.add(temp);
  510.  
  511. }
  512. }
  513. } catch (SQLException ex) {
  514. System.out.println(ex.getMessage());
  515. return null;
  516. } finally {
  517. try {
  518. if (conn != null)
  519. conn.close();
  520. if (stmt != null)
  521. stmt.close();
  522. } catch (SQLException e) {
  523. System.out.println("Error closing " + e.getMessage());
  524. }
  525. }
  526. return accessResults;
  527. }
  528. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement