Advertisement
Guest User

Untitled

a guest
Dec 19th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 65.36 KB | None | 0 0
  1. package server;
  2.  
  3. import java.io.*;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Timestamp;
  9. import java.text.ParseException;
  10. import java.text.SimpleDateFormat;
  11.  
  12. import static java.lang.System.out;
  13. import java.rmi.*;
  14. import java.rmi.registry.LocateRegistry;
  15. import java.rmi.registry.Registry;
  16. import java.rmi.server.*;
  17. import java.util.ArrayList;
  18. import java.util.Date;
  19. import java.util.HashMap;
  20. import java.util.Scanner;
  21. import java.util.StringTokenizer;
  22. import java.sql.PreparedStatement;
  23.  
  24. public class DataServer extends UnicastRemoteObject implements RMI, java.io.Serializable {
  25. public static int rmiRegistry;
  26. public static int rmiPort;
  27. public static int rmiPortSec;
  28. public static String rmiHost;
  29. public static String rmiHostSec;
  30. public Scanner inputI = new Scanner(System.in);
  31. public Scanner inputS = new Scanner(System.in);
  32. public static ArrayList<Server_rmi> servers = new ArrayList<Server_rmi>();
  33. public static ArrayList<Admin_rmi_I> admins = new ArrayList<Admin_rmi_I>();
  34. public static Connection conn = null;
  35. public static PreparedStatement mysql;
  36. public static ResultSet resultSet;
  37. /**
  38. *
  39. */
  40. private static final long serialVersionUID = 1L;
  41.  
  42. public DataServer() throws RemoteException {
  43. super();
  44. }
  45.  
  46. public synchronized static void load_config() throws RemoteException {
  47. String file = "DataServerConfig.txt";
  48. String line;
  49. StringTokenizer tokenizer;
  50. System.out.println("Uploding DataServer configurations...");
  51. try {
  52. FileReader inputFile = new FileReader(file);
  53. BufferedReader buffer = new BufferedReader(inputFile);
  54. for (int i = 0; i < 5; i++) {
  55. line = buffer.readLine();
  56. tokenizer = new StringTokenizer(line, "=");
  57. line = tokenizer.nextToken();
  58. if (line.equals("RMI Registry")) {
  59. rmiRegistry = Integer.parseInt(tokenizer.nextToken());
  60. } else if (line.equals("RMI UDP Port connection")) {
  61. rmiPort = Integer.parseInt(tokenizer.nextToken());
  62. } else if (line.equals("RMI UDP Host connection")) {
  63. rmiHost = tokenizer.nextToken();
  64. } else if (line.equals("RMI UDP sec Port connection")) {
  65. rmiPortSec = Integer.parseInt(tokenizer.nextToken());
  66. } else if (line.equals("RMI UDP sec Host connection")) {
  67. rmiHostSec = tokenizer.nextToken();
  68. }
  69. }
  70. buffer.close();
  71. } catch (FileNotFoundException e) {
  72. System.out.println("File " + file + " not found");
  73. System.exit(0);
  74. } catch (IOException e) {
  75. }
  76. System.out.println("DataServerConfig.txt successfully uploaded.");
  77. }
  78.  
  79. public synchronized void subscribe(Server_rmi mesa) {
  80. servers.add(mesa);
  81. }
  82.  
  83. public synchronized void subscribeAdmin(Admin_rmi_I admin) {
  84. admins.add(admin);
  85. }
  86.  
  87. public synchronized ArrayList<String> list_facultys() {
  88. ArrayList<String> faculdades = new ArrayList<String>();
  89. try {
  90. out.println("Searching Faculty...");
  91. mysql = conn.prepareStatement("SELECT nome FROM bd.faculdade;");
  92. ResultSet rQ = mysql.executeQuery();
  93.  
  94. while (rQ.next()) {
  95. faculdades.add(rQ.getString("nome"));
  96. }
  97. } catch (SQLException e) {
  98. out.println("Something's wrong!!!");
  99. return faculdades;
  100. }
  101. return faculdades;
  102.  
  103. }
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119. public ArrayList<String> getAllUsers() {
  120. HashMap<String, String> users;
  121. System.out.println("Looking up all users...");
  122. users = new HashMap<String, String>();
  123. users.put("bender", "rodriguez"); // static users and passwords, to simplify the example
  124. users.put("fry", "philip");
  125. users.put("leela", "turanga");
  126. users.put("homer", "simpson");
  127. return new ArrayList<String>(users.keySet());
  128. }
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152. public synchronized ArrayList<Eleicoes> listElectionToDelete(Timestamp data){
  153. ArrayList<Eleicoes> eleicoes = new ArrayList<Eleicoes>();
  154. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  155. try {
  156. out.println("Getting elections...");
  157. mysql = conn.prepareStatement("select * from eleicao e where data_inicio >= ? and e.ideleicao NOT IN (select eleitor_cc from eleitor_eleicao where e.ideleicao=eleitor_cc);");
  158. mysql.setTimestamp(1,data);
  159. ResultSet rQ = mysql.executeQuery();
  160.  
  161. while(rQ.next()) {
  162. eleicoes.add(new Eleicoes(rQ.getInt("ideleicao"),rQ.getString("tipo"),sdf.parse(rQ.getString("data_inicio")),sdf.parse(rQ.getString("data_final")),rQ.getString("titulo"),rQ.getString("resumo")));
  163. }
  164.  
  165. }catch(SQLException e) {
  166. out.println("Something's wrong!!!");
  167. } catch (ParseException e) {
  168. e.printStackTrace();
  169. }
  170. return eleicoes;
  171.  
  172. }
  173. public synchronized HashMap<Integer,HashMap<String,Timestamp>> auditoria() {
  174. HashMap<Integer,HashMap<String,Timestamp>> auditoria = new HashMap<Integer,HashMap<String,Timestamp>>();
  175. String nome;
  176. try {
  177. out.println("Searching results...");
  178. mysql = conn.prepareStatement("SELECT eleitor_cc,mesa_de_voto_nome,moment FROM bd.eleitor_mesa_insta;");
  179. ResultSet rQ = mysql.executeQuery();
  180.  
  181. while (rQ.next()) {
  182. HashMap<String,Timestamp> temp = new HashMap<String,Timestamp>();
  183. temp.put(rQ.getString("mesa_de_voto_nome"), rQ.getTimestamp("moment"));
  184. auditoria.put(rQ.getInt("eleitor_cc"), temp);
  185. }
  186.  
  187. } catch (SQLException e) {
  188. e.printStackTrace();
  189. out.println("Something's wrong!!!");
  190.  
  191. }
  192. return auditoria;
  193. }
  194.  
  195. public synchronized ArrayList<Eleicoes> listElectionsEnded(Timestamp data){
  196. ArrayList<Eleicoes> eleicoes = new ArrayList<Eleicoes>();
  197. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  198. try {
  199. out.println("Getting elections...");
  200. mysql = conn.prepareStatement("select * from eleicao where data_final <= ?;");
  201. mysql.setTimestamp(1,data);
  202. ResultSet rQ = mysql.executeQuery();
  203.  
  204. while(rQ.next()) {
  205. eleicoes.add(new Eleicoes(rQ.getInt("ideleicao"),rQ.getString("tipo"),sdf.parse(rQ.getString("data_inicio")),sdf.parse(rQ.getString("data_final")),rQ.getString("titulo"),rQ.getString("resumo")));
  206. }
  207.  
  208. }catch(SQLException e) {
  209. out.println("Something's wrong!!!");
  210. } catch (ParseException e) {
  211. e.printStackTrace();
  212. }
  213. return eleicoes;
  214.  
  215. }
  216.  
  217.  
  218.  
  219. public synchronized ArrayList<Eleicoes> listElectionsLive(Timestamp data){
  220. ArrayList<Eleicoes> eleicoes = new ArrayList<Eleicoes>();
  221. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  222. try {
  223. out.println("Getting elections...");
  224. mysql = conn.prepareStatement("select * from eleicao where data_final > ?;");
  225. mysql.setTimestamp(1,data);
  226. ResultSet rQ = mysql.executeQuery();
  227.  
  228. while(rQ.next()) {
  229. eleicoes.add(new Eleicoes(rQ.getInt("ideleicao"),rQ.getString("tipo"),sdf.parse(rQ.getString("data_inicio")),sdf.parse(rQ.getString("data_final")),rQ.getString("titulo"),rQ.getString("resumo")));
  230. }
  231.  
  232. }catch(SQLException e) {
  233. out.println("Something's wrong!!!");
  234. } catch (ParseException e) {
  235. e.printStackTrace();
  236. }
  237. return eleicoes;
  238.  
  239. }
  240.  
  241. public synchronized ArrayList<String> mesas_eleicao(int id_eleicao) {
  242. ArrayList<String> mesas_eleicao = new ArrayList<String>();
  243. try {
  244. out.println("Searching mesas elections...");
  245. mysql = conn
  246. .prepareStatement("SELECT mesa_de_voto_nome FROM bd.votosbna where eleicao_ideleicao=?;");
  247. mysql.setInt(1, id_eleicao);
  248. ResultSet rQ = mysql.executeQuery();
  249.  
  250. while (rQ.next()) {
  251. mesas_eleicao.add(rQ.getString("mesa_de_voto_nome"));
  252. }
  253. } catch (SQLException e) {
  254. out.println("Something's wrong!!!");
  255. return mesas_eleicao;
  256. }
  257. return mesas_eleicao;
  258.  
  259. }
  260.  
  261. public synchronized String get_type_eleicao_by_id(int id_eleicao) {
  262. String tipo = "Not found";
  263. try {
  264. out.println("Searching mesas elections...");
  265. mysql = conn.prepareStatement("SELECT tipo FROM bd.eleicao where ideleicao=?;");
  266. mysql.setInt(1, id_eleicao);
  267. ResultSet rQ = mysql.executeQuery();
  268.  
  269. while (rQ.next()) {
  270. tipo = rQ.getString("tipo");
  271.  
  272. }
  273. } catch (SQLException e) {
  274. out.println("Something's wrong!!!");
  275. return tipo;
  276. }
  277. return tipo;
  278.  
  279. }
  280.  
  281. public synchronized String get_location_election(int id_eleicao) {
  282. String location = "None";
  283. String tipo = get_type_eleicao_by_id(id_eleicao);
  284. if (tipo.equals("department") || tipo.equals("nucleo")) {
  285. try {
  286. out.println("Searching locatison election...");
  287. mysql = conn.prepareStatement(
  288. "SELECT departamento_nome FROM bd.eleicao_departamento where ideleicao1=?;");
  289. mysql.setInt(1, id_eleicao);
  290. ResultSet rQ = mysql.executeQuery();
  291.  
  292. while (rQ.next()) {
  293. location = rQ.getString("departamento_nome");
  294.  
  295. }
  296. } catch (SQLException e) {
  297. out.println("Something's wrong!!!");
  298. }
  299. } else if (tipo.equals("faculty")) {
  300. try {
  301. out.println("Searching location election...");
  302. mysql = conn
  303. .prepareStatement("SELECT faculdade_nome FROM bd.eleicao_faculdade where ideleicao1=?;");
  304. mysql.setInt(1, id_eleicao);
  305. ResultSet rQ = mysql.executeQuery();
  306.  
  307. while (rQ.next()) {
  308. location = rQ.getString("faculdade_nome");
  309.  
  310. }
  311. } catch (SQLException e) {
  312. out.println("Something's wrong!!!");
  313. }
  314. }
  315. return location;
  316.  
  317. }
  318.  
  319. public synchronized String get_department_eleitor(int cc) {
  320. String location = "None";
  321. try {
  322. out.println("Searching location department of elector...");
  323. mysql = conn.prepareStatement(
  324. "SELECT departamento_nome FROM bd.eleitor_departamento where eleitor_cc=?;");
  325. mysql.setInt(1, cc);
  326. ResultSet rQ = mysql.executeQuery();
  327.  
  328. while (rQ.next()) {
  329. location = rQ.getString("departamento_nome");
  330.  
  331. }
  332. } catch (SQLException e) {
  333. out.println("Something's wrong!!!");
  334. }
  335. return location;
  336. }
  337.  
  338. public synchronized String get_faculdade_eleitor(int cc) {
  339. String location = "None";
  340. try {
  341. out.println("Searching location faculdade of elector...");
  342. mysql = conn.prepareStatement("SELECT faculdade_nome FROM bd.eleitor_faculdade where eleitor_cc=?;");
  343. mysql.setInt(1, cc);
  344. ResultSet rQ = mysql.executeQuery();
  345.  
  346. while (rQ.next()) {
  347. location = rQ.getString("faculdade_nome");
  348.  
  349. }
  350. } catch (SQLException e) {
  351. out.println("Something's wrong!!!");
  352. }
  353. return location;
  354. }
  355.  
  356. public synchronized ArrayList<Lista_candidata> print_lists_for_person(int cc, String mesaVoto) {
  357. Pessoa pessoa_escolhida = null;
  358. for (Pessoa x : get_pessoas()) {
  359. if (cc == x.numero_cc) {
  360. pessoa_escolhida = x;
  361. break;
  362. }
  363. }
  364. int id = get_election_of_table(mesaVoto);
  365. String tipo = get_type_eleicao_by_id(id);
  366. String location = "None";
  367. ArrayList<Integer> ids_usados = new ArrayList<Integer>();
  368. try {
  369. mysql = conn
  370. .prepareStatement("SELECT eleicao_ideleicao FROM bd.eleitor_eleicao where eleitor_cc=?;");
  371. mysql.setInt(1, cc);
  372. ResultSet rQ = mysql.executeQuery();
  373.  
  374. while (rQ.next()) {
  375. ids_usados.add(rQ.getInt("eleicao_ideleicao"));
  376.  
  377. }
  378. } catch (SQLException e) {
  379. out.println("Something's wrong!!!");
  380. }
  381.  
  382. ArrayList<Lista_candidata> lists = get_lists_election(id);
  383. ArrayList<Lista_candidata> lists_to_return = new ArrayList<Lista_candidata>();
  384. if (!ids_usados.contains(id)) {
  385. if (tipo.equals("geral")) {
  386. for (Lista_candidata x : lists) {
  387. if (x.classe.equals(pessoa_escolhida.cargo)) {
  388. lists_to_return.add(x);
  389. }
  390. }
  391. } else if (tipo.equals("faculty")) {
  392. location = get_location_election(id);
  393. if (pessoa_escolhida.cargo.equals("teacher")) {
  394. if (get_faculdade_eleitor(pessoa_escolhida.numero_cc).equals(location)) {
  395. for (Lista_candidata x : lists) {
  396. if (x.classe.equals("teacher")) {
  397. lists_to_return.add(x);
  398. }
  399. }
  400. }
  401. }
  402. } else if (tipo.equals("department")) {
  403. location = get_location_election(id);
  404. if (pessoa_escolhida.cargo.equals("teacher")) {
  405. if (get_department_eleitor(pessoa_escolhida.numero_cc).equals(location)) {
  406. for (Lista_candidata x : lists) {
  407. if (x.classe.equals("teacher")) {
  408. lists_to_return.add(x);
  409. }
  410. }
  411. }
  412. }
  413. } else if (tipo.equals("nucleo")) {
  414. location = get_location_election(id);
  415. if (pessoa_escolhida.cargo.equals("student")) {
  416. if (get_department_eleitor(pessoa_escolhida.numero_cc).equals(location)) {
  417. for (Lista_candidata x : lists) {
  418. if (x.classe.equals("student")) {
  419. lists_to_return.add(x);
  420. }
  421. }
  422. }
  423. }
  424.  
  425. }
  426. }
  427.  
  428. return lists_to_return;
  429. }
  430.  
  431. public synchronized Integer votos_null(int id_eleicao) {
  432. int null_votos = 0;
  433. try {
  434. out.println("calculating null votes...");
  435. mysql = conn.prepareStatement(
  436. "SELECT sum(num_votos_nulos) as votos_nulos FROM bd.votosbna where eleicao_ideleicao=?;");
  437. mysql.setInt(1, id_eleicao);
  438. ResultSet rQ = mysql.executeQuery();
  439.  
  440. while (rQ.next()) {
  441. null_votos = rQ.getInt("votos_nulos");
  442. }
  443. } catch (SQLException e) {
  444. out.println("Something's wrong!!!");
  445. return null_votos;
  446. }
  447. return null_votos;
  448.  
  449. }
  450.  
  451. public synchronized Integer votos_brancos(int id_eleicao) {
  452. int votos_blanc = 0;
  453. try {
  454. out.println("calculating null votes...");
  455. mysql = conn.prepareStatement(
  456. "SELECT sum(num_votos_branco) as votos_brancos FROM bd.votosbna where eleicao_ideleicao=?;");
  457. mysql.setInt(1, id_eleicao);
  458. ResultSet rQ = mysql.executeQuery();
  459.  
  460. while (rQ.next()) {
  461. votos_blanc = rQ.getInt("votos_brancos");
  462. }
  463. } catch (SQLException e) {
  464. out.println("Something's wrong!!!");
  465. return votos_blanc;
  466. }
  467. return votos_blanc;
  468.  
  469. }
  470.  
  471. public synchronized ArrayList<String> dep_fac(String fac) {
  472. ArrayList<String> dep_fac = new ArrayList<String>();
  473. try {
  474. out.println("Departments of faculty....");
  475. mysql = conn.prepareStatement(
  476. "SELECT departamento_nome from bd.departamento_faculdade where faculdade_nome=?;");
  477. mysql.setString(1, fac);
  478. ResultSet rQ = mysql.executeQuery();
  479.  
  480. while (rQ.next()) {
  481. dep_fac.add(rQ.getString("departamento_nome"));
  482. }
  483. } catch (SQLException e) {
  484. out.println("Something's wrong!!!");
  485. return dep_fac;
  486. }
  487. return dep_fac;
  488. }
  489.  
  490. public synchronized HashMap<Integer, String> get_pessoas_mesas() {
  491. HashMap<Integer, String> cc_mesa = new HashMap<Integer, String>();
  492. try {
  493. out.println("Getting people on tables....");
  494. mysql = conn.prepareStatement("SELECT eleitor_cc,mesa_de_voto_nome from bd.eleitor_mesa_de_voto;");
  495. ResultSet rQ = mysql.executeQuery();
  496. while (rQ.next()) {
  497. cc_mesa.put(rQ.getInt("eleitor_cc"), rQ.getString("mesa_de_voto_nome"));
  498. }
  499. } catch (SQLException e) {
  500. out.println("Something's wrong!!!");
  501. return cc_mesa;
  502. }
  503. return cc_mesa;
  504. }
  505.  
  506. public synchronized ArrayList<String> listDepartments() {
  507. ArrayList<String> departments = new ArrayList<String>();
  508. try {
  509. out.println("Getting Departments...");
  510. mysql = conn.prepareStatement("select * from departamento;");
  511. ResultSet rQ = mysql.executeQuery();
  512.  
  513. while (rQ.next()) {
  514. departments.add(rQ.getString("nome"));
  515. }
  516.  
  517. } catch (SQLException e) {
  518. out.println("Something's wrong!!!");
  519. }
  520. return departments;
  521.  
  522. }
  523.  
  524. public synchronized ArrayList<String> avaible_departments() {
  525. ArrayList<String> departments = new ArrayList<String>();
  526. try {
  527. out.println("Searching departments...");
  528. mysql = conn.prepareStatement(
  529. "SELECT nome from bd.departamento where not exists (select departamento_nome from bd.departamento_faculdade where nome = departamento_nome);");
  530. ResultSet rQ = mysql.executeQuery();
  531.  
  532. while (rQ.next()) {
  533. departments.add(rQ.getString("nome"));
  534. }
  535. } catch (SQLException e) {
  536. e.printStackTrace();
  537. out.println("Something's wrong!!!");
  538. return departments;
  539. }
  540. return departments;
  541.  
  542. }
  543.  
  544. public synchronized ArrayList<String> departments_for_elections() {
  545. ArrayList<String> departments = new ArrayList<String>();
  546. try {
  547. out.println("Searching departments...");
  548. mysql = conn.prepareStatement(
  549. "SELECT nome from bd.departamento where exists (select departamento_nome from bd.departamento_faculdade where nome = departamento_nome);");
  550. ResultSet rQ = mysql.executeQuery();
  551.  
  552. while (rQ.next()) {
  553. departments.add(rQ.getString("nome"));
  554. }
  555. } catch (SQLException e) {
  556. e.printStackTrace();
  557. out.println("Something's wrong!!!");
  558. return departments;
  559. }
  560. return departments;
  561.  
  562. }
  563.  
  564. public synchronized ArrayList<String> avaible_tables() {
  565. ArrayList<String> mesas = new ArrayList<String>();
  566. try {
  567. out.println("Searching avaible tables...");
  568. mysql = conn.prepareStatement(
  569. "select nome from bd.mesa_de_voto where nome NOT IN (select mesa_de_voto_nome from bd.votosbna);");
  570. ResultSet rQ = mysql.executeQuery();
  571.  
  572. while (rQ.next()) {
  573. mesas.add(rQ.getString("nome"));
  574. }
  575. } catch (SQLException e) {
  576. e.printStackTrace();
  577. out.println("Something's wrong!!!");
  578. return mesas;
  579. }
  580. return mesas;
  581.  
  582. }
  583.  
  584. public synchronized Integer get_election_of_table(String name_table) {
  585. int id_eleicao = 0;
  586. try {
  587. out.println("getting election of table...");
  588. mysql = conn
  589. .prepareStatement("select eleicao_ideleicao from bd.votosbna where mesa_de_voto_nome=?;");
  590. mysql.setString(1, name_table);
  591. ResultSet rQ = mysql.executeQuery();
  592.  
  593. while (rQ.next()) {
  594. id_eleicao = rQ.getInt("eleicao_ideleicao");
  595. }
  596. } catch (SQLException e) {
  597. e.printStackTrace();
  598. out.println("Something's wrong!!!");
  599. }
  600. return id_eleicao;
  601.  
  602. }
  603.  
  604. public synchronized Integer count_number_of_persom_table(String name_table) {
  605. int num_pessoas = 0;
  606. try {
  607. out.println("getting table number of people...");
  608. mysql = conn.prepareStatement(
  609. "select count(eleitor_cc) as number from bd.eleitor_mesa_de_voto where mesa_de_voto_nome=?;");
  610. mysql.setString(1, name_table);
  611. ResultSet rQ = mysql.executeQuery();
  612.  
  613. while (rQ.next()) {
  614. num_pessoas = rQ.getInt("number");
  615. return num_pessoas;
  616. }
  617. } catch (SQLException e) {
  618. e.printStackTrace();
  619. out.println("Something's wrong!!!");
  620. }
  621. return num_pessoas;
  622.  
  623. }
  624.  
  625. public synchronized ArrayList<Lista_candidata> get_lists_election(int id_election) {
  626. ArrayList<Lista_candidata> lists_election = new ArrayList<Lista_candidata>();
  627. try {
  628. out.println("Searching lists election...");
  629.  
  630. mysql = conn.prepareStatement(
  631. "select id,classe,nome,num_votos from candidato where id in ( select id_lista from tipo_eleitores_vota where ideleicao1=?);");
  632. mysql.setInt(1, id_election);
  633. ResultSet rQ = mysql.executeQuery();
  634.  
  635. while (rQ.next()) {
  636. Lista_candidata lista_add = new Lista_candidata(rQ.getInt("id"), rQ.getString("nome"),
  637. rQ.getString("classe"), rQ.getInt("num_votos"));
  638. lists_election.add(lista_add);
  639. }
  640. } catch (SQLException e) {
  641. e.printStackTrace();
  642. out.println("Something's wrong!!!");
  643. return lists_election;
  644. }
  645. return lists_election;
  646.  
  647. }
  648.  
  649. public synchronized void update_dep_fac(String department, String faculdade) {
  650. out.println("Adding Department...");
  651. try {
  652. mysql = conn.prepareStatement(
  653. "INSERT INTO bd.departamento_faculdade(departamento_nome,faculdade_nome) VALUES(?,?);");
  654. mysql.setString(1, department);
  655. mysql.setString(2, faculdade);
  656. mysql.executeUpdate();
  657. conn.commit();
  658.  
  659. } catch (SQLException e) {
  660. e.printStackTrace();
  661. out.println("Something's wrong!!!");
  662. }
  663. }
  664.  
  665. public synchronized void insert_eleicao_dep(int id, String departamento) {
  666. out.println("Adding Department of election...");
  667. try {
  668. mysql = conn.prepareStatement(
  669. "INSERT INTO bd.eleicao_departamento(ideleicao1,departamento_nome) VALUES(?,?);");
  670. mysql.setInt(1, id);
  671. mysql.setString(2, departamento);
  672. mysql.executeUpdate();
  673. conn.commit();
  674.  
  675. } catch (SQLException e) {
  676. e.printStackTrace();
  677. out.println("Something's wrong!!!");
  678. }
  679. }
  680.  
  681. public synchronized void insert_person_table(int cc, String mesa) {
  682. out.println("Adding person to table...");
  683. try {
  684. mysql = conn.prepareStatement(
  685. "INSERT INTO bd.eleitor_mesa_de_voto(eleitor_cc,mesa_de_voto_nome) VALUES(?,?);");
  686. mysql.setInt(1, cc);
  687. mysql.setString(2, mesa);
  688. mysql.executeUpdate();
  689. conn.commit();
  690.  
  691. } catch (SQLException e) {
  692. e.printStackTrace();
  693. out.println("Something's wrong!!!");
  694. }
  695. }
  696.  
  697. public synchronized void insert_fac_eleicao(int id, String faculdade) {
  698. out.println("Adding Faculty of election...");
  699. try {
  700. mysql = conn.prepareStatement(
  701. "INSERT INTO bd.eleicao_faculdade(ideleicao1,faculdade_nome) VALUES(?,?);");
  702. mysql.setInt(1, id);
  703. mysql.setString(2, faculdade);
  704. mysql.executeUpdate();
  705. conn.commit();
  706. } catch (SQLException e) {
  707. e.printStackTrace();
  708. out.println("Something's wrong!!!");
  709. }
  710. }
  711.  
  712. public synchronized void add_pessoa(int cc, String pessoa, String pass, int phone, Date expiration, String morada,
  713. String cargo) {
  714. out.println("Adding Person...");
  715. try {
  716. java.sql.Timestamp date = new java.sql.Timestamp(expiration.getTime());
  717. mysql = conn.prepareStatement(
  718. "INSERT INTO bd.eleitor(cc,nome,password,numtelefone,validadecc,morada,cargo) VALUES(?,?,?,?,?,?,?);");
  719. mysql.setInt(1, cc);
  720. mysql.setString(2, pessoa);
  721. mysql.setString(3, pass);
  722. mysql.setInt(4, phone);
  723. mysql.setTimestamp(5, date);
  724. mysql.setString(6, morada);
  725. mysql.setString(7, cargo);
  726. mysql.executeUpdate();
  727. conn.commit();
  728. } catch (SQLException e) {
  729. e.printStackTrace();
  730. out.println("Something's wrong!!!");
  731. }
  732. }
  733.  
  734. public synchronized void add_eleicao(int id, java.util.Date inicio, Date fim, String titulo, String resumo,
  735. String tipo) {
  736. out.println("Adding eleicao...");
  737. try {
  738. java.sql.Timestamp date = new java.sql.Timestamp(inicio.getTime());
  739. out.print(date);
  740. java.sql.Timestamp date2 = new java.sql.Timestamp(fim.getTime());
  741. mysql = conn.prepareStatement(
  742. "INSERT INTO bd.eleicao(ideleicao,data_inicio,data_final,titulo,resumo,tipo) VALUES(?,?,?,?,?,?);");
  743. mysql.setInt(1, id);
  744. mysql.setTimestamp(2, date);
  745. mysql.setTimestamp(3, date2);
  746. mysql.setString(4, titulo);
  747. mysql.setString(5, resumo);
  748. mysql.setString(6, tipo);
  749. mysql.executeUpdate();
  750. conn.commit();
  751. } catch (SQLException e) {
  752. e.printStackTrace();
  753. out.println("Something's wrong!!!");
  754. }
  755. }
  756.  
  757. public synchronized void delete_table_from_election(String nome_mesa) {
  758. out.println("Deleting table from votosbna...");
  759. try {
  760. mysql = conn.prepareStatement("DELETE from bd.votosbna where mesa_de_voto_nome=?;");
  761. mysql.setString(1, nome_mesa);
  762. mysql.executeUpdate();
  763. conn.commit();
  764. } catch (SQLException e) {
  765. e.printStackTrace();
  766. out.println("Something's wrong!!!");
  767. }
  768. out.println("Deleting table from votocandidatos...");
  769. try {
  770. mysql = conn.prepareStatement("DELETE from bd.votoscandidatos where nome_mesa=?;");
  771. mysql.setString(1, nome_mesa);
  772. mysql.executeUpdate();
  773. conn.commit();
  774. } catch (SQLException e) {
  775. e.printStackTrace();
  776. out.println("Something's wrong!!!");
  777. }
  778. out.println("Deleting table from faculty if exists...");
  779. try {
  780. mysql = conn.prepareStatement("DELETE from bd.mesa_de_voto_faculdade where mesa_de_voto_nome=?;");
  781. mysql.setString(1, nome_mesa);
  782. mysql.executeUpdate();
  783. conn.commit();
  784. } catch (SQLException e) {
  785. e.printStackTrace();
  786. out.println("Something's wrong!!!");
  787. }
  788. out.println("Deleting table from department if exists...");
  789. try {
  790. mysql = conn.prepareStatement("DELETE from bd.departamento_mesa_de_voto where mesa_de_voto_nome=?;");
  791. mysql.setString(1, nome_mesa);
  792. mysql.executeUpdate();
  793. conn.commit();
  794. } catch (SQLException e) {
  795. e.printStackTrace();
  796. out.println("Something's wrong!!!");
  797. }
  798. out.println("Deleting table from eleitor mesa de voto if exists...");
  799. try {
  800. mysql = conn.prepareStatement("DELETE from bd.eleitor_mesa_de_voto where mesa_de_voto_nome=?;");
  801. mysql.setString(1, nome_mesa);
  802. mysql.executeUpdate();
  803. conn.commit();
  804. } catch (SQLException e) {
  805. e.printStackTrace();
  806. out.println("Something's wrong!!!");
  807. }
  808. }
  809.  
  810. public synchronized void delete_table_from_department(String nome_mesa) {
  811. out.println("Deleting table from department...");
  812. try {
  813. mysql = conn.prepareStatement("DELETE from bd.departamento_mesa_de_voto where mesa_de_voto_nome=?;");
  814. mysql.setString(1, nome_mesa);
  815. mysql.executeUpdate();
  816. conn.commit();
  817. } catch (SQLException e) {
  818. e.printStackTrace();
  819. out.println("Something's wrong!!!");
  820. }
  821. }
  822.  
  823. public synchronized void delete_table_from_faculty(String nome_mesa) {
  824. out.println("Deleting table from faculty...");
  825. try {
  826. mysql = conn.prepareStatement("DELETE from bd.mesa_de_voto_faculdade where mesa_de_voto_nome=?;");
  827. mysql.setString(1, nome_mesa);
  828. mysql.executeUpdate();
  829. conn.commit();
  830. } catch (SQLException e) {
  831. e.printStackTrace();
  832. out.println("Something's wrong!!!");
  833. }
  834. }
  835.  
  836. public synchronized void delete_list(int id_lista) {
  837. out.println("Deleting list from tables...");
  838. try {
  839. mysql = conn.prepareStatement("DELETE from bd.votoscandidatos where id_lista=?;");
  840. mysql.setInt(1, id_lista);
  841. mysql.executeUpdate();
  842. conn.commit();
  843. } catch (SQLException e) {
  844. e.printStackTrace();
  845. out.println("Something's wrong!!!");
  846. }
  847. out.println("Deleting listagem to election...");
  848. try {
  849. mysql = conn.prepareStatement("DELETE from bd.tipo_eleitores_vota where id_lista=?;");
  850. mysql.setInt(1, id_lista);
  851. mysql.executeUpdate();
  852. conn.commit();
  853. } catch (SQLException e) {
  854. e.printStackTrace();
  855. out.println("Something's wrong!!!");
  856. }
  857. out.println("Deleting list from list of lists...");
  858. try {
  859. mysql = conn.prepareStatement("DELETE from bd.candidato where id=?;");
  860. mysql.setInt(1, id_lista);
  861. mysql.executeUpdate();
  862. conn.commit();
  863. } catch (SQLException e) {
  864. e.printStackTrace();
  865. out.println("Something's wrong!!!");
  866. }
  867. }
  868.  
  869. public synchronized void add_mesa(String name) {
  870. out.println("Adding Table...");
  871. try {
  872. mysql = conn.prepareStatement("INSERT INTO bd.mesa_de_voto(nome) VALUES(?);");
  873. mysql.setString(1, name);
  874. mysql.executeUpdate();
  875. conn.commit();
  876.  
  877. } catch (SQLException e) {
  878. e.printStackTrace();
  879. out.println("Something's wrong!!!");
  880. }
  881. }
  882.  
  883. public synchronized void insert_mesa_department(String name, String departamento) {
  884. out.println("Adding Table...");
  885. try {
  886. mysql = conn.prepareStatement(
  887. "INSERT INTO bd.departamento_mesa_de_voto(departamento_nome,mesa_de_voto_nome) VALUES(?,?);");
  888. mysql.setString(1, departamento);
  889. mysql.setString(2, name);
  890. mysql.executeUpdate();
  891. conn.commit();
  892.  
  893. } catch (SQLException e) {
  894. e.printStackTrace();
  895. out.println("Something's wrong!!!");
  896. }
  897. }
  898.  
  899. public synchronized void insert_mesa_faculdade(String name, String faculdade) {
  900. out.println("Adding Table...");
  901. try {
  902. mysql = conn.prepareStatement(
  903. "INSERT INTO bd.mesa_de_voto_faculdade(mesa_de_voto_nome,faculdade_nome) VALUES(?,?);");
  904. mysql.setString(1, name);
  905. mysql.setString(2, faculdade);
  906. mysql.executeUpdate();
  907. conn.commit();
  908.  
  909. } catch (SQLException e) {
  910. e.printStackTrace();
  911. out.println("Something's wrong!!!");
  912. }
  913. }
  914.  
  915. /*
  916. * public synchronized ArrayList<Integer> cc_numbers() {// o cc sao 8 nao 6
  917. * mudar este para o return pessoas? out.println("Retriving cc numbers...");
  918. * ArrayList<Integer> cc_numbers = new ArrayList<Integer>(); try { mysql =
  919. * conn.prepareStatement("SELECT cc from bd.eleitor;"); ResultSet rQ =
  920. * mysql.executeQuery();
  921. *
  922. * while (rQ.next()) { cc_numbers.add(rQ.getInt("cc")); }
  923. *
  924. * } catch (SQLException e) { e.printStackTrace();
  925. * out.println("Something's wrong!!!"); } return cc_numbers; }
  926. */
  927.  
  928. public synchronized void insert_listas(int id_eleicao, String nome_lista, String tipo, int id_list) {
  929. out.println("Adding list to election...");
  930. try {
  931. mysql = conn.prepareStatement("INSERT INTO bd.candidato(id,classe,nome,num_votos) VALUES(?,?,?,?);");
  932. mysql.setInt(1, id_list);
  933. mysql.setString(2, tipo);
  934. mysql.setString(3, nome_lista);
  935. mysql.setInt(4, 0);
  936. mysql.executeUpdate();
  937. conn.commit();
  938.  
  939. } catch (SQLException e) {
  940. e.printStackTrace();
  941. out.println("Something's wrong!!!");
  942. }
  943. try {
  944. mysql = conn
  945. .prepareStatement("INSERT INTO bd.tipo_eleitores_vota(id_lista,ideleicao1) VALUES(?,?);");
  946. mysql.setInt(1, id_list);
  947. mysql.setInt(2, id_eleicao);
  948. mysql.executeUpdate();
  949. conn.commit();
  950.  
  951. } catch (SQLException e) {
  952. e.printStackTrace();
  953. out.println("Something's wrong!!!");
  954. }
  955. }
  956.  
  957. public synchronized void insert_mesa_election(String nome_mesa, int id) {
  958. out.println("Adding mesa to election...");
  959. try {
  960. mysql = conn.prepareStatement(
  961. "INSERT INTO bd.votosbna(num_votos_branco,num_votos_nulos,mesa_de_voto_nome,eleicao_ideleicao) VALUES(?,?,?,?);");
  962. mysql.setInt(1, 0);
  963. mysql.setInt(2, 0);
  964. mysql.setString(3, nome_mesa);
  965. mysql.setInt(4, id);
  966. mysql.executeUpdate();
  967. conn.commit();
  968.  
  969. } catch (SQLException e) {
  970. e.printStackTrace();
  971. out.println("Something's wrong!!!");
  972. }
  973. }
  974.  
  975. public synchronized void insert_mesa_election_list_votes(String nome_mesa, int id_lista) {
  976. out.println("Adding mesa to election...");
  977. try {
  978. mysql = conn.prepareStatement(
  979. "INSERT INTO bd.votoscandidatos(nome_mesa,id_lista,num_votos) VALUES(?,?,?);");
  980. mysql.setString(1, nome_mesa);
  981. mysql.setInt(2, id_lista);
  982. mysql.setInt(3, 0);
  983. mysql.executeUpdate();
  984. conn.commit();
  985.  
  986. } catch (SQLException e) {
  987. e.printStackTrace();
  988. out.println("Something's wrong!!!");
  989. }
  990. }
  991.  
  992. public synchronized void insert_department(int cc, String department) {
  993. out.println("Adding Department to person...");
  994. try {
  995. mysql = conn.prepareStatement(
  996. "INSERT INTO bd.eleitor_departamento(eleitor_cc,departamento_nome) VALUES(?,?);");
  997. mysql.setInt(1, cc);
  998. mysql.setString(2, department);
  999. mysql.executeUpdate();
  1000. conn.commit();
  1001.  
  1002. } catch (SQLException e) {
  1003. e.printStackTrace();
  1004. out.println("Something's wrong!!!");
  1005. }
  1006. }
  1007.  
  1008. public synchronized void insert_faculty(int cc, String faculty) {
  1009. out.println("Adding Faculty to person...");
  1010. try {
  1011. mysql = conn.prepareStatement(
  1012. "INSERT INTO bd.eleitor_faculdade(eleitor_cc,faculdade_nome) VALUES(?,?);");
  1013. mysql.setInt(1, cc);
  1014. mysql.setString(2, faculty);
  1015. mysql.executeUpdate();
  1016. conn.commit();
  1017.  
  1018. } catch (SQLException e) {
  1019. e.printStackTrace();
  1020. out.println("Something's wrong!!!");
  1021. }
  1022. }
  1023.  
  1024. public synchronized ArrayList<Pessoa> get_pessoas() {
  1025. out.println("Retriving Users...");
  1026. ArrayList<Pessoa> pessoas = new ArrayList<Pessoa>();
  1027. try {
  1028. mysql = conn.prepareStatement(
  1029. "SELECT cc,nome,password,morada,cargo,numtelefone,validadecc from bd.eleitor;");
  1030. ResultSet rQ = mysql.executeQuery();
  1031.  
  1032. while (rQ.next()) {
  1033. Pessoa pessoa1 = new Pessoa(rQ.getString("nome"), rQ.getString("cargo"), rQ.getString("password"),
  1034. rQ.getString("morada"), rQ.getInt("numtelefone"), rQ.getInt("cc"), rQ.getDate("validadecc"));
  1035. pessoas.add(pessoa1);
  1036. }
  1037.  
  1038. } catch (SQLException e) {
  1039. e.printStackTrace();
  1040. out.println("Something's wrong!!!");
  1041. }
  1042. return pessoas;
  1043. }
  1044.  
  1045. public synchronized String location_table(String name_table) {
  1046. String location = "None";
  1047. out.println("Retriving location...");
  1048. try {
  1049. mysql = conn.prepareStatement(
  1050. "SELECT departamento_nome from bd.departamento_mesa_de_voto where mesa_de_voto_nome=?;");
  1051. mysql.setString(1, name_table);
  1052. ResultSet rQ = mysql.executeQuery();
  1053.  
  1054. while (rQ.next()) {
  1055. location = rQ.getString("departamento_nome");
  1056. }
  1057.  
  1058. } catch (SQLException e) {
  1059. e.printStackTrace();
  1060. out.println("Something's wrong!!!");
  1061. }
  1062. if (location.equals("None")) {
  1063. try {
  1064. mysql = conn.prepareStatement(
  1065. "SELECT faculdade_nome from bd.mesa_de_voto_faculdade where mesa_de_voto_nome=?;");
  1066. mysql.setString(1, name_table);
  1067. ResultSet rQ = mysql.executeQuery();
  1068.  
  1069. while (rQ.next()) {
  1070. location = rQ.getString("faculdade_nome");
  1071. }
  1072.  
  1073. } catch (SQLException e) {
  1074. e.printStackTrace();
  1075. out.println("Something's wrong!!!");
  1076. }
  1077. }
  1078. return location;
  1079. }
  1080.  
  1081. public synchronized ArrayList<Integer> id_listas() {
  1082. out.println("Retriving id lists...");
  1083. ArrayList<Integer> ids_listas = new ArrayList<Integer>();
  1084. try {
  1085. mysql = conn.prepareStatement("SELECT id from bd.candidato;");
  1086. ResultSet rQ = mysql.executeQuery();
  1087. while (rQ.next()) {
  1088. ids_listas.add(rQ.getInt("id"));
  1089. }
  1090.  
  1091. } catch (SQLException e) {
  1092. e.printStackTrace();
  1093. out.println("Something's wrong!!!");
  1094. }
  1095. return ids_listas;
  1096. }
  1097.  
  1098. public synchronized String update_person(Pessoa update_per) {
  1099. out.println("Updating person...");
  1100. java.sql.Timestamp date = new java.sql.Timestamp(update_per.validade_cc.getTime());
  1101. try {
  1102. mysql = conn.prepareStatement(
  1103. "UPDATE bd.eleitor SET password = ?, numtelefone=?,validadecc=?, morada=?, cargo=? WHERE cc=?;");
  1104. mysql.setString(1, update_per.password);
  1105. mysql.setInt(2, update_per.telefone);
  1106. mysql.setTimestamp(3, date);
  1107. mysql.setString(4, update_per.morada);
  1108. mysql.setString(5, update_per.cargo);
  1109. mysql.setInt(6, update_per.numero_cc);
  1110. mysql.executeUpdate();
  1111. conn.commit();
  1112. } catch (SQLException e) {
  1113. return "Person has been deleted by another admin!\n";
  1114. }
  1115. return "Person updated\n";
  1116. }
  1117.  
  1118. public synchronized String update_tab_location_fac(String mesa, String faculdade) {
  1119. out.println("Updating faculty of table...");
  1120. try {
  1121. mysql = conn.prepareStatement(
  1122. "UPDATE bd.mesa_de_voto_faculdade SET faculdade_nome = ? WHERE mesa_de_voto_nome=?;");
  1123. mysql.setString(1, faculdade);
  1124. mysql.setString(2, mesa);
  1125. mysql.executeUpdate();
  1126. conn.commit();
  1127. } catch (SQLException e) {
  1128. return "table has been deleted by another admin!\n";
  1129. }
  1130. return "table location updated\n";
  1131. }
  1132.  
  1133. public synchronized String update_tab_location_dep(String mesa, String departamento) {
  1134. out.println("Updating department of table...");
  1135. try {
  1136. mysql = conn.prepareStatement(
  1137. "UPDATE bd.departamento_mesa_de_voto SET departamento_nome = ? WHERE mesa_de_voto_nome=?;");
  1138. mysql.setString(1, departamento);
  1139. mysql.setString(2, mesa);
  1140. mysql.executeUpdate();
  1141. conn.commit();
  1142. } catch (SQLException e) {
  1143. return "table has been deleted by another admin!\n";
  1144. }
  1145. return "table location updated\n";
  1146. }
  1147.  
  1148. public synchronized String update_election(Eleicoes eleicao) {
  1149. out.println("Updating eleicao...");
  1150. try {
  1151. java.sql.Timestamp date = new java.sql.Timestamp(eleicao.start.getTime());
  1152. java.sql.Timestamp date2 = new java.sql.Timestamp(eleicao.end.getTime());
  1153. mysql = conn.prepareStatement(
  1154. "UPDATE bd.eleicao SET data_inicio=?,data_final=?, titulo=?, resumo=?,tipo=? WHERE ideleicao=?;");
  1155. mysql.setTimestamp(1, date);
  1156. mysql.setTimestamp(2, date2);
  1157. mysql.setString(3, eleicao.titulo);
  1158. mysql.setString(4, eleicao.resumo);
  1159. mysql.setString(5, eleicao.tipo);
  1160. mysql.setInt(6, eleicao.id);
  1161. mysql.executeUpdate();
  1162. conn.commit();
  1163. } catch (SQLException e) {
  1164. e.printStackTrace();
  1165. out.println("Impossible update since election has been deleted by another admin!!!");
  1166. }
  1167. return "Election updated\n";
  1168. }
  1169.  
  1170. public synchronized String update_department_eleicao(int id, String department) {
  1171. out.println("Updating department of election...");
  1172. try {
  1173. mysql = conn.prepareStatement(
  1174. "UPDATE bd.eleicao_departamento SET departamento_nome = ? WHERE ideleicao1=?;");
  1175. mysql.setString(1, department);
  1176. mysql.setInt(2, id);
  1177. mysql.executeUpdate();
  1178. conn.commit();
  1179. } catch (SQLException e) {
  1180. e.printStackTrace();
  1181. return "Person has been deleted by another admin!\n";
  1182. }
  1183. return "Person updated\n";
  1184. }
  1185.  
  1186. public synchronized String update_faculty_eleicao(int id, String faculty) {
  1187. out.println("Updating faculty of election...");
  1188. try {
  1189. mysql = conn
  1190. .prepareStatement("UPDATE bd.eleicao_faculdade SET faculdade_nome = ? WHERE ideleicao1=?;");
  1191. mysql.setString(1, faculty);
  1192. mysql.setInt(2, id);
  1193. mysql.executeUpdate();
  1194. conn.commit();
  1195. } catch (SQLException e) {
  1196. e.printStackTrace();
  1197. return "Person has been deleted by another admin!\n";
  1198. }
  1199. return "Person updated\n";
  1200. }
  1201.  
  1202. public synchronized String update_department(String departamento, int cc) {
  1203. out.println("Updating department of person...");
  1204. try {
  1205. mysql = conn.prepareStatement(
  1206. "UPDATE bd.eleitor_departamento SET departamento_nome = ? WHERE eleitor_cc=?;");
  1207. mysql.setString(1, departamento);
  1208. mysql.setInt(2, cc);
  1209. mysql.executeUpdate();
  1210. conn.commit();
  1211. } catch (SQLException e) {
  1212. e.printStackTrace();
  1213. return "Person has been deleted by another admin!\n";
  1214. }
  1215. return "Person updated\n";
  1216. }
  1217.  
  1218. public synchronized String del_dep_person(int cc) {
  1219. out.println("Deleting department of person...");
  1220. try {
  1221. mysql = conn.prepareStatement("DELETE from bd.eleitor_departamento WHERE eleitor_cc=?;");
  1222. mysql.setInt(1, cc);
  1223. mysql.executeUpdate();
  1224. conn.commit();
  1225. } catch (SQLException e) {
  1226. e.printStackTrace();
  1227. return "Person has been deleted by another admin!\n";
  1228. }
  1229. return "Person updated\n";
  1230. }
  1231.  
  1232. public synchronized void del_local_election_fac(int id_election) {
  1233. try {
  1234. mysql = conn.prepareStatement("DELETE from bd.eleicao_faculdade WHERE ideleicao1=?;");
  1235. mysql.setInt(1, id_election);
  1236. mysql.executeUpdate();
  1237. conn.commit();
  1238. } catch (SQLException e) {
  1239. e.printStackTrace();
  1240. }
  1241.  
  1242. }
  1243.  
  1244. public synchronized void del_local_election_dep(int id_election) {
  1245. out.println("Deleting local of election...");
  1246. try {
  1247. mysql = conn.prepareStatement("DELETE from bd.eleicao_departamento WHERE ideleicao1=?;");
  1248. mysql.setInt(1, id_election);
  1249. mysql.executeUpdate();
  1250. conn.commit();
  1251. } catch (SQLException e) {
  1252. e.printStackTrace();
  1253. }
  1254. }
  1255.  
  1256. public synchronized String get_location_election_depar(int id_election) {
  1257. out.println("Getting local of election...");
  1258. try {
  1259. mysql = conn.prepareStatement(
  1260. "Select from departamento_nome bd.eleicao_departamento WHERE ideleicao1=?;");
  1261. mysql.setInt(1, id_election);
  1262. mysql.executeUpdate();
  1263. conn.commit();
  1264. } catch (SQLException e) {
  1265. e.printStackTrace();
  1266. return "not found";
  1267. }
  1268. return "found";
  1269. }
  1270.  
  1271. public synchronized String get_location_election_fac(int id_election) {
  1272. out.println("Getting local of election...");
  1273. try {
  1274. mysql = conn.prepareStatement("Select faculdade_nome from bd.eleicao_faculdade WHERE ideleicao1=?;");
  1275. mysql.setInt(1, id_election);
  1276. mysql.executeUpdate();
  1277. conn.commit();
  1278. } catch (SQLException e) {
  1279. e.printStackTrace();
  1280. return "not found";
  1281. }
  1282. return "found";
  1283. }
  1284.  
  1285. public synchronized String del_person_dep(String departamento) {
  1286. out.println("Deleting department of person...");
  1287. try {
  1288. mysql = conn.prepareStatement("DELETE from bd.eleitor_departamento WHERE departamento_nome=?;");
  1289. mysql.setString(1, departamento);
  1290. mysql.executeUpdate();
  1291. conn.commit();
  1292. } catch (SQLException e) {
  1293. e.printStackTrace();
  1294. return "Person has been deleted by another admin!\n";
  1295. }
  1296. return "Person updated\n";
  1297. }
  1298.  
  1299. public synchronized ArrayList<String> return_available_mesas() {
  1300. ArrayList<String> mesas = new ArrayList<String>();
  1301. try {
  1302. out.println("Getting all available tables for election...");
  1303. mysql = conn.prepareStatement(
  1304. "select nome from bd.mesa_de_voto where nome NOT IN (select mesa_de_voto_nome from departamento_mesa_de_voto);");
  1305. ResultSet rQ = mysql.executeQuery();
  1306.  
  1307. while (rQ.next()) {
  1308. mesas.add(rQ.getString("nome"));
  1309. }
  1310.  
  1311. } catch (SQLException e) {
  1312. out.println("Something's wrong!!!");
  1313. }
  1314. return mesas;
  1315.  
  1316. }
  1317.  
  1318. public synchronized ArrayList<String> mesas_prontas_abrir() {
  1319. ArrayList<Integer> eleicoes = new ArrayList<Integer>();
  1320. Date date = new Date();
  1321. for (Eleicoes x : return_eleicoes()) {
  1322. if (x.start.before(date) && x.end.after(date)) {
  1323. eleicoes.add(x.id);
  1324. }
  1325. }
  1326. ArrayList<String> mesas = new ArrayList<String>();
  1327. try {
  1328. out.println("Getting all available tables for election...");
  1329. mysql = conn.prepareStatement("select mesa_de_voto_nome,eleicao_ideleicao from bd.votosbna;");
  1330. ResultSet rQ = mysql.executeQuery();
  1331.  
  1332. while (rQ.next()) {
  1333. if (eleicoes.contains(rQ.getInt("eleicao_ideleicao"))) {
  1334. if (count_number_of_persom_table(rQ.getString("mesa_de_voto_nome")) == 3) {
  1335. mesas.add(rQ.getString("mesa_de_voto_nome"));
  1336. }
  1337. }
  1338. }
  1339.  
  1340. } catch (SQLException e) {
  1341. out.println("Something's wrong!!!");
  1342. }
  1343. return mesas;
  1344.  
  1345. }
  1346.  
  1347. public synchronized boolean searchVoterForDelete(int cc) {
  1348. int idBD;
  1349. out.println("Checking if a voter as any activity...");
  1350. try {
  1351. mysql = conn.prepareStatement("SELECT eleitor_cc FROM bd.eleitor_eleicao WHERE eleitor_cc = ?;");
  1352. mysql.setInt(1, cc);
  1353. ResultSet rQ = mysql.executeQuery();
  1354.  
  1355. while (rQ.next()) {
  1356. idBD = rQ.getInt("eleitor_cc");
  1357. if (idBD == cc) {
  1358. out.println("Voter as already history...can't be deleted!");
  1359. return false;
  1360. }
  1361. }
  1362.  
  1363. } catch (SQLException e) {
  1364. out.println("Something's wrong!!!");
  1365. return false;
  1366. }
  1367. try {
  1368. mysql = conn
  1369. .prepareStatement("SELECT eleitor_cc FROM bd.eleitor_mesa_de_voto WHERE eleitor_cc = ?;");
  1370. mysql.setInt(1, cc);
  1371. ResultSet rQ = mysql.executeQuery();
  1372.  
  1373. while (rQ.next()) {
  1374. idBD = rQ.getInt("eleitor_cc");
  1375. if (idBD == cc) {
  1376. out.println("Voter is in a table at the moment...can't be deleted!");
  1377. return false;
  1378. }
  1379. }
  1380.  
  1381. } catch (SQLException e) {
  1382. out.println("Something's wrong!!!");
  1383. return false;
  1384. }
  1385. return true;
  1386. }
  1387.  
  1388. public synchronized boolean searchMesaVBNA(String name) {
  1389. String idBD;
  1390. try {
  1391. out.println("Checking if table is ok to delete...");
  1392. mysql = conn
  1393. .prepareStatement("SELECT mesa_de_voto_nome FROM bd.votosbna WHERE mesa_de_voto_nome = ?;");
  1394. mysql.setString(1, name);
  1395. ResultSet rQ = mysql.executeQuery();
  1396.  
  1397. while (rQ.next()) {
  1398. idBD = rQ.getString("mesa_de_voto_nome");
  1399. if (idBD.toUpperCase().equals(name.toUpperCase())) {
  1400. out.println("There was a match...can't delete!");
  1401. return false;
  1402. }
  1403. }
  1404.  
  1405. } catch (SQLException e) {
  1406. out.println("Something's wrong searching table!!!");
  1407. return false;
  1408. }
  1409. return true;
  1410. }
  1411.  
  1412. public synchronized boolean deleteTable(String name) {
  1413. try {
  1414. out.println("Deleting table...");
  1415. mysql = conn.prepareStatement("DELETE FROM mesa_de_voto WHERE nome=?;");
  1416. mysql.setString(1, name);
  1417. mysql.executeUpdate();
  1418. conn.commit();
  1419. out.println("Table deleted...");
  1420.  
  1421. } catch (SQLException e) {
  1422. out.println("Something's wrong trying to delete table!!!");
  1423. return false;
  1424. }
  1425. return true;
  1426. }
  1427.  
  1428. public synchronized boolean deleteVoter(int cc) {
  1429. out.println("Deleting voter...");
  1430. try {
  1431. mysql = conn.prepareStatement("DELETE FROM eleitor_departamento WHERE eleitor_cc=?;");
  1432. mysql.setInt(1, cc);
  1433. mysql.executeUpdate();
  1434. conn.commit();
  1435.  
  1436. } catch (SQLException e) {
  1437. out.println("Something's wrong deleting form eleitor_departamento!!!");
  1438. return false;
  1439. }
  1440. try {
  1441. mysql = conn.prepareStatement("DELETE FROM eleitor_faculdade WHERE eleitor_cc=?;");
  1442. mysql.setInt(1, cc);
  1443. mysql.executeUpdate();
  1444. conn.commit();
  1445.  
  1446. } catch (SQLException e) {
  1447. out.println("Something's wrong deleting from eleitor_faculdade!!!");
  1448. return false;
  1449. }
  1450. try {
  1451. mysql = conn.prepareStatement("DELETE FROM eleitor WHERE cc=?;");
  1452. mysql.setInt(1, cc);
  1453. mysql.executeUpdate();
  1454. conn.commit();
  1455.  
  1456. } catch (SQLException e) {
  1457. out.println("Something's wrong deleting from eleitor!!!");
  1458. return false;
  1459. }
  1460. out.println("Voter Deleted...");
  1461. return true;
  1462. }
  1463.  
  1464. public synchronized ArrayList<String> listTables() {
  1465. ArrayList<String> mesas = new ArrayList<String>();
  1466. try {
  1467. out.println("Getting Tables...");
  1468. mysql = conn.prepareStatement("select * from mesa_de_voto;");
  1469. ResultSet rQ = mysql.executeQuery();
  1470.  
  1471. while (rQ.next()) {
  1472. mesas.add(rQ.getString("nome"));
  1473. }
  1474.  
  1475. } catch (SQLException e) {
  1476. out.println("Something's wrong!!!");
  1477. }
  1478. return mesas;
  1479.  
  1480. }
  1481.  
  1482. public synchronized ArrayList<Eleicoes> return_eleicoes() {
  1483. ArrayList<Eleicoes> eleicoes = new ArrayList<Eleicoes>();
  1484. try {
  1485. out.println("Getting all elections...");
  1486. mysql = conn.prepareStatement(
  1487. "select ideleicao,data_inicio,data_final,titulo,resumo,tipo from bd.eleicao;");
  1488. ResultSet rQ = mysql.executeQuery();
  1489.  
  1490. while (rQ.next()) {
  1491. Eleicoes nova_eleicao = new Eleicoes(rQ.getInt("ideleicao"), rQ.getString("tipo"),
  1492. rQ.getTimestamp("data_inicio"), rQ.getTimestamp("data_final"), rQ.getString("titulo"),
  1493. rQ.getString("resumo"));
  1494. eleicoes.add(nova_eleicao);
  1495. }
  1496. } catch (SQLException e) {
  1497. out.println("Something's wrong!!!");
  1498. }
  1499. return eleicoes;
  1500. }
  1501.  
  1502. public synchronized String del_dep_fac(String departamento) {
  1503. out.println("Deleting department of faculty...");
  1504. try {
  1505. mysql = conn.prepareStatement("DELETE from bd.departamento_faculdade WHERE departamento_nome=?;");
  1506. mysql.setString(1, departamento);
  1507. mysql.executeUpdate();
  1508. conn.commit();
  1509. } catch (SQLException e) {
  1510. e.printStackTrace();
  1511. return "mudar estes prints!\n";
  1512. }
  1513. return "Person updated\n";
  1514. }
  1515.  
  1516. public synchronized String update_faculty(String faculty, int cc) {
  1517. out.println("Updating faculty of person...");
  1518. try {
  1519. mysql = conn
  1520. .prepareStatement("UPDATE bd.eleitor_faculdade SET faculdade_nome = ? WHERE eleitor_cc=?;");
  1521. mysql.setString(1, faculty);
  1522. mysql.setInt(2, cc);
  1523. mysql.executeUpdate();
  1524. conn.commit();
  1525. } catch (SQLException e) {
  1526. e.printStackTrace();
  1527. return "Person has been deleted by another admin!\n";
  1528. }
  1529. return "Person updated\n";
  1530. }
  1531.  
  1532. public synchronized String fac_pessoa(int cc) {
  1533. out.println("Retriving faculty of User...");
  1534. String faculty = "None";
  1535. try {
  1536. mysql = conn.prepareStatement("SELECT faculdade_nome from bd.eleitor_faculdade where eleitor_cc=?;");
  1537. mysql.setInt(1, cc);
  1538. ResultSet rQ = mysql.executeQuery();
  1539.  
  1540. while (rQ.next()) {
  1541. faculty = rQ.getString("faculdade_nome");
  1542. }
  1543.  
  1544. } catch (SQLException e) {
  1545. e.printStackTrace();
  1546. out.println("Something's wrong!!!");
  1547. }
  1548. return faculty;
  1549. }
  1550.  
  1551. public synchronized String dep_pessoa(int cc) {
  1552. out.println("Retriving department of user...");
  1553. String department = "None";
  1554. try {
  1555. mysql = conn.prepareStatement(
  1556. "SELECT departamento_nome from bd.eleitor_departamento where eleitor_cc=?;");
  1557. mysql.setInt(1, cc);
  1558. ResultSet rQ = mysql.executeQuery();
  1559.  
  1560. while (rQ.next()) {
  1561. department = rQ.getString("departamento_nome");
  1562. }
  1563.  
  1564. } catch (SQLException e) {
  1565. e.printStackTrace();
  1566. out.println("Something's wrong!!!");
  1567. }
  1568. return department;
  1569. }
  1570.  
  1571. public synchronized ArrayList<String> tables() {
  1572. out.println("Retriving existing tables...");
  1573. ArrayList<String> exit_tab = new ArrayList<String>();
  1574. try {
  1575. mysql = conn.prepareStatement("SELECT nome from bd.mesa_de_voto;");
  1576. ResultSet rQ = mysql.executeQuery();
  1577.  
  1578. while (rQ.next()) {
  1579. exit_tab.add(rQ.getString("nome"));
  1580. }
  1581.  
  1582. } catch (SQLException e) {
  1583. e.printStackTrace();
  1584. out.println("Something's wrong!!!");
  1585. }
  1586. return exit_tab;
  1587. }
  1588.  
  1589. public synchronized boolean search_faculty(String name) {
  1590. String nome;
  1591. try {
  1592. out.println("Searching Faculty...");
  1593. mysql = conn.prepareStatement("SELECT nome FROM bd.faculdade WHERE nome = ?;");
  1594. mysql.setString(1, name);
  1595. ResultSet rQ = mysql.executeQuery();
  1596.  
  1597. while (rQ.next()) {
  1598. nome = rQ.getString("nome");
  1599. if (nome.toUpperCase().equals(name.toUpperCase())) {
  1600. out.println("There was a match...sending back to the user!");
  1601. return false;
  1602. }
  1603. }
  1604.  
  1605. } catch (SQLException e) {
  1606. out.println("Something's wrong!!!");
  1607. return false;
  1608. }
  1609. return true;
  1610. }
  1611.  
  1612. public synchronized boolean search_department(String name) {
  1613. String nome;
  1614. try {
  1615. out.println("Searching Department...");
  1616. mysql = conn.prepareStatement("SELECT nome FROM bd.departamento WHERE nome = ?;");
  1617. mysql.setString(1, name);
  1618. ResultSet rQ = mysql.executeQuery();
  1619.  
  1620. while (rQ.next()) {
  1621. nome = rQ.getString("nome");
  1622. if (nome.toUpperCase().equals(name.toUpperCase())) {
  1623. out.println("There was a match...sending back to the user!");
  1624. return false;
  1625. }
  1626. }
  1627.  
  1628. } catch (SQLException e) {
  1629. out.println("Something's wrong!!!");
  1630. return false;
  1631. }
  1632. return true;
  1633. }
  1634.  
  1635. public synchronized void create_faculty(String name) {
  1636. out.println("Creating Faculty...");
  1637. try {
  1638. mysql = conn.prepareStatement("INSERT INTO bd.faculdade(nome) VALUES(?);");
  1639. mysql.setString(1, name);
  1640. mysql.executeUpdate();
  1641. conn.commit();
  1642.  
  1643. } catch (SQLException e) {
  1644. e.printStackTrace();
  1645. out.println("Something's wrong!!!");
  1646. }
  1647. }
  1648.  
  1649. public synchronized void add_election_fac(String faculty, int id) {
  1650. out.println("Adding election to fac...");
  1651. try {
  1652. mysql = conn.prepareStatement(
  1653. "INSERT INTO bd.eleicao_faculdade(ideleicao1,faculdade_nome) VALUES(?,?);");
  1654. mysql.setInt(1, id);
  1655. mysql.setString(2, faculty);
  1656. mysql.executeUpdate();
  1657. conn.commit();
  1658.  
  1659. } catch (SQLException e) {
  1660. e.printStackTrace();
  1661. out.println("Something's wrong!!!");
  1662. }
  1663. }
  1664.  
  1665. public synchronized void add_election_dep(String department, int id) {
  1666. out.println("Adding election to dep...");
  1667. try {
  1668. mysql = conn.prepareStatement(
  1669. "INSERT INTO bd.eleicao_departamento(ideleicao1,departamento_nome) VALUES(?,?);");
  1670. mysql.setInt(1, id);
  1671. mysql.setString(2, department);
  1672. mysql.executeUpdate();
  1673. conn.commit();
  1674.  
  1675. } catch (SQLException e) {
  1676. e.printStackTrace();
  1677. out.println("Something's wrong!!!");
  1678. }
  1679. }
  1680.  
  1681. public synchronized void delete_person_from_table(String nome_mesa, int cc) {
  1682. out.println("Deleting table from department...");
  1683. try {
  1684. mysql = conn.prepareStatement(
  1685. "DELETE from bd.eleitor_mesa_de_voto where mesa_de_voto_nome=? and eleitor_cc=?;");
  1686. mysql.setString(1, nome_mesa);
  1687. mysql.setInt(2, cc);
  1688. mysql.executeUpdate();
  1689. conn.commit();
  1690. } catch (SQLException e) {
  1691. e.printStackTrace();
  1692. out.println("Something's wrong!!!");
  1693. }
  1694. }
  1695.  
  1696. public synchronized void AddDepartments(String name) {
  1697. out.println("Creating Department...");
  1698. try {
  1699. mysql = conn.prepareStatement("INSERT INTO bd.departamento(nome) VALUES(?);");
  1700. mysql.setString(1, name);
  1701. mysql.executeUpdate();
  1702. conn.commit();
  1703.  
  1704. } catch (SQLException e) {
  1705. e.printStackTrace();
  1706. out.println("Something's wrong!!!");
  1707. }
  1708. }
  1709.  
  1710. /*
  1711. * public synchronized boolean search_election() { int idBD; try {
  1712. * out.println("Searching Election..."); mysql = conn.
  1713. * prepareStatement("SELECT ideleicao FROM bd.eleicao WHERE ideleicao = ?;"
  1714. * ); mysql.setInt(1, id); ResultSet rQ = mysql.executeQuery();
  1715. *
  1716. * while (rQ.next()) { idBD = rQ.getInt("ideleicao"); if (idBD == id) {
  1717. * out.println("There was a match...sending back to the user!"); return false; }
  1718. * }
  1719. *
  1720. * } catch (SQLException e) { out.println("Something's wrong!!!"); return false;
  1721. * } return true; }
  1722. */
  1723.  
  1724. // =========================================================
  1725. public static void main(String args[]) throws RemoteException {
  1726. load_config();
  1727. try {
  1728. conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bd", "root", "2014228262");
  1729.  
  1730. } catch (SQLException ex) {
  1731. // handle any errors
  1732. System.out.println("SQLException: " + ex.getMessage());
  1733. System.out.println("SQLState: " + ex.getSQLState());
  1734. System.out.println("VendorError: " + ex.getErrorCode());
  1735. }
  1736. try {
  1737. conn.setAutoCommit(false);
  1738. } catch (SQLException ex) {
  1739. // conn.rollback();
  1740. }
  1741. try {
  1742. Registry createRMIRegistry = LocateRegistry.createRegistry(rmiRegistry);
  1743. DataServer dataserver = new DataServer();
  1744. createRMIRegistry.rebind("rmi", dataserver);
  1745. System.out.println("Hello Server ready.");
  1746. } catch (RemoteException re) {
  1747. System.out.println("Exception in HelloImpl.main: " + re);
  1748. }
  1749. System.out.println("\n\nData Server is ready for business!!!");
  1750. }
  1751.  
  1752. public void notifyAdmins(String notify) {
  1753. for (Admin_rmi_I x : admins) {
  1754. try {
  1755. x.imprimeNotificacao(notify);
  1756. } catch (RemoteException e) {
  1757. e.printStackTrace();
  1758. }
  1759. }
  1760. }
  1761.  
  1762. public synchronized boolean getATerminal(int cc) {
  1763. for (Pessoa x : get_pessoas()) {
  1764. if (x.numero_cc == cc) {
  1765. return true;
  1766.  
  1767. }
  1768. }
  1769. return false;
  1770.  
  1771. }
  1772.  
  1773. public synchronized String register_vote(int cc, String list, String table) throws RemoteException {
  1774. ArrayList<Lista_candidata> lists = print_lists_for_person(cc, table);
  1775. int check = 0;
  1776. int id_lista = 0;
  1777. for (Lista_candidata x : lists) {
  1778. if (x.nome_lista.equals(list)) {
  1779. check = 1;
  1780. id_lista = x.id;
  1781. }
  1782. }
  1783. if (list.equals("blanc")) {
  1784. int votos_blanc = 0;
  1785. try {
  1786. mysql = conn
  1787. .prepareStatement("SELECT num_votos_branco from bd.votosbna WHERE mesa_de_voto_nome=?;");
  1788. mysql.setString(1, table);
  1789. ResultSet rQ = mysql.executeQuery();
  1790.  
  1791. while (rQ.next()) {
  1792. votos_blanc = rQ.getInt("num_votos_branco");
  1793. }
  1794.  
  1795. } catch (SQLException e) {
  1796. return "erro1?!\n";
  1797. }
  1798. try {
  1799. mysql = conn.prepareStatement(
  1800. "UPDATE bd.votosbna SET num_votos_branco = ? WHERE mesa_de_voto_nome=?;");
  1801. mysql.setInt(1, votos_blanc + 1);
  1802. mysql.setString(2, table);
  1803. mysql.executeUpdate();
  1804. conn.commit();
  1805. } catch (SQLException e) {
  1806. return "erro2!\n";
  1807. }
  1808. adicional(cc, table, id_lista);
  1809. return "blanc vote registed";
  1810. } else {
  1811. if (check == 1) {
  1812. int votos = 0;
  1813. try {
  1814. mysql = conn.prepareStatement("SELECT num_votos from bd.candidato WHERE id=?;");
  1815. mysql.setInt(1, id_lista);
  1816. ResultSet rQ = mysql.executeQuery();
  1817.  
  1818. while (rQ.next()) {
  1819. votos = rQ.getInt("num_votos");
  1820. }
  1821.  
  1822. } catch (SQLException e) {
  1823. return "erro69?!\n";
  1824. }
  1825. try {
  1826. mysql = conn.prepareStatement("UPDATE bd.candidato SET num_votos = ? WHERE id=?;");
  1827. mysql.setInt(1, votos + 1);
  1828. mysql.setInt(2, id_lista);
  1829. mysql.executeUpdate();
  1830. conn.commit();
  1831. } catch (SQLException e) {
  1832. return "erro2!\n";
  1833. }
  1834. int votos1 = 0;
  1835. try {
  1836. mysql = conn.prepareStatement("SELECT num_votos from bd.votoscandidatos WHERE id_lista=? and nome_mesa=?;");
  1837. mysql.setInt(1, id_lista);
  1838. mysql.setString(2, table);
  1839. ResultSet rQ = mysql.executeQuery();
  1840.  
  1841. while (rQ.next()) {
  1842. votos1 = rQ.getInt("num_votos");
  1843. }
  1844.  
  1845. } catch (SQLException e) {
  1846. return "erro44444?!\n";
  1847. }
  1848. try {
  1849. mysql = conn.prepareStatement(
  1850. "UPDATE bd.votoscandidatos SET num_votos = ? WHERE id_lista=? and nome_mesa=?;");
  1851. mysql.setInt(1, votos1 + 1);
  1852. mysql.setInt(2, id_lista);
  1853. mysql.setString(3, table);
  1854. mysql.executeUpdate();
  1855. conn.commit();
  1856. } catch (SQLException e) {
  1857. return "erro2!\n";
  1858. }
  1859. adicional(cc, table, id_lista);
  1860. return "vote registered";
  1861. } else {
  1862. int votos_null = 0;
  1863. try {
  1864. mysql = conn.prepareStatement(
  1865. "SELECT num_votos_nulos from bd.votosbna WHERE mesa_de_voto_nome=?;");
  1866. mysql.setString(1, table);
  1867. ResultSet rQ = mysql.executeQuery();
  1868.  
  1869. while (rQ.next()) {
  1870. votos_null = rQ.getInt("num_votos_nulos");
  1871. }
  1872.  
  1873. } catch (SQLException e) {
  1874. return "erro900909090?!\n";
  1875. }
  1876. try {
  1877. mysql = conn.prepareStatement(
  1878. "UPDATE bd.votosbna SET num_votos_nulos = ? WHERE mesa_de_voto_nome=?;");
  1879. mysql.setInt(1, votos_null + 1);
  1880. mysql.setString(2, table);
  1881. mysql.executeUpdate();
  1882. conn.commit();
  1883. } catch (SQLException e) {
  1884. return "erro2!\n";
  1885. }
  1886. adicional(cc, table, id_lista);
  1887. return "vote registed as null";
  1888. }
  1889. }
  1890. }
  1891.  
  1892. public synchronized void adicional(int cc, String table, int id_lista) throws RemoteException {
  1893. try {
  1894. Date date = new Date();
  1895. java.sql.Timestamp date1 = new java.sql.Timestamp(date.getTime());
  1896. mysql = conn.prepareStatement(
  1897. "INSERT INTO bd.eleitor_mesa_insta(eleitor_cc,mesa_de_voto_nome,moment) VALUES(?,?,?);");
  1898. mysql.setInt(1, cc);
  1899. mysql.setString(2, table);
  1900. mysql.setTimestamp(3, date1);
  1901. mysql.executeUpdate();
  1902. conn.commit();
  1903.  
  1904. } catch (SQLException e) {
  1905. e.printStackTrace();
  1906. out.println("Something's wrong!!!");
  1907. }
  1908. int id_eleicao = 0;
  1909. try {
  1910. mysql = conn.prepareStatement("SELECT ideleicao1 from bd.tipo_eleitores_vota WHERE id_lista=?;");
  1911. mysql.setInt(1, id_lista);
  1912. ResultSet rQ = mysql.executeQuery();
  1913.  
  1914. while (rQ.next()) {
  1915. id_eleicao = rQ.getInt("ideleicao1");
  1916. }
  1917.  
  1918. } catch (SQLException e) {
  1919. out.println("error");
  1920. }
  1921. try {
  1922. mysql = conn.prepareStatement(
  1923. "INSERT INTO bd.eleitor_eleicao(eleitor_cc,eleicao_ideleicao) VALUES(?,?);");
  1924. mysql.setInt(1, cc);
  1925. mysql.setInt(2, id_eleicao);
  1926. mysql.executeUpdate();
  1927. conn.commit();
  1928.  
  1929. } catch (SQLException e) {
  1930. e.printStackTrace();
  1931. out.println("Something's wrong!!!");
  1932. }
  1933. notifyAdmins("The person with the cc number of:" + cc + " has votted on the table:" + table);
  1934. // falta aqui uma notificaçao
  1935. }
  1936.  
  1937. public void loginDone(String cc, String mesa) throws RemoteException {
  1938. for (Pessoa x : get_pessoas()) {
  1939. if (x.numero_cc == Integer.parseInt(cc)) {
  1940. out.println(x.numero_cc+"="+Integer.parseInt(cc));
  1941. notifyAdmins("The person:" + x.nome + " has logged in the table:" + mesa);
  1942. break;
  1943. }
  1944. }
  1945. }
  1946.  
  1947. public synchronized boolean searchFacultyForDelete(String name) {
  1948. String idBD;
  1949. out.println("Checking if faculty is ok to delete...");
  1950. // Verifica se têm mesas associadas
  1951. try {
  1952. mysql = conn.prepareStatement(
  1953. "SELECT faculdade_nome FROM bd.mesa_de_voto_faculdade WHERE faculdade_nome = ?;");
  1954. mysql.setString(1, name);
  1955. ResultSet rQ = mysql.executeQuery();
  1956.  
  1957. while (rQ.next()) {
  1958. idBD = rQ.getString("faculdade_nome");
  1959. if (idBD.toUpperCase().equals(name.toUpperCase())) {
  1960. out.println("Faculty have a table within...can't be deleted!");
  1961. return false;
  1962. }
  1963. }
  1964.  
  1965. } catch (SQLException e) {
  1966. out.println("Something's wrong analysing tables from faculty!!!");
  1967. return false;
  1968. }
  1969. // Verifica se têm eleitores registados
  1970. try {
  1971. mysql = conn.prepareStatement(
  1972. "SELECT faculdade_nome FROM bd.eleitor_faculdade WHERE faculdade_nome = ?;");
  1973. mysql.setString(1, name);
  1974. ResultSet rQ = mysql.executeQuery();
  1975.  
  1976. while (rQ.next()) {
  1977. idBD = rQ.getString("faculdade_nome");
  1978. if (idBD.toUpperCase().equals(name.toUpperCase())) {
  1979. out.println("Faculty have people on their data base ...can't be deleted!");
  1980. return false;
  1981. }
  1982. }
  1983.  
  1984. } catch (SQLException e) {
  1985. out.println("Something's wrong analysing voters residents at faculty!!!");
  1986. return false;
  1987. }
  1988. // Verifica se contem departamentos
  1989. try {
  1990. mysql = conn.prepareStatement(
  1991. "SELECT faculdade_nome FROM bd.departamento_faculdade WHERE faculdade_nome = ?;");
  1992. mysql.setString(1, name);
  1993. ResultSet rQ = mysql.executeQuery();
  1994.  
  1995. while (rQ.next()) {
  1996. idBD = rQ.getString("faculdade_nome");
  1997. if (idBD.toUpperCase().equals(name.toUpperCase())) {
  1998. out.println("Faculty have departments on their data base ...can't be deleted!");
  1999. return false;
  2000. }
  2001. }
  2002.  
  2003. } catch (SQLException e) {
  2004. out.println("Something's wrong analysing departments from faculty!!!");
  2005. return false;
  2006. }
  2007.  
  2008. return true;
  2009. }
  2010.  
  2011. public synchronized boolean searchDepartmentForDelete(String name) {
  2012. String idBD;
  2013. out.println("Checking if department is ok to delete...");
  2014. try {
  2015. mysql = conn.prepareStatement(
  2016. "SELECT departamento_nome FROM bd.departamento_mesa_de_voto WHERE departamento_nome = ?;");
  2017. mysql.setString(1, name);
  2018. ResultSet rQ = mysql.executeQuery();
  2019.  
  2020. while (rQ.next()) {
  2021. idBD = rQ.getString("departamento_nome");
  2022. if (idBD.toUpperCase().equals(name.toUpperCase())) {
  2023. out.println("Departament have a table within...can't be deleted!");
  2024. return false;
  2025. }
  2026. }
  2027.  
  2028. } catch (SQLException e) {
  2029. out.println("Something's wrong analysing department!!!");
  2030. return false;
  2031. }
  2032. return true;
  2033. }
  2034.  
  2035. public synchronized boolean deleteDepartment(String name) {
  2036. out.println("Deleting Department...");
  2037. try {
  2038. mysql = conn.prepareStatement("DELETE FROM eleitor_departamento WHERE departamento_nome=?;");
  2039. mysql.setString(1, name);
  2040. mysql.executeUpdate();
  2041. conn.commit();
  2042.  
  2043. } catch (SQLException e) {
  2044. out.println("Something's wrong deleting from eleitor_departamento!!!");
  2045. return false;
  2046. }
  2047. try {
  2048. mysql = conn.prepareStatement("DELETE FROM departamento_faculdade WHERE departamento_nome=?;");
  2049. mysql.setString(1, name);
  2050. mysql.executeUpdate();
  2051. conn.commit();
  2052.  
  2053. } catch (SQLException e) {
  2054. out.println("Something's wrong deleting from departamento_faculdade!!!");
  2055. return false;
  2056. }
  2057. try {
  2058. mysql = conn.prepareStatement("DELETE FROM departamento WHERE nome=?;");
  2059. mysql.setString(1, name);
  2060. mysql.executeUpdate();
  2061. conn.commit();
  2062.  
  2063. } catch (SQLException e) {
  2064. out.println("Something's wrong deleting from departamento!!!");
  2065. return false;
  2066. }
  2067. out.println("Department Deleted...");
  2068. return true;
  2069. }
  2070.  
  2071. public synchronized boolean deleteFaculty(String name) {
  2072. out.println("Deleting Faculty...");
  2073. try {
  2074. mysql = conn.prepareStatement("DELETE FROM faculdade WHERE nome=?;");
  2075. mysql.setString(1, name);
  2076. mysql.executeUpdate();
  2077. conn.commit();
  2078.  
  2079. } catch (SQLException e) {
  2080. out.println("Something's wrong deleting faculty!!!");
  2081. return false;
  2082. }
  2083. out.println("Faculty deleted...");
  2084. return true;
  2085. }
  2086.  
  2087. public synchronized boolean login(String username, String password) {
  2088. for (Pessoa x : get_pessoas()) {
  2089. if (x.nome.equals(username)) {
  2090. if (x.password.equals(password)) {
  2091. x.loggedIN = true;
  2092. return true;
  2093. }
  2094. }
  2095. }
  2096. return false;
  2097. }
  2098.  
  2099. public synchronized boolean deleteElection(int id) {
  2100. String name;
  2101. out.println("Deleting election...");
  2102. // Eliminar do eleicao tabela candidato_eleicao
  2103. try {
  2104. mysql = conn.prepareStatement("DELETE FROM tipo_eleitores_vota WHERE ideleicao1=?;");
  2105. mysql.setInt(1, id);
  2106. mysql.executeUpdate();
  2107. conn.commit();
  2108.  
  2109. } catch (SQLException e) {
  2110. e.printStackTrace();
  2111. out.println("Something's wrong deleting from candidato_eleicao!!!");
  2112. return false;
  2113. }
  2114. // Eliminar eleicao da mesa
  2115. // arranja mesa
  2116. try {
  2117. mysql = conn.prepareStatement("select mesa_de_voto_nome from votosbna where eleicao_ideleicao=?;");
  2118. mysql.setInt(1, id);
  2119. ResultSet rQ = mysql.executeQuery();
  2120.  
  2121. while (rQ.next()) {
  2122. name = rQ.getString("mesa_de_voto_nome");
  2123. freeTablesBeforeDelete(name);
  2124. }
  2125.  
  2126. } catch (SQLException e) {
  2127. out.println("Something's wrong on votosbna!!!");
  2128. return false;
  2129. }
  2130. //////
  2131. try {
  2132. mysql = conn.prepareStatement("DELETE FROM votosbna WHERE eleicao_ideleicao=?;");
  2133. mysql.setInt(1, id);
  2134. mysql.executeUpdate();
  2135. conn.commit();
  2136.  
  2137. } catch (SQLException e) {
  2138. out.println("Something's wrong deleting from votosbna!!!");
  2139. return false;
  2140. }
  2141. // apagar do departamento e faculdade
  2142. try {
  2143. mysql = conn.prepareStatement("DELETE FROM eleicao_departamento WHERE ideleicao1=?;");
  2144. mysql.setInt(1, id);
  2145. mysql.executeUpdate();
  2146. conn.commit();
  2147. } catch (SQLException e) {
  2148. e.printStackTrace();
  2149. out.println("Something's wrong deleting from department!!!");
  2150. return false;
  2151. }
  2152. try {
  2153. mysql = conn.prepareStatement("DELETE FROM eleicao_faculdade WHERE ideleicao1=?;");
  2154. mysql.setInt(1, id);
  2155. mysql.executeUpdate();
  2156. conn.commit();
  2157. } catch (SQLException e) {
  2158. e.printStackTrace();
  2159. out.println("Something's wrong deleting from eleicao!!!");
  2160. return false;
  2161. }
  2162. try {
  2163. mysql = conn.prepareStatement("DELETE FROM eleicao WHERE ideleicao=?;");
  2164. mysql.setInt(1, id);
  2165. mysql.executeUpdate();
  2166. conn.commit();
  2167. } catch (SQLException e) {
  2168. e.printStackTrace();
  2169. out.println("Something's wrong deleting from eleicao!!!");
  2170. return false;
  2171. }
  2172.  
  2173. out.println("Election deleted...");
  2174. return true;
  2175. }
  2176.  
  2177. public synchronized boolean freeTablesBeforeDelete(String name) {
  2178. out.println("Freeing tables...");
  2179. try {
  2180. mysql = conn.prepareStatement("DELETE FROM eleitor_mesa_de_voto WHERE mesa_de_voto_nome=?;");
  2181. mysql.setString(1, name);
  2182. mysql.executeUpdate();
  2183. conn.commit();
  2184.  
  2185. } catch (SQLException e) {
  2186. out.println("Something's wrong deleting from eleitor_mesa_de_voto!!!");
  2187. return false;
  2188. }
  2189. try {
  2190. mysql = conn.prepareStatement("DELETE FROM departamento_mesa_de_voto WHERE mesa_de_voto_nome=?;");
  2191. mysql.setString(1, name);
  2192. mysql.executeUpdate();
  2193. conn.commit();
  2194.  
  2195. } catch (SQLException e) {
  2196. out.println("Something's wrong deleting from departamento_mesa_de_voto!!!");
  2197. return false;
  2198. }
  2199. try {
  2200. mysql = conn.prepareStatement("DELETE FROM mesa_de_voto_faculdade WHERE mesa_de_voto_nome=?;");
  2201. mysql.setString(1, name);
  2202. mysql.executeUpdate();
  2203. conn.commit();
  2204.  
  2205. } catch (SQLException e) {
  2206. out.println("Something's wrong deleting from mesa_de_voto_faculdade!!!");
  2207. return false;
  2208. }
  2209. return true;
  2210. }
  2211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement