Advertisement
Guest User

Untitled

a guest
Oct 27th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 33.97 KB | None | 0 0
  1. package adminPackage;
  2.  
  3. import java.awt.*;
  4. import java.rmi.NotBoundException;
  5. import java.rmi.RemoteException;
  6. import java.rmi.registry.LocateRegistry;
  7. import java.text.SimpleDateFormat;
  8. import java.util.ArrayList;
  9. import java.util.Calendar;
  10. import java.util.Date;
  11. import java.util.Scanner;
  12.  
  13. import RMIPackage.*;
  14. import com.sun.org.apache.regexp.internal.RE;
  15.  
  16. public class Admin {
  17. private static Scanner input;
  18.  
  19. public static void main(String[] args){
  20. String hostname;
  21. int def_port;
  22. input = new Scanner(System.in);
  23. String choice;
  24.  
  25. if (args.length == 2){
  26. hostname = args[0];
  27. def_port = Integer.parseInt(args[1]);
  28. }
  29. else{
  30. hostname = "localhost";
  31. def_port = 6500;
  32. }
  33.  
  34. try{
  35. VotingAdminInterface vote = (VotingAdminInterface) LocateRegistry.getRegistry(def_port).lookup("vote_booth");
  36. elecCheck checkThread = new elecCheck(vote);
  37. boothCheck boothThread = new boothCheck(vote);
  38. checkThread.start();
  39. boothThread.start();
  40.  
  41. while(true){
  42. System.out.println("Admin console ready.What do you want to do?\n1-Register a new user\n"
  43. + "2-Add a new department\n3-Create an election\n4-Manage candidate lists"
  44. + "\n5-Edit an election\n6-Add/Remove tables\n7-Check user history\n8-Check Election History");
  45. choice = input.nextLine();
  46.  
  47. switch(choice) {
  48. case "1":
  49. // Informação user
  50. String name;
  51. String ID;
  52. String myDate;
  53. String phone;
  54. int profession;
  55. String department;
  56. String password;
  57.  
  58. System.out.print("Register requested.\n");
  59.  
  60. // Nome
  61. System.out.print("Name: ");
  62. name = input.nextLine();
  63.  
  64. // No. ID
  65. System.out.print("\nID: ");
  66. ID = input.nextLine();
  67.  
  68. // Data expiração ID
  69. System.out.print("\nExpiration Date(dd-MM-yyy hh:mm:ss):");
  70. myDate = input.nextLine();
  71. SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
  72. Date expDate = sdf.parse(myDate);
  73. Calendar cal = Calendar.getInstance();
  74. cal.setTime(expDate);
  75.  
  76. // No. Telefone
  77. System.out.print("\nPhone number: ");
  78. phone = input.nextLine();
  79.  
  80. // Profissão
  81. System.out.print("\nProfession (1-Student, 2-Professor, 3- Employee): ");
  82. profession = Integer.parseInt(input.nextLine());
  83.  
  84. // Departamento
  85. System.out.print("\nDepartment: ");
  86. department = input.nextLine();
  87.  
  88. // Password
  89. System.out.print("\nPassword: ");
  90. password = input.nextLine();
  91.  
  92. User user = new User(name, ID, cal, phone, profession, department, password);
  93.  
  94. int failed = 0;
  95. for (int i = 0; i < 10; i++) {
  96. try {
  97. boolean ack = vote.registerUser(user);
  98. if (ack) {
  99. System.out.println("Successfully registered!");
  100. } else {
  101. System.out.println("Error: Couldn't register new user...");
  102. }
  103. break;
  104. } catch (Exception e) {
  105. failed++;
  106. if (failed == 10) {
  107. System.out.println("RMI Timeout on User registry, trying to reconnect");
  108. try {
  109. Thread.sleep(1000);
  110. vote = (VotingAdminInterface) LocateRegistry.getRegistry(6500).lookup("vote_booth");
  111. } catch (Exception e2) {
  112. System.out.println("RMI server not responding.");
  113. break;
  114. }
  115. }
  116. Thread.sleep(3000);
  117. }
  118. }
  119. break;
  120.  
  121. case "2":
  122. String depName;
  123. String depID;
  124. String facName;
  125. System.out.println("\nAdd a new department:");
  126. // Nome departamento
  127. System.out.print("\nDepartment Name: ");
  128. depName = input.nextLine();
  129.  
  130. // ID
  131. System.out.print("\nDepartment ID: ");
  132. depID = input.nextLine();
  133.  
  134. // Faculdade
  135. System.out.print("\nFaculty name: ");
  136. facName = input.nextLine();
  137.  
  138. Department dep = new Department(depName, depID, facName);
  139.  
  140. boolean newDepAck = vote.registerDep(dep);
  141.  
  142. if (newDepAck) {
  143. System.out.println("New department added!");
  144. }
  145. else {
  146. System.out.println("Error adding the new department...");
  147. }
  148. break;
  149. case "3":
  150.  
  151. int type;
  152. String title;
  153. String description;
  154. String date;
  155. Date startDate;
  156. Date endDate;
  157.  
  158. System.out.println("\nCreate an election");
  159.  
  160. System.out.print("\nElection type(1-Student Association 2- General Council: ");
  161. type = Integer.parseInt(input.nextLine());
  162.  
  163. System.out.print("\nTitle: ");
  164. title = input.nextLine();
  165.  
  166. System.out.print("\nDescription: ");
  167. description = input.nextLine();
  168.  
  169. System.out.print("\nStart date (dd-MM-yyy hh:mm:ss): ");
  170. date = input.nextLine();
  171. SimpleDateFormat sdf2 = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
  172. startDate = sdf2.parse(date);
  173. Calendar cal2 = Calendar.getInstance();
  174. cal2.setTime(startDate);
  175.  
  176. System.out.print("\nEnd date (dd-MM-yyy hh:mm:ss): ");
  177. date = input.nextLine();
  178. SimpleDateFormat sdf3 = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
  179. endDate = sdf3.parse(date);
  180. Calendar cal3 = Calendar.getInstance();
  181. cal3.setTime(endDate);
  182.  
  183. if (type == 1) {
  184.  
  185. System.out.print("Student election - Department(id): ");
  186. depID = input.nextLine();
  187. System.out.print("\nViable lists: \n");
  188.  
  189. ArrayList<candidateList> available;
  190.  
  191. available = vote.getList(1);
  192.  
  193. for (int i = 0; i < available.size(); i++) {
  194. System.out.println("Title: " + available.get(i).getName() + " ID: " + available.get(i).getID());
  195. }
  196.  
  197. System.out.println("Input the ID of the lists you want to add (0 to stop):");
  198.  
  199. ArrayList<candidateList> toAdd = new ArrayList<candidateList>();
  200. candidateList nullVote = new candidateList("NULLVOTE", "NULLVOTE", 1, null);
  201. candidateList blankVote = new candidateList("BLANKVOTE", "BLANKVOTE", 1, null);
  202. toAdd.add(nullVote);
  203. toAdd.add(blankVote);
  204. String IDchoice = input.nextLine();
  205. while (!IDchoice.equals("0")) {
  206. for (int i = 0; i < available.size(); i++) {
  207. if (IDchoice.equals(available.get(i).getID())) {
  208. toAdd.add(available.get(i));
  209. }
  210. }
  211. IDchoice = input.nextLine();
  212. }
  213.  
  214.  
  215. Election election = new Election(title, description, cal2, cal3, type, toAdd);
  216.  
  217. failed = 0;
  218. for (int i = 0; i < 10; i++) {
  219. try {
  220. boolean studElecAdd = vote.newElection(election);
  221. if (studElecAdd) {
  222. System.out.println("Successfully created the election!");
  223. System.out.print("Add voting tables (by dep ID) to the election (0 to stop): ");
  224. ArrayList<String> depTables = new ArrayList<String>();
  225. String depId = input.nextLine();
  226. while (!depId.equals("0")) {
  227. depTables.add(depId);
  228. depId = input.nextLine();
  229. }
  230.  
  231. boolean boothAck = vote.addBooth(election.getTitle(), depTables);
  232.  
  233. if (boothAck) {
  234. System.out.println("Booths added successfully");
  235. } else {
  236. System.out.println("Error adding booths.");
  237. }
  238. break;
  239. } else {
  240. System.out.println("Error creating election...");
  241. break;
  242. }
  243. } catch (Exception e) {
  244. failed++;
  245. if (failed == 10) {
  246. System.out.println("RMI Timeout on election creation, trying to reconnect");
  247. try {
  248. Thread.sleep(1000);
  249. vote = (VotingAdminInterface) LocateRegistry.getRegistry(6500).lookup("vote_booth");
  250. } catch (Exception e2) {
  251. System.out.println("RMI server not responding.");
  252. break;
  253. }
  254. }
  255. Thread.sleep(3000);
  256. }
  257. }
  258. break;
  259. } else {
  260.  
  261. System.out.println("Council election");
  262. System.out.println("Viable lists: ");
  263.  
  264. ArrayList<candidateList> available = new ArrayList<candidateList>();
  265.  
  266. available = vote.getList(2);
  267.  
  268. for (int i = 0; i < available.size(); i++) {
  269. System.out.println("Title: " + available.get(i).getName() + " ID: " + available.get(i).getID());
  270. }
  271.  
  272. System.out.println("Input the ID of the lists you want to add:");
  273.  
  274. ArrayList<candidateList> toAdd = new ArrayList<candidateList>();
  275.  
  276. candidateList nullVote = new candidateList("NULLVOTE", "NULLVOTE", 1, null);
  277. candidateList blankVote = new candidateList("BLANKVOTE", "BLANKVOTE", 1, null);
  278. toAdd.add(nullVote);
  279. toAdd.add(blankVote);
  280.  
  281. String IDchoice = input.nextLine();
  282. while (!IDchoice.equals("0")) {
  283. for (int i = 0; i < available.size(); i++) {
  284. if (IDchoice.equals(available.get(i).getID())) {
  285. toAdd.add(available.get(i));
  286. }
  287. }
  288. IDchoice = input.nextLine();
  289. }
  290.  
  291. Election election = new Election(title, description, cal2, cal3, type, toAdd);
  292. failed = 0;
  293. try {
  294. boolean genElecAdd = vote.newElection(election);
  295.  
  296. if (genElecAdd) {
  297. System.out.println("Successfully created the election!");
  298. System.out.print("Add voting tables (by dep ID) to the election (0 to stop): ");
  299. ArrayList<String> depTables = new ArrayList<String>();
  300. String depId = input.nextLine();
  301. while (!depId.equals("0")) {
  302. depTables.add(depId);
  303. depId = input.nextLine();
  304. }
  305.  
  306. boolean boothAck = vote.addBooth(election.getTitle(), depTables);
  307.  
  308. if (boothAck) {
  309. System.out.println("Booths added successfully");
  310. } else {
  311. System.out.println("Error adding booths.");
  312. }
  313. break;
  314. } else {
  315. System.out.println("Error creating election...");
  316. break;
  317. }
  318. } catch (Exception e) {
  319. failed++;
  320. if (failed == 10) {
  321. System.out.println("RMI Timeout on election creation, trying to reconnect");
  322. try {
  323. Thread.sleep(1000);
  324. vote = (VotingAdminInterface) LocateRegistry.getRegistry(6500).lookup("vote_booth");
  325. } catch (Exception e2) {
  326. System.out.println("RMI server not responding.");
  327. break;
  328. }
  329. }
  330. Thread.sleep(3000);
  331. }
  332. }
  333. break;
  334. case "4":
  335. System.out.println("Manage candidate lists");
  336.  
  337. System.out.print("1-Create new list\n2-Delete a list\n3-Edit a list\nInput: ");
  338.  
  339. choice = input.nextLine();
  340.  
  341. switch (choice) {
  342.  
  343. case "1":
  344.  
  345. System.out.println("Create a new list");
  346. System.out.println("List type? 1- Student,2 - Teacher,3 - Employees");
  347. int listType = Integer.parseInt(input.nextLine());
  348.  
  349. if (listType == 1) {
  350. System.out.println("Student list creation\nPlease input the IDs of students to add to the list(0 to exit):");
  351. ArrayList<User> studentList = new ArrayList<User>();
  352. String studentId = input.nextLine();
  353. while (!studentId.equals("0")) {
  354. User newStudent = vote.findId(studentId, 1);
  355. if (newStudent.getID().equals("Bad id")) {
  356. System.out.println("No student with such id");
  357. } else {
  358. studentList.add(newStudent);
  359. }
  360. studentId = input.nextLine();
  361. }
  362.  
  363. System.out.print("List Name: ");
  364. String listName = input.nextLine();
  365. System.out.print("\nList ID: ");
  366. String listID = input.nextLine();
  367.  
  368. candidateList cl = new candidateList(listName, listID, 1, studentList);
  369.  
  370. failed = 0;
  371. for (int i = 0; i < 10; i++) {
  372. try {
  373.  
  374. boolean ackCandidate = vote.createList(cl);
  375.  
  376. if (ackCandidate) {
  377. System.out.println("List successfully created!");
  378. } else {
  379. System.out.println("Problem creating the list...");
  380. }
  381. } catch (Exception e) {
  382. failed++;
  383. if (failed == 10) {
  384. System.out.println("RMI Timeout on election creation, trying to reconnect");
  385. try {
  386. Thread.sleep(1000);
  387. vote = (VotingAdminInterface) LocateRegistry.getRegistry(6500).lookup("vote_booth");
  388. } catch (Exception e2) {
  389. System.out.println("RMI server not responding.");
  390. break;
  391. }
  392. }
  393. Thread.sleep(3000);
  394. }
  395. break;
  396. }
  397. break;
  398. } else if (listType == 2) {
  399.  
  400. System.out.println("Teacher list creation\nPlease input the IDs of teachers to add to the list(0 to exit):");
  401. ArrayList<User> teacherList = new ArrayList<User>();
  402. String teacherId = input.nextLine();
  403. while (!teacherId.equals("0")) {
  404. User newTeacher = vote.findId(teacherId, 2);
  405. if (newTeacher.getID().equals("Bad id")) {
  406. System.out.println("No teacher with such id");
  407. } else {
  408. teacherList.add(newTeacher);
  409. }
  410. teacherId = input.nextLine();
  411. }
  412.  
  413. System.out.print("List Name: ");
  414. String listName = input.nextLine();
  415. System.out.print("\nList ID: ");
  416. String listID = input.nextLine();
  417.  
  418. candidateList cl = new candidateList(listName, listID, 2, teacherList);
  419.  
  420. boolean ackCandidate = vote.createList(cl);
  421.  
  422. if (ackCandidate) {
  423. System.out.println("List successfully created!");
  424. } else {
  425. System.out.println("Problem creating the list...");
  426. }
  427.  
  428. } else {
  429.  
  430. System.out.println("Employee list creation\nPlease input the IDs of employees to add to the list(0 to exit):");
  431. ArrayList<User> employeeList = new ArrayList<User>();
  432. String employeeId = input.nextLine();
  433. while (!employeeId.equals("0")) {
  434. User newEmployee = vote.findId(employeeId, 3);
  435. if (newEmployee.getID().equals("Bad id")) {
  436. System.out.println("No employee with such id");
  437. } else {
  438. employeeList.add(newEmployee);
  439. }
  440. employeeId = input.nextLine();
  441. }
  442.  
  443. System.out.print("List Name: ");
  444. String listName = input.nextLine();
  445. System.out.print("\nList ID: ");
  446. String listID = input.nextLine();
  447.  
  448. candidateList cl = new candidateList(listName, listID, 3, employeeList);
  449.  
  450. boolean ackCandidate = vote.createList(cl);
  451.  
  452. if (ackCandidate) {
  453. System.out.println("List successfully created!");
  454. } else {
  455. System.out.println("Problem creating the list...");
  456. }
  457.  
  458. }
  459. break;
  460.  
  461. case "2":
  462.  
  463. System.out.println("Delete a candidate list");
  464. System.out.print("Input the ID of the list to delete: ");
  465. String listID = input.nextLine();
  466.  
  467. boolean deleteAck = vote.deleteList(listID);
  468.  
  469. if (deleteAck) {
  470. System.out.println("List deleted successfully!");
  471. } else {
  472. System.out.println("Error deleting list");
  473. }
  474.  
  475. break;
  476. case "3":
  477. System.out.println("Edit a candidate list name");
  478. System.out.print("Input the ID of the list to edit: ");
  479. String listId = input.nextLine();
  480. System.out.print("Input new list name: ");
  481. String newName = input.nextLine();
  482. boolean editAck = vote.editList(listId, newName);
  483. if (editAck) {
  484. System.out.println("List name edited successfully!");
  485. } else {
  486. System.out.println("Error editing name");
  487. }
  488. break;
  489. }
  490. break;
  491.  
  492. case "5":
  493.  
  494. System.out.println("Edit election");
  495. System.out.print("Insert the title of the election to edit: ");
  496. String oldElecName = input.nextLine();
  497. Election oldElec = vote.getElection(oldElecName);
  498. if (!oldElec.getTitle().equals(null)) {
  499.  
  500.  
  501. //Verifica se esta a decorrer
  502. if (oldElec.getStartDate().before(Calendar.getInstance())) {
  503. System.out.println("Election already ongoing (or closed), cannot edit");
  504. break;
  505. } else {
  506. System.out.print("\nInsert new election title: ");
  507. oldElec.setTitle(input.nextLine());
  508.  
  509. System.out.print("\nInsert new election description: ");
  510. oldElec.setDescription(input.nextLine());
  511.  
  512. System.out.print("\nSet new start date: ");
  513. date = input.nextLine();
  514. SimpleDateFormat sdf4 = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
  515. startDate = sdf4.parse(date);
  516. Calendar cal4 = Calendar.getInstance();
  517. cal4.setTime(startDate);
  518. oldElec.setStartDate(cal4);
  519.  
  520. System.out.print("\nSet new end date: ");
  521. date = input.nextLine();
  522. SimpleDateFormat sdf5 = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
  523. endDate = sdf5.parse(date);
  524. Calendar cal5 = Calendar.getInstance();
  525. cal5.setTime(endDate);
  526. oldElec.setEndDate(cal5);
  527.  
  528. //Substitui no rmi
  529. boolean editElec = vote.editElec(oldElec);
  530.  
  531. if (editElec) {
  532. System.out.println("Election edited successfully");
  533. } else {
  534. System.out.println("Error editing the election...");
  535. }
  536. }
  537.  
  538. } else {
  539. System.out.println("Election with that title doesn't exist");
  540. break;
  541. }
  542. break;
  543. case "6":
  544.  
  545. System.out.println("Voting table menu:\n1-Add new table to an election\n2-Remove table from election");
  546.  
  547. choice = input.nextLine();
  548.  
  549. switch (choice) {
  550.  
  551. case"1":
  552.  
  553. System.out.println("Input the title of the election you want to add voting booths to:");
  554.  
  555. String elTitle = input.nextLine();
  556. System.out.print("Add voting tables (by dep ID) to the election (0 to stop): ");
  557. ArrayList<String> depTables = new ArrayList<String>();
  558. String depId = input.nextLine();
  559. while (!depId.equals("0")) {
  560. depTables.add(depId);
  561. depId = input.nextLine();
  562. }
  563.  
  564. boolean boothAck = vote.addBooth(elTitle, depTables);
  565.  
  566. if (boothAck) {
  567. System.out.println("Booths added successfully");
  568. } else {
  569. System.out.println("Error adding booths.");
  570. }
  571. break;
  572.  
  573. default:
  574. System.out.println("Invalid choice, going back to menu");
  575. break;
  576. }
  577. break;
  578. }
  579. break;
  580. }
  581. }catch (Exception e) {
  582. System.out.println("Exception in main: " + e);
  583. e.printStackTrace();
  584. }
  585. }
  586. }
  587.  
  588.  
  589. class elecCheck extends Thread{
  590. private VotingAdminInterface vote;
  591. private ArrayList <Election> seen = new ArrayList <Election>();
  592. private ArrayList<Integer> printable = new ArrayList<Integer>();
  593.  
  594. public elecCheck(VotingAdminInterface vote){
  595. this.vote = vote;
  596. }
  597.  
  598. public void run() {
  599. System.out.println("ELECTION THREAD: Checking for expired elections");
  600. int failed=0;
  601. while (true) {
  602. try {
  603. try {
  604. Thread.sleep(5000);
  605. } catch (InterruptedException ex) {
  606. Thread.currentThread().interrupt();
  607. }
  608. ArrayList<Election> expired = vote.checkElecDate();
  609. boolean checked=false;
  610. Election toAdd = null;
  611. for (int i = 0; i < expired.size(); i++) {
  612. for (int j = 0; j < seen.size(); j++) {
  613. if (expired.get(i).getTitle().equals(seen.get(j).getTitle())) {
  614. checked = true;
  615. }
  616. }
  617. if(checked==false){
  618. toAdd = expired.get(i);
  619. printable.add(0);
  620. seen.add(toAdd);
  621. }
  622. checked = false;
  623. }
  624.  
  625. for(int i=0;i <seen.size();i++){
  626. if(printable.get(i)==0){
  627. printable.set(i,1);
  628. System.out.println("Election expired - " + toAdd.getTitle());
  629. }
  630. }
  631.  
  632. failed = 0;
  633. } catch (RemoteException e) {
  634. failed++;
  635. if(failed == 3){
  636. System.out.println("Timeout - RMI didn't respond for 30 seconds, trying to reconnect...");
  637. try{
  638. this.vote = (VotingAdminInterface) LocateRegistry.getRegistry(6500).lookup("vote_booth");
  639. } catch (Exception e2){
  640. System.out.println("RMI server not responding, shutting down thread");
  641. break;
  642. }
  643. }
  644. }
  645. }
  646. }
  647. }
  648.  
  649. class boothCheck extends Thread {
  650. private VotingAdminInterface vote;
  651.  
  652. private ArrayList <Department> seen2 = new ArrayList <Department>();
  653. private ArrayList<Integer> printable2 = new ArrayList<Integer>();
  654.  
  655. public boothCheck(VotingAdminInterface vote) {
  656. this.vote = vote;
  657. }
  658.  
  659. public void run() {
  660. System.out.println("Booth checking thread started.");
  661. int failed = 0;
  662. while(true){
  663. try {
  664. try {
  665. Thread.sleep(5000);
  666. } catch (InterruptedException ex) {
  667. Thread.currentThread().interrupt();
  668. }
  669. ArrayList<Department> tables = vote.checkTables();
  670. boolean checked=false;
  671. Department toAdd = null;
  672. //Check for new tables
  673. for (int i = 0; i < tables.size(); i++) {
  674. for (int j = 0; j < seen2.size(); j++) {
  675. if (tables.get(i).getID().equals(seen2.get(j).getID())) {
  676. checked = true;
  677. }
  678. }
  679. if(!checked){
  680. toAdd = tables.get(i);
  681. printable2.add(0);
  682. seen2.add(toAdd);
  683. }
  684. checked = false;
  685. }
  686.  
  687. for(int i=0;i<seen2.size();i++){
  688. System.out.println(i);
  689. if(printable2.get(i)==0){
  690. printable2.set(i,1);
  691. System.out.println("Voting table added in " + seen2.get(i).getDep());
  692. }
  693. }
  694.  
  695. //Check for deleted tables
  696. checked = false;
  697. for(int i=0;i<seen2.size();i++){
  698. for(int j=0;j<tables.size();j++){
  699. if(seen2.get(i).getID().equals(tables.get(j).getID())) {
  700. checked = true;
  701. }
  702. }
  703. if(!checked){
  704. System.out.println("Voting table removed in "+seen2.get(i).getDep());
  705. seen2.remove(i);
  706. }
  707. checked = false;
  708. }
  709. failed = 0;
  710. }catch(RemoteException e){
  711. failed++;
  712. if(failed == 3){
  713. System.out.println("Timeout - RMI didn't respond for 30 seconds, trying to reconnect...");
  714. try{
  715. this.vote = (VotingAdminInterface) LocateRegistry.getRegistry(6500).lookup("vote_booth");
  716. } catch (Exception e2){
  717. System.out.println("RMI server not responding, shutting down thread");
  718. break;
  719. }
  720. }
  721. }
  722. }
  723. }
  724. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement