Advertisement
Guest User

Untitled

a guest
Dec 15th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 34.01 KB | None | 0 0
  1. package RMI;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import static java.lang.Thread.sleep;
  7. import java.net.DatagramPacket;
  8. import java.net.DatagramSocket;
  9. import java.net.InetAddress;
  10. import java.net.SocketException;
  11. import java.rmi.Remote;
  12. import java.rmi.RemoteException;
  13. import java.rmi.registry.LocateRegistry;
  14. import java.rmi.server.UnicastRemoteObject;
  15. import java.sql.Connection;
  16. import java.sql.DriverManager;
  17. import java.sql.ResultSet;
  18. import java.sql.SQLException;
  19. import java.text.DateFormat;
  20. import java.text.ParseException;
  21. import java.text.SimpleDateFormat;
  22. import java.util.Calendar;
  23. import java.util.Date;
  24. import java.util.Properties;
  25. import java.util.Random;
  26. import java.util.Scanner;
  27. import java.util.logging.Level;
  28. import java.util.logging.Logger;
  29. import RMI.Interface;
  30.  
  31.  
  32. public class RMI extends UnicastRemoteObject implements Interface{
  33.  
  34. public static String url;
  35.  
  36. // Your credentials
  37. private static final String USER = "acj";
  38. private static final String PASS = "bd";
  39. // Server IP (or localhost, if same machine)
  40. private static final String SERVER = "127.0.0.1";
  41. //"10.0.2.2";
  42. private static final String PORT = "1521";
  43. public static int port;
  44. private static final long serialVersionUID = 1L;
  45.  
  46. public RMI rmi;
  47.  
  48. static Scanner sc = new Scanner(System.in);
  49.  
  50. //Example method to establish 1 connection
  51.  
  52. public static Connection getConnection() throws SQLException {
  53.  
  54. Properties connectionProps = new Properties();
  55. connectionProps.put("user", USER);
  56. connectionProps.put("password", PASS);
  57. Connection conn = DriverManager.getConnection(
  58. "jdbc:oracle:thin://@" + SERVER
  59. + ":" + PORT + "/XE", // XE only for Oracle XE
  60. connectionProps);
  61. conn.setAutoCommit(false); // This is important!!
  62.  
  63. return conn;
  64.  
  65. }
  66.  
  67. public RMI() throws RemoteException, SQLException {
  68.  
  69. super();
  70. getConnection();
  71. new refreshDate().start();
  72.  
  73.  
  74. }
  75.  
  76. public int teste() throws ParseException, RemoteException, SQLException {
  77. return 9;
  78. }
  79.  
  80. public String registarEleitor(int num_eleitor, String password, int id_dep, int telemovel, String morada, String tipo, String validade_cc, int cc) throws RemoteException, SQLException, ParseException {
  81.  
  82.  
  83. Connection conn1 = getConnection();
  84. boolean flag = false;
  85. String mensagem = "type | registerUser; status | user created";
  86. java.util.Date datasistema = datasistema();
  87. java.util.Date dataValidade = conversao(validade_cc);
  88.  
  89. if (dataValidade.after(datasistema)) {
  90.  
  91. try {
  92.  
  93. java.sql.Statement stat = conn1.createStatement();
  94.  
  95.  
  96. stat.execute("INSERT into eleitor (num_eleitor,senha,id_dep,telemovel,morada,tipo,validade_cc,cc) VALUES ('"+num_eleitor+"','"+password+"','"+id_dep+"','"+telemovel+"','"+morada+"','"+tipo+"','"+validade_cc+"','"+cc+"' ) ");
  97. flag = true;
  98. conn1.commit();
  99.  
  100. } catch (SQLException e) {
  101. // log the warning...
  102. e.printStackTrace();
  103. }
  104.  
  105.  
  106. }
  107. else {
  108. flag = false;
  109. }
  110.  
  111.  
  112.  
  113. if (flag) {
  114. return mensagem;
  115. }
  116. else {
  117. return "type | registerUser; registry not created";
  118. }
  119. }
  120.  
  121. public class refreshDate extends Thread //FUNCAO THREAD PARA ENCERRAR PROJETOS
  122. {
  123. public void run()
  124. {
  125. while(true)
  126. {
  127. try
  128. {
  129. terminoLeilao();
  130. sleep(1000*60);
  131. }
  132. catch (InterruptedException e)
  133. {
  134. e.printStackTrace();
  135. } catch (RemoteException e) {
  136. e.printStackTrace();
  137. } catch (SQLException ex) {
  138. Logger.getLogger(RMI.class.getName()).log(Level.SEVERE, null, ex);
  139. } catch (ParseException ex) {
  140. Logger.getLogger(RMI.class.getName()).log(Level.SEVERE, null, ex);
  141. }
  142. }
  143. }
  144. }
  145.  
  146. public String criarEleicao(String tipo, String data_inicio, String data_fim, String comentario) throws ParseException, RemoteException, SQLException {
  147.  
  148. Connection conn1 = getConnection();
  149. boolean flag = false;
  150. String mensagem = "type | registerElection; status | registered; cod_eleicao |";
  151. Random randomGenerator = new Random();
  152. int cod_eleicao = randomGenerator.nextInt(1000);
  153. String query = "SELECT eleicoes.cod_eleicao FROM eleicoes";
  154.  
  155. try {
  156. java.sql.Statement stat = conn1.createStatement();
  157. java.sql.Statement stat1 = conn1.createStatement();
  158. ResultSet rs = stat1.executeQuery(query);
  159.  
  160. while(rs.next()) {
  161. //Se entrar aqui, calcular um novo id
  162. cod_eleicao = randomGenerator.nextInt(1000);
  163. }
  164. mensagem = mensagem + cod_eleicao;
  165. stat.execute("INSERT into eleicoes (cod_eleicao,tipo,data_inicio,data_fim,comentario) VALUES ('"+cod_eleicao+"','"+tipo+"','"+data_inicio+"','"+data_fim+"','"+comentario+"')");
  166. flag = true;
  167. conn1.commit();
  168. }
  169. catch (SQLException e) {
  170. // log the warning...
  171. e.printStackTrace();
  172. }
  173.  
  174. if (flag) {
  175. return mensagem;
  176. }
  177. else {
  178. return "type | registerElection; status | not registered";
  179. }
  180.  
  181. }
  182.  
  183.  
  184. public String alterarTipoEleicao(int id_eleicao, String tipo) throws ParseException, RemoteException, SQLException {
  185.  
  186. Connection conn1 = getConnection();
  187. boolean flag = false;
  188. String mensagem = "type | change election; status | election title changed;";
  189. String query = "SELECT eleicoes.data_inicio, eleicoes.data_fim FROM eleicoes";
  190.  
  191. try {
  192. java.sql.Statement stat = conn1.createStatement();
  193. java.sql.Statement stat1 = conn1.createStatement();
  194. ResultSet rs = stat.executeQuery(query);
  195.  
  196. while(rs.next()) {
  197.  
  198. String data_inicio = rs.getString(1);
  199. String data_fim = rs.getString(2);
  200.  
  201. java.util.Date inicio = conversao(data_inicio);
  202. java.util.Date fim = conversao(data_fim);
  203. java.util.Date actual = datasistema();
  204.  
  205. if (actual.before(fim)) {
  206. if (actual.after(inicio)) {
  207. System.out.println("ERRO. a eleicao ja esta a decorrer");
  208. break;
  209. }
  210. }
  211. else {
  212. stat1.execute("UPDATE eleicoes SET eleicoes.tipo = ('"+tipo+"') ");
  213. conn1.commit();
  214.  
  215. flag = true;
  216. }
  217. }
  218. }
  219. catch (SQLException e) {
  220. // log the warning...
  221. e.printStackTrace();
  222. }
  223.  
  224. if (flag) {
  225. return mensagem;
  226. }
  227. else {
  228. return "type | change election; status | election title not changed;";
  229. }
  230.  
  231. }
  232.  
  233.  
  234.  
  235. public String alterarInicioEleicao(int id_eleicao, String data_novo_inicio) throws ParseException, RemoteException, SQLException {
  236.  
  237. Connection conn1 = getConnection();
  238. boolean flag = false;
  239. String mensagem = "type | change election; status | election start changed;";
  240. String query = "SELECT eleicoes.data_inicio, eleicoes.data_fim FROM eleicoes";
  241.  
  242. try {
  243. java.sql.Statement stat = conn1.createStatement();
  244. java.sql.Statement stat1 = conn1.createStatement();
  245. ResultSet rs = stat.executeQuery(query);
  246.  
  247. while(rs.next()) {
  248.  
  249. String data_inicio = rs.getString(1);
  250. String data_fim = rs.getString(2);
  251.  
  252. java.util.Date inicio = conversao(data_inicio);
  253. java.util.Date fim = conversao(data_fim);
  254. java.util.Date actual = datasistema();
  255.  
  256. if (actual.before(fim)) {
  257. if (actual.after(inicio)) {
  258. System.out.println("ERRO. a eleicao ja esta a decorrer");
  259. break;
  260. }
  261. }
  262. else {
  263. stat1.execute("UPDATE eleicoes SET eleicoes.inicio = ('"+data_novo_inicio+"') ");
  264. conn1.commit();
  265.  
  266. flag = true;
  267. }
  268. }
  269. }
  270. catch (SQLException e) {
  271. // log the warning...
  272. e.printStackTrace();
  273. }
  274.  
  275. if (flag) {
  276. return mensagem;
  277. }
  278. else {
  279. return "type | change election; status | election start not changed;";
  280. }
  281.  
  282. }
  283.  
  284. public String alterarFimEleicao(int id_eleicao, String data_novo_fim) throws ParseException, RemoteException, SQLException {
  285.  
  286. Connection conn1 = getConnection();
  287. boolean flag = false;
  288. String mensagem = "type | change election; status | election end changed;";
  289. String query = "SELECT eleicoes.data_inicio, eleicoes.data_fim FROM eleicoes";
  290.  
  291. try {
  292. java.sql.Statement stat = conn1.createStatement();
  293. java.sql.Statement stat1 = conn1.createStatement();
  294. ResultSet rs = stat.executeQuery(query);
  295.  
  296. while(rs.next()) {
  297.  
  298. String data_inicio = rs.getString(1);
  299. String data_fim = rs.getString(2);
  300.  
  301. java.util.Date inicio = conversao(data_inicio);
  302. java.util.Date fim = conversao(data_fim);
  303. java.util.Date actual = datasistema();
  304.  
  305. if (actual.before(fim)) {
  306. if (actual.after(inicio)) {
  307. System.out.println("ERRO. a eleicao ja esta a decorrer");
  308. break;
  309. }
  310. }
  311. else {
  312. stat1.execute("UPDATE eleicoes SET eleicoes.inicio = ('"+data_novo_fim+"') ");
  313. conn1.commit();
  314.  
  315. flag = true;
  316. }
  317. }
  318. }
  319. catch (SQLException e) {
  320. // log the warning...
  321. e.printStackTrace();
  322. }
  323.  
  324. if (flag) {
  325. return mensagem;
  326. }
  327. else {
  328. return "type | change election; status | election end not changed";
  329. }
  330.  
  331. }
  332.  
  333. public String alterarComentarioEleicao(int id_eleicao, String comentario) throws ParseException, RemoteException, SQLException {
  334.  
  335. Connection conn1 = getConnection();
  336. boolean flag = false;
  337. String mensagem = "type | change election; status | election comment changed;";
  338. String query = "SELECT eleicoes.data_inicio, eleicoes.data_fim FROM eleicoes";
  339.  
  340. try {
  341. java.sql.Statement stat = conn1.createStatement();
  342. java.sql.Statement stat1 = conn1.createStatement();
  343. ResultSet rs = stat.executeQuery(query);
  344.  
  345. while(rs.next()) {
  346.  
  347. String data_inicio = rs.getString(1);
  348. String data_fim = rs.getString(2);
  349.  
  350. java.util.Date inicio = conversao(data_inicio);
  351. java.util.Date fim = conversao(data_fim);
  352. java.util.Date actual = datasistema();
  353.  
  354. if (actual.before(fim)) {
  355. if (actual.after(inicio)) {
  356. System.out.println("ERRO. a eleicao ja esta a decorrer");
  357. break;
  358. }
  359. }
  360. else {
  361. stat1.execute("UPDATE eleicoes SET eleicoes.comentario = ('"+comentario+"') ");
  362. conn1.commit();
  363.  
  364. flag = true;
  365. }
  366. }
  367. }
  368. catch (SQLException e) {
  369. // log the warning...
  370. e.printStackTrace();
  371. }
  372.  
  373. if (flag) {
  374. return mensagem;
  375. }
  376. else {
  377. return "type | change election; status | election comment not changed;";
  378. }
  379. }
  380.  
  381. public String gerirDepartamento(int id_fac, String nome) throws RemoteException, SQLException {
  382.  
  383. Connection conn1 = getConnection();
  384. boolean flag = false;
  385. String mensagem = "";
  386. Random randomGenerator = new Random();
  387. int id_dep = randomGenerator.nextInt(1000);
  388. String query = "SELECT id_dep FROM departamento WHERE id_dep = ('"+id_dep+"')";
  389. String query1 = "SELECT id_fac FROM faculdade WHERE id_fac = ('"+id_fac+"') ";
  390.  
  391. try {
  392. java.sql.Statement stat = conn1.createStatement();
  393. java.sql.Statement stat1 = conn1.createStatement();
  394. java.sql.Statement stat2 = conn1.createStatement();
  395.  
  396. ResultSet rs = stat1.executeQuery(query);
  397. ResultSet rs1 = stat2.executeQuery(query1);
  398.  
  399. //FAZER AQUI CLAUSULA PARA VERIFICAR SE O ID DA FACULDADE JA EXISTE!!!!!!!!!!
  400.  
  401. while (rs1.next()) {
  402. //Se entrar aqui, é porque existe um id da faculdade
  403.  
  404. while(rs.next()) {
  405. //Se entrar aqui, calcular um novo id
  406. id_dep = randomGenerator.nextInt(1000);
  407.  
  408. }
  409.  
  410. stat.execute("INSERT into departamento (id_dep, id_fac, nome) VALUES ('"+id_dep+"','"+id_fac+"','"+nome+"') ");
  411. mensagem = "type | gerirDepartamento; id_fac | "+id_fac+"; nome | "+nome+"id_dep | "+id_dep+ "; department created";
  412. flag = true;
  413. conn1.commit();
  414. //e necessario alterar a mensagem de impressao!!!!!!!!!!!
  415.  
  416. }
  417.  
  418. }
  419. catch (SQLException e) {
  420. e.printStackTrace();
  421. }
  422.  
  423. if (flag) {
  424. return mensagem;
  425. }
  426. else {
  427. return "type | gerirDepartamento; department not created";
  428. }
  429.  
  430. }
  431.  
  432. public String gerirFaculdade(String nome) throws RemoteException, SQLException {
  433.  
  434. System.out.println("entrou");
  435. Connection conn1 = getConnection();
  436. boolean flag = false;
  437. String mensagem = "";
  438. Random randomGenerator = new Random();
  439. int id_fac = randomGenerator.nextInt(1000);
  440. String query = "SELECT id_fac FROM faculdade WHERE faculdade.id_fac = id_fac";
  441.  
  442. try {
  443. java.sql.Statement stat = conn1.createStatement();
  444. java.sql.Statement stat1 = conn1.createStatement();
  445.  
  446. ResultSet rs = stat1.executeQuery(query);
  447.  
  448. //while (rs.next()) {
  449.  
  450. System.out.println("entrou1");
  451. stat.execute("INSERT into faculdade (id_fac,name) VALUES ('"+id_fac+"','"+nome+"') ");
  452. mensagem = "type | gerirFaculdade; nome | "+nome+"id_fac | "+id_fac+"; Faculdade created";
  453. flag = true;
  454. conn1.commit();
  455.  
  456. //}
  457.  
  458. }
  459. catch (SQLException e) {
  460. System.out.println("erro na BD: parametro existente");
  461. //e.printStackTrace();
  462. }
  463.  
  464. if (flag) {
  465. return mensagem;
  466. }
  467. else {
  468. return "error";
  469. }
  470. }
  471.  
  472. public String gerirListas(String tipo, String nome, int cod_eleicao) throws RemoteException, SQLException {
  473.  
  474. //Uma lista de estudantes só funciona para uma eleição de listas de estudantes.
  475.  
  476. Connection conn1 = getConnection();
  477. boolean flag = false;
  478.  
  479. Random randomGenerator = new Random();
  480. int cod_lista = randomGenerator.nextInt(1000);
  481. String query = "SELECT listacandidatos.cod_lista FROM listacandidatos WHERE listacandidatos.cod_lista = ('"+cod_lista+"') ";
  482. String mensagem = "type | manageCandidates; status | List created; id_lista |";
  483.  
  484. try {
  485.  
  486. java.sql.Statement stat = conn1.createStatement();
  487. java.sql.Statement stat1 = conn1.createStatement();
  488.  
  489. ResultSet rs = stat1.executeQuery(query);
  490.  
  491. while(rs.next()) {
  492.  
  493. cod_lista = randomGenerator.nextInt(1000);
  494.  
  495. }
  496. mensagem = mensagem + " " + cod_lista;
  497. stat.execute("INSERT into LISTACANDIDATOS (cod_lista,nome,tipo,cod_eleicao) VALUES ('"+cod_lista+"','"+nome+"','"+tipo+"','"+cod_eleicao+"')");
  498. stat.execute("INSERT into CONCORRER (cod_lista,cod_eleicao) VALUES ('"+cod_lista+"','"+cod_eleicao+"')");
  499. stat.execute("INSERT into num_validos (cod_eleicao,cod_lista,num_brancos,num_nulos,num_validos) VALUES ('"+cod_eleicao+"','"+cod_lista+"',0,0,0) ");
  500. flag = true;
  501. conn1.commit();
  502.  
  503. }
  504. catch (SQLException e) {
  505. // log the warning...
  506. e.printStackTrace();
  507. }
  508.  
  509. if (flag) {
  510. return "mensagem";
  511. }
  512. else {
  513. return "type | manageCandidates; status | List not created";
  514. }
  515. }
  516.  
  517. public boolean identificarEleitor(int num_eleitor/*, String password, String opcao, String voto, int cod_eleicao, int cod_lista*/) throws RemoteException, SQLException {
  518.  
  519. //Aqui consiste tem pesquisar por qualquer campo do eleitor de modo a identificá-lo.
  520. //Pesquisar o numero de eleitor e verificar se existe.
  521. //Quando for identificado, passar à função de "login".
  522.  
  523. String query = "SELECT eleitor.num_eleitor FROM eleitor WHERE eleitor.num_eleitor = ('"+num_eleitor+"') ";
  524. Connection conn1 = getConnection();
  525. boolean flag = false;
  526. String mensagem = "type | unlock; status | unlocked"; // ALTERAR MENSAGEM
  527.  
  528. try {
  529. java.sql.Statement stat = conn1.createStatement();
  530. ResultSet rs = stat.executeQuery(query);
  531.  
  532. while(rs.next()) {
  533. //Se entrar aqui, foi bem sucedido
  534. System.out.println("Utilizador identificado");
  535. flag = true;
  536. //autenticacaoEleitor(num_eleitor,password, opcao, voto, cod_eleicao, cod_lista);
  537. }
  538. }
  539. catch (SQLException e) {
  540. e.printStackTrace();
  541. }
  542.  
  543. if (flag) {
  544. return true;
  545. }
  546. else {
  547. return false/*"error"*/;
  548. }
  549.  
  550. }
  551.  
  552. public boolean autenticacaoEleitor(int num_eleitor, String password/*, String opcao, String voto, int cod_eleicao, int cod_lista*/) throws RemoteException, SQLException {
  553.  
  554. //Aqui solicita ao utilizador a sua senha e o seu numero de eleitor.
  555. //Confirma se os dados existem
  556. //Se existirem, passa à função de voto
  557.  
  558. String query = "SELECT eleitor.num_eleitor, eleitor.senha FROM eleitor WHERE eleitor.num_eleitor = ('"+num_eleitor+"') AND eleitor.senha = ('"+password+"')";
  559. Connection conn1 = getConnection();
  560. boolean flag = false;
  561. String mensagem = "type | login; status | logged;"; // ALTERAR MENSAGEM
  562.  
  563. try {
  564. java.sql.Statement stat = conn1.createStatement();
  565. ResultSet rs = stat.executeQuery(query);
  566.  
  567. while(rs.next()) {
  568. //Utilizador autenticado
  569. System.out.println("Utilizador autenticado");
  570. flag =true;
  571. //votar(num_eleitor,opcao, voto, cod_eleicao, cod_lista);
  572. }
  573. }
  574. catch (SQLException e) {
  575. e.printStackTrace();
  576. }
  577.  
  578. if (flag) {
  579. return true;
  580. }
  581. else {
  582. return false;
  583. }
  584.  
  585. }
  586.  
  587. public String voto (int num_eleitor, String voto, int cod_eleicao, int cod_lista) throws SQLException, RemoteException, ParseException {
  588.  
  589. Connection conn1 = getConnection();
  590. boolean flag = false;
  591. int tipo = 0;
  592. int valor = 0;
  593.  
  594. try {
  595.  
  596. java.sql.Statement stat = conn1.createStatement();
  597.  
  598. java.sql.Statement stat1 = conn1.createStatement();
  599.  
  600. java.sql.Statement stat2 = conn1.createStatement();
  601.  
  602. java.sql.Statement stat3 = conn1.createStatement();
  603.  
  604. java.sql.Statement stat4 = conn1.createStatement();
  605.  
  606. java.sql.Statement stat5 = conn1.createStatement();
  607.  
  608. java.sql.Statement stat10 = conn1.createStatement();
  609.  
  610. java.sql.Statement stat11 = conn1.createStatement();
  611.  
  612. java.sql.Statement stat12 = conn1.createStatement();
  613.  
  614. Date dataAtual = datasistema(); //Data para string
  615.  
  616. String data = dataAtualFormatada();
  617.  
  618. int id_mesa = 0;
  619.  
  620. int id_dep_user = 0;
  621.  
  622. String tipoEleitor = "",tipoEleicao = "";
  623.  
  624. String query = "SELECT listacandidatos.nome, listacandidatos.tipo FROM listacandidatos WHERE listacandidatos.cod_lista = ('"+cod_lista+"') ";
  625.  
  626. String query1 = "SELECT num_brancos,num_nulos,num_validos FROM num_validos WHERE num_validos.cod_lista = ('"+cod_lista+"') ";
  627.  
  628. String query2 = "SELECT id_dep FROM eleitor WHERE eleitor.num_eleitor = ('"+num_eleitor+"') ";
  629.  
  630. String query8 = "SELECT eleitor.tipo FROM eleitor WHERE eleitor.num_eleitor = ('"+num_eleitor+"')";
  631.  
  632. String query9 = "SELECT eleicoes.tipo FROM eleicoes WHERE eleicoes.cod_eleicao = ('"+cod_eleicao+"') ";
  633.  
  634. ResultSet rs = stat2.executeQuery(query);
  635.  
  636. ResultSet rs1 = stat3.executeQuery(query1);
  637.  
  638. ResultSet rs2 = stat4.executeQuery(query2);
  639.  
  640. ResultSet rs8 = stat10.executeQuery(query8);
  641.  
  642. ResultSet rs9 = stat11.executeQuery(query9);
  643.  
  644. while (rs8.next()) {
  645. tipoEleitor = rs8.getString(1);
  646. }
  647.  
  648. while (rs9.next()) {
  649. tipoEleicao = rs9.getString(1);
  650. }
  651.  
  652. if ( (tipoEleicao.trim().equals("nucleo") && tipoEleitor.trim().equals("estudante")) || (tipoEleicao.trim().equals("conselho") && tipoEleitor.trim().equals("estudante")) || (tipoEleicao.trim().equals("conselho") && tipoEleitor.trim().equals("docente")) || (tipoEleicao.trim().equals("conselho") && tipoEleitor.trim().equals("funcionario")) ) {
  653. //Falta uma condição para verificar as listas
  654. while (rs2.next()) {
  655.  
  656. id_dep_user = rs2.getInt(1);
  657.  
  658. String query3 = "SELECT id_mesa FROM mesa WHERE mesa.id_dep = ('"+id_dep_user+"') ";
  659.  
  660. ResultSet rs3 = stat5.executeQuery(query3);
  661.  
  662. while(rs3.next()) {
  663.  
  664. id_mesa = rs3.getInt(1);
  665. }
  666. }
  667.  
  668. while (rs1.next()) {
  669. int num_brancos = rs1.getInt(1);
  670. int num_nulos = rs1.getInt(2);
  671. int num_validos = rs1.getInt(3);
  672.  
  673. while (rs.next()) { //Existe o codigo de lista desejado
  674. String tipo_lista = rs.getString(2);
  675. if ( (tipoEleitor.trim().equals("estudante") && tipo_lista.trim().equals("estudante")) || (tipoEleitor.trim().equals("docente") && tipo_lista.trim().equals("docente")) || (tipoEleitor.trim().equals("funcionario") && tipo_lista.trim().equals("funcionario"))) {
  676. if (voto.equals(Integer.toString(cod_lista))) {
  677.  
  678. num_validos = num_validos + 1;
  679. System.out.println(num_validos);
  680. flag = true;
  681. stat.execute("UPDATE num_validos SET num_validos.num_validos = ('"+num_validos+"') WHERE num_validos.cod_eleicao = ('"+cod_eleicao+"') ");
  682. conn1.commit();
  683. }
  684. else if (voto.equals("")) { //O voto é branco
  685.  
  686. System.out.println("entrou");
  687. num_brancos = num_brancos + 1;
  688. flag = true;
  689. stat.execute("UPDATE num_validos SET num_validos.num_brancos = ('"+num_brancos+"') WHERE num_validos.cod_eleicao = ('"+cod_eleicao+"')");
  690. tipo = 2;
  691. valor = num_brancos;
  692. conn1.commit();
  693.  
  694. }
  695. else { // O voto é nulo
  696. num_nulos = num_nulos + 1;
  697. flag = true;
  698. stat.execute("UPDATE num_validos SET num_validos.num_nulos = ('"+num_nulos+"') WHERE num_validos.cod_eleicao = ('"+cod_eleicao+"')");
  699. tipo = 3;
  700. valor = num_nulos;
  701. conn1.commit();
  702. }
  703.  
  704. stat1.execute("INSERT into votar(num_eleitor,datavoto,id_mesa) VALUES ('"+num_eleitor+"','"+data+"','"+id_mesa+"')");
  705. conn1.commit();
  706. }
  707. }
  708. }
  709. }
  710. }
  711. catch (SQLException e) {
  712. System.out.println("erro na db");
  713. }
  714.  
  715. if (flag) {
  716. return "voted successfull";
  717. }
  718. else {
  719. return "error voting";
  720. }
  721.  
  722. }
  723.  
  724. public String dataAtualFormatada() throws SQLException, RemoteException, ParseException{
  725. // Create an instance of SimpleDateFormat used for formatting
  726. // the string representation of date (month/day/year)
  727. DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
  728.  
  729. // Get the date today using Calendar object.
  730. Date today = Calendar.getInstance().getTime();
  731. // Using DateFormat format method we can create a string
  732. // representation of a date with the defined format.
  733. String reportDate = df.format(today);
  734.  
  735. // Print what date is today!
  736. return (reportDate);
  737. }
  738.  
  739. public String gerirMesas(int id_dep, int cod_eleicao) throws SQLException, RemoteException {
  740.  
  741. //Adicionar e remover mesas de voto associadas a uma eleicao.
  742. Connection conn1 = getConnection();
  743. boolean flag = false;
  744. int signal=0;
  745. String mensagem = "type | manageTables; table created; id_table | ";
  746. Random randomGenerator = new Random();
  747. int id_mesa = randomGenerator.nextInt(1000);
  748.  
  749. //Verificar se já existe mesa de voto no departamento
  750. try {
  751. java.sql.Statement stat = conn1.createStatement();
  752. String query = "SELECT mesa.id_dep FROM mesa WHERE mesa.id_dep = ('"+id_dep+"') ";
  753. ResultSet rs = stat.executeQuery(query);
  754. while(rs.next()) {
  755. signal=1;
  756. break;
  757. //volta ao menu
  758. }
  759. if (signal==0) {
  760. mensagem = mensagem + id_mesa;
  761. stat.execute("INSERT into MESA (id_mesa,id_dep,cod_eleicao) VALUES ('"+id_mesa+"','"+id_dep+"','"+cod_eleicao+"') ");
  762. flag = true;
  763. conn1.commit();
  764. }
  765. }
  766.  
  767. catch (SQLException e) {
  768. e.printStackTrace();
  769. }
  770.  
  771. if (flag) {
  772. return mensagem;
  773. }
  774. else {
  775. return "type | manageTables; table not created";
  776. }
  777.  
  778. }
  779.  
  780. public String saberLocal(int num_eleitor) throws SQLException,RemoteException {
  781.  
  782. String query = "SELECT votar.id_mesa,votar.datavoto FROM votar WHERE votar.num_eleitor = ('"+num_eleitor+"') ";
  783. Connection conn1 = getConnection();
  784. boolean flag = false;
  785. int item_count = 0;
  786. int i=0;
  787. String mensagemInicial = "type | item_list; item_count | ";
  788. String mensagem = "";
  789.  
  790. try {
  791. java.sql.Statement stat = conn1.createStatement();
  792. ResultSet rs = stat.executeQuery(query);
  793.  
  794. while(rs.next()) {
  795. int id_mesa = rs.getInt(1);
  796. String dataVoto = rs.getString(2);
  797.  
  798. mensagem = mensagem + "item_"+i+"num_eleitor | "+num_eleitor+"; id_mesa | "+id_mesa+"; dataVoto | "+dataVoto;
  799. mensagemInicial = mensagemInicial + item_count + ";"+ mensagem;
  800. flag = true;
  801.  
  802. }
  803. mensagemInicial = mensagemInicial + item_count + ";"+ mensagem;
  804.  
  805. }
  806. catch (SQLException e) {
  807. e.printStackTrace();
  808. }
  809.  
  810. if (flag) {
  811. System.out.println(mensagemInicial);
  812. return mensagemInicial;
  813. }
  814. else {
  815. return "error";
  816. }
  817.  
  818. }
  819.  
  820. public String terminoLeilao() throws SQLException, RemoteException, ParseException {
  821.  
  822. //verificar dentro de eleicao constantemente se a hora de termino do leilao é inferior à hora atual do pc
  823. //caso seja, alterar o comentario para finalizado.
  824.  
  825. String query = "SELECT eleicoes.data_fim, eleicoes.cod_eleicao, eleicoes.tipo, eleicoes.data_inicio, eleicoes.comentario, eleicoes.titulo, eleicoes.descricao FROM eleicoes";
  826. Connection conn1 = getConnection();
  827. boolean flag = false;
  828. String comentario = "finalizado";
  829. String mensagem = "";
  830.  
  831. try {
  832. java.sql.Statement stat = conn1.createStatement();
  833. java.sql.Statement stat1 = conn1.createStatement();
  834. java.sql.Statement stat2 = conn1.createStatement();
  835. ResultSet rs = stat.executeQuery(query);
  836.  
  837. while(rs.next()) {
  838. String data_fim = rs.getString(1); // data de finalizar eleicao
  839. int cod_eleicao = rs.getInt(2);
  840. String tipo = rs.getString(3);
  841. String data_inicio = rs.getString(4);
  842. String comentario_eleicao = rs.getString(5);
  843. String titulo = rs.getString(6);
  844. String descricao = rs.getString(7);
  845.  
  846. //necessario comparar data_fim com data sistema!
  847. java.util.Date dataFinal = conversao(data_fim);
  848. java.util.Date dataActual = datasistema();
  849.  
  850. if (dataFinal.before(dataActual) && (!comentario_eleicao.equals(comentario))) {
  851.  
  852. stat1.execute("UPDATE eleicoes SET eleicoes.comentario = ('"+comentario+"') ");
  853. stat2.execute("INSERT into eleicoesPassadas(data_fim,cod_eleicao,tipo,data_inicio,comentario,titulo,descricao) VALUES ( ('"+data_fim+"'),('"+cod_eleicao+"'), ('"+tipo+"'), ('"+data_inicio+"'), ('"+comentario+"'), ('"+titulo+"'), ('"+descricao+"')) ");
  854. conn1.commit();
  855. }
  856. }
  857. }
  858. catch (SQLException e) {
  859. e.printStackTrace();
  860. }
  861.  
  862. if (flag) {
  863. return mensagem;
  864. }
  865. else {
  866. return "error";
  867. }
  868.  
  869. }
  870.  
  871. public String eleicoesPassadas() throws SQLException, RemoteException, ParseException {
  872.  
  873. String query = "SELECT eleicoespassadas.cod_eleicao FROM eleicoespassadas";
  874. int item_count = 0;
  875. boolean flag = false;
  876. String mensagemInicial = "type | item_list; item_count | ";
  877. String mensagem = "";
  878. int i=0;
  879.  
  880. Connection conn1 = getConnection();
  881.  
  882. try {
  883. java.sql.Statement stat = conn1.createStatement();
  884. java.sql.Statement stat1 = conn1.createStatement();
  885. ResultSet rs = stat.executeQuery(query);
  886.  
  887. while(rs.next()) {
  888.  
  889. int cod_eleicao = rs.getInt(1);
  890. String query1 = "SELECT num_brancos,num_nulos,num_validos,cod_lista, cod_eleicao FROM num_validos WHERE num_validos.cod_eleicao = ('"+cod_eleicao+"') ";
  891. ResultSet rs1 = stat1.executeQuery(query1);
  892.  
  893. while (rs1.next()) {
  894.  
  895. int total = 0;
  896. int num_brancos = rs1.getInt(1);
  897. int num_nulos = rs1.getInt(2);
  898. int num_validos = rs1.getInt(3);
  899. int cod_lista = rs1.getInt(4);
  900. total = num_brancos + num_nulos + num_validos;
  901. item_count = item_count + 1;
  902. num_brancos = (num_brancos * total) / 100;
  903. num_nulos = (num_nulos * total) / 100;
  904. num_validos = (num_validos * total) / 100;
  905. mensagem = mensagem + " item_"+i+"cod_lista | "+cod_lista+"; cod_eleicao | "+cod_eleicao+"; num_brancos | "+num_brancos+"; num_nulos | "+num_nulos+"; num_validos | "+num_validos+";";
  906. flag = true;
  907.  
  908. }
  909. mensagemInicial = mensagemInicial + item_count + ";"+ mensagem;
  910. }
  911.  
  912.  
  913. }
  914. catch (SQLException e){
  915. e.printStackTrace();
  916. }
  917.  
  918.  
  919. if (flag) {
  920. System.out.println(mensagemInicial);
  921. return mensagemInicial;
  922. }
  923. else {
  924. return "error";
  925. }
  926.  
  927. }
  928.  
  929. public java.util.Date datasistema() throws RemoteException, ParseException {
  930. java.util.Date date = Calendar.getInstance().getTime();
  931. java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm"); //DATE DO COMPUTADOR
  932.  
  933. return (date);
  934.  
  935. }
  936.  
  937. public java.util.Date conversao(String data) throws RemoteException, ParseException {
  938.  
  939. DateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm");
  940. java.util.Date date1 = formatter.parse(data); //STRING PARA DATE DE UMA HORA FORNECIDA
  941.  
  942. return (java.util.Date)(date1);
  943.  
  944. }
  945.  
  946. public static void main(String[] args) throws RemoteException, SQLException {
  947.  
  948. RMI server = new RMI();
  949. server.RMI(1098);
  950.  
  951.  
  952. }
  953.  
  954. public void RMI(int port) throws RemoteException, SQLException {
  955.  
  956. try {
  957. rmi = new RMI();
  958. LocateRegistry.createRegistry(port).rebind("inter", (Remote) rmi);
  959. System.out.println("Server ready...");
  960.  
  961.  
  962. }catch (Exception e){
  963. System.err.println("ERRO: RMI " + e.toString());
  964. }
  965.  
  966. }
  967.  
  968.  
  969. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement