Guest User

Untitled

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