Advertisement
Guest User

Untitled

a guest
Dec 13th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 34.92 KB | None | 0 0
  1. import com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException;
  2. import org.joda.time.DateTime;
  3.  
  4. import java.io.*;
  5. import java.lang.reflect.Array;
  6. import java.rmi.*;
  7. import java.rmi.registry.LocateRegistry;
  8. import java.rmi.registry.Registry;
  9. import java.rmi.server.ExportException;
  10. import java.rmi.server.UnicastRemoteObject;
  11. import java.sql.*;
  12. import java.sql.Connection;
  13. import java.util.*;
  14.  
  15. /**
  16. * Created by joel on 10/11/15.
  17. */
  18. public class DataServer extends UnicastRemoteObject implements Data {
  19. public static Connection connection = null;
  20. public static PreparedStatement pstmt;
  21. public static ResultSet resultSet;
  22. public static Ghost ghost;
  23. public static int registryPort;
  24. public static String mysqlHost;
  25. public static String mysqlUsername;
  26. public static String mysqlpassword;
  27.  
  28. public DataServer() throws RemoteException {
  29. super();
  30. }
  31.  
  32. public ArrayList<Project> listActiveProdjects(Request request) throws RemoteException{
  33.  
  34. try{
  35. ArrayList<Project> arr = new ArrayList<>();
  36. pstmt = connection.prepareStatement("SELECT name, headval, actval FROM prodjects WHERE done=?");
  37. pstmt.setInt(1,0);
  38. ResultSet rs = pstmt.executeQuery();
  39. while(rs.next()){
  40. Project temp = new Project(rs.getString(1), rs.getInt(2), rs.getInt(3));
  41. arr.add(temp);
  42. }
  43. return arr;
  44. }catch(SQLException e) {
  45. System.out.println("Returned exception: " + e);
  46. return null;
  47. }
  48. }
  49.  
  50. public ArrayList<Project> listOldProdjects(Request request) throws RemoteException{
  51. try{
  52. ArrayList<Project> arr = new ArrayList<>();
  53. pstmt = connection.prepareStatement("SELECT name, headval, actval, success FROM prodjects WHERE done=?");
  54. pstmt.setInt(1,1);
  55. ResultSet rs = pstmt.executeQuery();
  56. while(rs.next()){
  57. Project temp = new Project(rs.getString(1), rs.getInt(2), rs.getInt(3), rs.getInt(4));
  58. arr.add(temp);
  59. }
  60. return arr;
  61. }catch(SQLException e) {
  62. System.out.println("Returned exception: " + e);
  63. return null;
  64. }
  65. }
  66.  
  67. public Project consultProdject(Request request) throws RemoteException{
  68. try{
  69. //get project name, deadline and description from table prodjects
  70. pstmt = connection.prepareStatement("SELECT name, deadline, description, pid FROM prodjects WHERE name=?");
  71. pstmt.setString(1, (String)request.requestObj);
  72. ResultSet rs = pstmt.executeQuery();
  73. rs.next();
  74. String name = rs.getString(1);
  75. String deadline = rs.getString(2);
  76. String description = rs.getString(3);
  77. int pid = rs.getInt(4);
  78.  
  79. //get project possible rewards from table recompenses
  80. pstmt = connection.prepareStatement("SELECT description, value FROM recompenses WHERE pid=? AND uid=?");
  81. pstmt.setInt(1,pid);
  82. pstmt.setInt(2, -1);
  83. rs= pstmt.executeQuery();
  84. ArrayList <Recompense> tempRecompenses = new ArrayList<>();
  85. while(rs.next()){
  86. String recompenseDesc = rs.getString(1);
  87. int value = rs.getInt(2);
  88. tempRecompenses.add(new Recompense(value, recompenseDesc));
  89. }
  90.  
  91.  
  92. //get project extra
  93. pstmt = connection.prepareStatement("SELECT description, value FROM extrarecompenses WHERE pid=? AND uid=?");
  94. pstmt.setInt(1,pid);
  95. pstmt.setInt(2,-1);
  96. rs = pstmt.executeQuery();
  97. ArrayList <Recompense> tempExtraRecompenses = new ArrayList<>();
  98. while(rs.next()){
  99. String extraDesc = rs.getString(1);
  100. int extraValue = rs.getInt(2);
  101. tempExtraRecompenses.add(new Recompense(extraValue,extraDesc));
  102. }
  103.  
  104.  
  105. Project proj = new Project(name,
  106. deadline,
  107. description,
  108. tempRecompenses,
  109. tempExtraRecompenses);
  110.  
  111. return proj;
  112.  
  113. }catch(SQLException e){
  114. e.printStackTrace();
  115. return null;
  116. }
  117. }
  118.  
  119. public boolean loginClient(Request request) throws RemoteException {
  120. System.out.println("loginClient");
  121. try {
  122. User user = (User) request.user;
  123. System.out.println("User: "+request.user.username+" pass: "+request.user.password);
  124. pstmt = connection.prepareStatement("SELECT password FROM users WHERE username=?;");
  125. pstmt.setString(1, user.username);
  126. resultSet = pstmt.executeQuery();
  127. while(resultSet.next()) {
  128. String str = resultSet.getString(1);
  129. if(str.equals(user.password)){
  130. return true;
  131. }
  132. }
  133. }catch (SQLException e){e.printStackTrace();}
  134. return false;
  135. }
  136.  
  137. public boolean registerClient(Request request) throws RemoteException{
  138. System.out.println("registerClient");
  139. try {
  140. User user = (User) request.user;
  141. pstmt = connection.prepareStatement("INSERT INTO users(username, password) VALUES (?, ?);");
  142. pstmt.setString(1, user.username);
  143. pstmt.setString(2, user.password);
  144. pstmt.executeUpdate();
  145. }catch (SQLException e){
  146. return false;
  147. }
  148. return true;
  149. }
  150.  
  151. public int getSaldo(Request request) throws RemoteException{
  152. System.out.println("getSaldo");
  153. try {
  154. User user = (User) request.requestObj;
  155. String username=user.username;
  156. pstmt = connection.prepareStatement("SELECT password, saldo FROM users WHERE username=?;");
  157. pstmt.setString(1, username);
  158. resultSet = pstmt.executeQuery();
  159. while(resultSet.next()) {
  160. String str = resultSet.getString(1);
  161. int saldo = resultSet.getInt(2);
  162. if(str.equals(user.password)){
  163. return saldo;
  164. }
  165. }
  166. }catch (SQLException e){e.printStackTrace();}
  167. return -1;
  168. }
  169.  
  170. public ArrayList <Recompense> getRecompenses(Request request) throws RemoteException{
  171. System.out.println("getRecompenses");
  172. try{
  173. User usernameTemp = (User) request.requestObj;
  174. ArrayList <Recompense> arr = new ArrayList<>();
  175. pstmt = connection.prepareStatement("SELECT uid FROM users WHERE username=?;");
  176. pstmt.setString(1, usernameTemp.username);
  177. resultSet = pstmt.executeQuery();
  178. int uid = -1;
  179. resultSet.next();
  180. uid = resultSet.getInt(1);
  181. pstmt = connection.prepareStatement("SELECT value, description, delivered FROM recompenses WHERE uid=?;");
  182. pstmt.setInt(1, uid);
  183. resultSet = pstmt.executeQuery();
  184. while(resultSet.next()){
  185. arr.add(new Recompense(resultSet.getInt(1), resultSet.getString(2), resultSet.getInt(3)));
  186. }
  187. return arr;
  188. }catch (SQLException e){
  189. return null;
  190. }
  191. }
  192.  
  193. public User getProjID(Request request){
  194. try{
  195. User userTemp = new User();
  196. String temp = (String) request.requestObj;
  197. pstmt = connection.prepareStatement("SELECT pid FROM prodjects WHERE name=?;");
  198. pstmt.setString(1, temp);
  199. resultSet = pstmt.executeQuery();
  200. resultSet.next();
  201. int pid = resultSet.getInt(1);
  202.  
  203. pstmt = connection.prepareStatement("SELECT pid, uid FROM admins WHERE pid=?");
  204. pstmt.setInt(1,pid);
  205. resultSet = pstmt.executeQuery();
  206. resultSet.next();
  207. int uid = resultSet.getInt(2);
  208.  
  209. pstmt = connection.prepareStatement("SELECT uid, username FROM users WHERE uid=?");
  210. pstmt.setInt(1,uid);
  211. resultSet = pstmt.executeQuery();
  212. resultSet.next();
  213. String ola= resultSet.getString(2);
  214. System.out.println("USERNAM GETPROJID IN FUNCTION"+ola);
  215.  
  216. return userTemp;
  217.  
  218. }catch(SQLException e){
  219. System.out.println("SQL exception: " + e);
  220. return null;
  221. }
  222.  
  223. }
  224.  
  225. public String[] getProjChoices(Request request){
  226. try{
  227. String temp = (String) request.requestObj;
  228. pstmt = connection.prepareStatement("SELECT choice1, choice2 FROM prodjects WHERE name=?");
  229. pstmt.setString(1, temp);
  230. resultSet = pstmt.executeQuery();
  231. resultSet.next();
  232. String choice1 = resultSet.getString(1);
  233. String choice2 = resultSet.getString(2);
  234. String[] tempArray = new String[2];
  235. tempArray[0] = choice1;
  236. tempArray[1] = choice2;
  237. return tempArray;
  238.  
  239. }catch(SQLException e){
  240. System.out.println("SQL exception: " + e);
  241. return null;
  242. }
  243. }
  244.  
  245. public boolean pledge(Request request) throws RemoteException{
  246. System.out.println("pledge");
  247. try{
  248. String timestampStr = "";
  249. Pledge pledge = (Pledge) request.requestObj;
  250. //CHECKING IF INTERRUPTED
  251.  
  252. pstmt = connection.prepareStatement("SELECT pid FROM prodjects WHERE name=?");
  253. pstmt.setString(1, pledge.name);
  254. resultSet = pstmt.executeQuery();
  255. resultSet.next();
  256. int pid = resultSet.getInt(1);
  257.  
  258. pstmt = connection.prepareStatement("SELECT timest FROM users WHERE timest=?");
  259. pstmt.setString(1, request.timeStamp.toString());
  260. resultSet = pstmt.executeQuery();
  261. if(resultSet.next()){
  262. pstmt = connection.prepareStatement("UPDATE users SET saldo = saldo - ?, timest = ? WHERE username=?;");
  263. pstmt.setInt(1, pledge.value);
  264. pstmt.setString(2, "-1");
  265. pstmt.setString(3, pledge.user.username);
  266. pstmt.executeUpdate();
  267.  
  268. if (pledge.choice == 1) {
  269. pstmt = connection.prepareStatement("UPDATE prodjects SET timest = ?, actval = actval - ?, choicecount1 = choicecount1 - ? WHERE pid=?;");
  270. } else {
  271. pstmt = connection.prepareStatement("UPDATE prodjects SET timest = ?, actval = actval - ?, choicecount2 = choicecount2 - ? WHERE pid=?;");
  272. }
  273. pstmt.setString(1, "-1");
  274. pstmt.setInt(2, pledge.value);
  275. pstmt.setInt(3, pledge.value);
  276. pstmt.setInt(4, pid);
  277. pstmt.executeUpdate();
  278. }
  279.  
  280. pstmt = connection.prepareStatement("DELETE FROM pledge WHERE timest=?");
  281. pstmt.setString(1, request.timeStamp.toString());
  282. pstmt.executeUpdate();
  283.  
  284. pstmt = connection.prepareStatement("DELETE FROM recompenses WHERE timest=?");
  285. pstmt.setString(1, request.timeStamp.toString());
  286. pstmt.executeUpdate();
  287.  
  288.  
  289. pstmt = connection.prepareStatement("SELECT saldo FROM users WHERE username=?");
  290. pstmt.setString(1, pledge.user.username);
  291. resultSet = pstmt.executeQuery();
  292. resultSet.next();
  293. int saldo = resultSet.getInt(1);
  294. if(saldo >= pledge.value) {
  295.  
  296. pstmt = connection.prepareStatement("SELECT uid FROM users WHERE username=?");
  297. pstmt.setString(1, pledge.user.username);
  298. resultSet = pstmt.executeQuery();
  299. resultSet.next();
  300. int uid = resultSet.getInt(1);
  301.  
  302. pstmt = connection.prepareStatement("SELECT uid FROM users WHERE username=?");
  303. pstmt.setString(1, pledge.target.username);
  304. resultSet = pstmt.executeQuery();
  305. resultSet.next();
  306. int uidtarget = resultSet.getInt(1);
  307.  
  308. pstmt = connection.prepareStatement("SELECT description, value FROM recompenses WHERE pid=? AND uid=? ORDER BY rid;");
  309. pstmt.setInt(1, pid);
  310. pstmt.setInt(2, -1);
  311. resultSet = pstmt.executeQuery();
  312. String description = "";
  313. int value = -1;
  314. while (resultSet.next()) {
  315. description = resultSet.getString(1);
  316. value = resultSet.getInt(2);
  317. if (value <= pledge.value)
  318. break;
  319. }
  320.  
  321. pstmt = connection.prepareStatement("INSERT INTO pledge(pid, uid, uidtarget, choice, value, timest) VALUES (?, ?, ?, ?, ?, ?);");
  322. pstmt.setInt(1, pid);
  323. pstmt.setInt(2, uid);
  324. pstmt.setInt(3, uidtarget);
  325. pstmt.setInt(4, pledge.choice);
  326. pstmt.setInt(5, pledge.value);
  327. pstmt.setString(6, request.timeStamp.toString());
  328. pstmt.executeUpdate();
  329.  
  330. if (pledge.choice == 1) {
  331. pstmt = connection.prepareStatement("UPDATE prodjects SET timest = ?, actval = actval + ?, choicecount1 = choicecount1 + ? WHERE pid=?;");
  332. } else {
  333. pstmt = connection.prepareStatement("UPDATE prodjects SET timest = ?, actval = actval + ?, choicecount2 = choicecount2 + ? WHERE pid=?;");
  334. }
  335. pstmt.setString(1, request.timeStamp.toString());
  336. pstmt.setInt(2, pledge.value);
  337. pstmt.setInt(3, pledge.value);
  338. pstmt.setInt(4, pid);
  339. pstmt.executeUpdate();
  340.  
  341. pstmt = connection.prepareStatement("UPDATE users SET saldo = saldo - ?, timest = ? WHERE uid=?;");
  342. pstmt.setInt(1, pledge.value);
  343. pstmt.setString(2, request.timeStamp.toString());
  344. pstmt.setInt(3, uid);
  345. pstmt.executeUpdate();
  346.  
  347. if (value != -1) {
  348. pstmt = connection.prepareStatement("INSERT INTO recompenses(pid, uid, value, description, timest) VALUES (?, ?, ?, ?, ?);");
  349. pstmt.setInt(1, pid);
  350. pstmt.setInt(2, uidtarget);
  351. pstmt.setInt(3, value);
  352. pstmt.setString(4, description);
  353. pstmt.setString(5, request.timeStamp.toString());
  354. pstmt.executeUpdate();
  355. }
  356.  
  357. return true;
  358. }else{
  359. return false;
  360. }
  361. }catch (SQLException e){
  362. System.out.println(e);
  363. e.printStackTrace();
  364. return false;
  365. }
  366. }
  367.  
  368. public boolean sendMessage(Request request) throws RemoteException{
  369. System.out.println("Send Message");
  370. try{
  371. String projectName = (String) request.requestObj;
  372. Message tempMessage = (Message) request.requestObj2;
  373.  
  374. pstmt = connection.prepareStatement("SELECT pid, name FROM prodjects WHERE name=?");
  375. pstmt.setString(1, projectName);
  376. ResultSet rs = pstmt.executeQuery();
  377. rs.next();
  378. int pid = rs.getInt(1);
  379.  
  380. String tempUsername = request.user.username;
  381. pstmt = connection.prepareStatement("SELECT username, uid FROM users WHERE username=?");
  382. pstmt.setString(1,tempUsername);
  383. rs = pstmt.executeQuery();
  384. rs.next();
  385. int uid = rs.getInt(2);
  386. System.out.println(uid);
  387.  
  388. pstmt = connection.prepareStatement("INSERT INTO message (pid, uid, qest)" +
  389. "VALUES (?,?,?)");
  390. pstmt.setInt(1,pid);
  391. pstmt.setInt(2,uid);
  392. pstmt.setString(3,tempMessage.quest);
  393. pstmt.executeUpdate();
  394. return true;
  395.  
  396. }catch(SQLException e){
  397. System.out.println("SQL exception: " + e);
  398. return false;
  399. }
  400. }
  401.  
  402. public boolean answerMessage(Request request) throws RemoteException{
  403. System.out.println("Answering message");
  404.  
  405. try{
  406. String projName = (String) request.requestObj;
  407. Message tempMessage = (Message) request.requestObj2;
  408.  
  409. pstmt = connection.prepareStatement("SELECT pid, name FROM prodjects WHERE name=?");
  410. pstmt.setString(1, projName);
  411. ResultSet rs = pstmt.executeQuery();
  412. rs.next();
  413. int pid = rs.getInt(1);
  414.  
  415. pstmt = connection.prepareStatement("SELECT uid FROM users WHERE username=?");
  416. pstmt.setString(1, request.user.username);
  417. rs = pstmt.executeQuery();
  418. rs.next();
  419. int uid = rs.getInt(1);
  420.  
  421. pstmt = connection.prepareStatement("SELECT description FROM admins WHERE uid=? AND pid=?");
  422. pstmt.setInt(1,uid);
  423. pstmt.setInt(2,pid);
  424. rs = pstmt.executeQuery();
  425.  
  426. if(rs.next()){
  427. pstmt = connection.prepareStatement("SELECT pid, uid, qest, mid FROM message WHERE pid=? AND uid=? AND qest=?");
  428. pstmt.setInt(1,pid);
  429. pstmt.setInt(2,uid);
  430. pstmt.setString(3, tempMessage.quest);
  431. rs = pstmt.executeQuery();
  432. rs.next();
  433. int mid = rs.getInt(4);
  434.  
  435. pstmt = connection.prepareStatement("UPDATE message SET answ = ? WHERE mid=?");
  436. pstmt.setString(1,tempMessage.answer);
  437. pstmt.setInt(2, mid);
  438. pstmt.executeUpdate();
  439. return true;
  440. }
  441. return false;
  442.  
  443. }catch(SQLException e){
  444. e.printStackTrace();
  445. return false;
  446. }
  447. }
  448.  
  449. public boolean createProdject(Request request) throws RemoteException{
  450. System.out.println("createProdject");
  451. try {
  452. Project project = (Project) request.requestObj;
  453. pstmt = connection.prepareStatement("INSERT INTO prodjects(" +
  454. "name, deadline, headval, description, choice1, choice2) VALUES (?, ?, ?, ?, ?, ?);");
  455. pstmt.setString(1, project.name);
  456. pstmt.setString(2, project.deadline.toString());
  457. pstmt.setInt(3, project.headVal);
  458. pstmt.setString(4, project.description);
  459. pstmt.setString(5, project.choice1);
  460. pstmt.setString(6, project.choice2);
  461. pstmt.executeUpdate();
  462. int pid = -1;
  463. pstmt = connection.prepareStatement("SELECT pid FROM prodjects WHERE name = ?;");
  464. pstmt.setString(1, project.name);
  465. resultSet = pstmt.executeQuery();
  466. while(resultSet.next()){
  467. pid = resultSet.getInt(1);
  468. }
  469. System.out.println(pid);
  470. if(pid != -1) {
  471. if(project.recompenses != null){
  472. for (int i = 0; i < project.recompenses.size(); i++) {
  473. pstmt = connection.prepareStatement("INSERT INTO recompenses(" +
  474. "pid, uid, value, description) VALUES (?, ?, ?, ?);");
  475. pstmt.setInt(1, pid);
  476. pstmt.setInt(2, -1);
  477. pstmt.setInt(3, project.recompenses.get(i).val);
  478. pstmt.setString(4, project.recompenses.get(i).description);
  479. pstmt.executeUpdate();
  480. }
  481. }
  482.  
  483. if (project.extraRecompenses != null){
  484. for (int i = 0; i < project.extraRecompenses.size(); i++) {
  485. pstmt = connection.prepareStatement("INSERT INTO extrarecompenses(" +
  486. "pid, uid, value, description) VALUES (?, ?, ?, ?);");
  487. pstmt.setInt(1, pid);
  488. pstmt.setInt(2, -1);
  489. pstmt.setInt(3, project.extraRecompenses.get(i).val);
  490. pstmt.setString(4, project.extraRecompenses.get(i).description);
  491. pstmt.executeUpdate();
  492. }
  493. }
  494.  
  495. for (int i = 0; i < project.admins.size(); i++) {
  496. pstmt = connection.prepareStatement("INSERT INTO admins(" +
  497. "pid, uid, description, username) VALUES (?, ?, ?, ?);");
  498. PreparedStatement tmppstmt = connection.prepareStatement("SELECT uid FROM users WHERE username=?;");
  499. tmppstmt.setString(1, project.admins.get(i).username);
  500. resultSet = tmppstmt.executeQuery();
  501. int uid = -1;
  502. while (resultSet.next()) {
  503. uid = resultSet.getInt(1);
  504. }
  505. pstmt.setInt(1, pid);
  506. pstmt.setInt(2, uid);
  507. pstmt.setString(3, project.admins.get(i).description);
  508. pstmt.setString(4, project.admins.get(i).username);
  509. pstmt.executeUpdate();
  510. }
  511.  
  512.  
  513. }
  514. }catch (SQLException e){
  515. System.out.println("SQL exception "+e);
  516. return false;
  517. }
  518. return true;
  519. }
  520.  
  521. public boolean addRecompense(Request request) throws RemoteException{
  522. System.out.println("Add reward!\n");
  523. try{
  524. String name = (String) request.requestObj;
  525. pstmt = connection.prepareStatement("SELECT pid, name FROM prodjects WHERE name=?");
  526. pstmt.setString(1, name);
  527. ResultSet rs = pstmt.executeQuery();
  528. rs.next();
  529. int pid = rs.getInt(1);
  530.  
  531. pstmt = connection.prepareStatement("SELECT uid FROM users WHERE username=?");
  532. pstmt.setString(1, request.user.username);
  533. rs = pstmt.executeQuery();
  534. rs.next();
  535. int uid = rs.getInt(1);
  536.  
  537. pstmt = connection.prepareStatement("SELECT description FROM admins WHERE uid=? AND pid=?");
  538. pstmt.setInt(1,uid);
  539. pstmt.setInt(2,pid);
  540. rs = pstmt.executeQuery();
  541.  
  542. if(rs.next()){
  543. Recompense tempRecompense = (Recompense) request.requestObj2;
  544. pstmt = connection.prepareStatement("INSERT INTO recompenses (pid,uid,value,description)" +
  545. "VALUES (?,?,?,?)");
  546. pstmt.setInt(1,pid);
  547. pstmt.setInt(2,-1);
  548. pstmt.setInt(3, tempRecompense.val);
  549. pstmt.setString(4, tempRecompense.description);
  550. pstmt.executeUpdate();
  551. }
  552.  
  553. else{
  554.  
  555. return false;
  556. }
  557.  
  558. return true;
  559.  
  560. }catch(SQLException e){
  561. e.printStackTrace();
  562. return false;
  563. }
  564. }
  565.  
  566. public boolean removeRecompense(Request request) throws RemoteException{
  567. System.out.println("Remove reward!\n");
  568. try{
  569. String name = (String) request.requestObj;
  570. pstmt = connection.prepareStatement("SELECT pid, name FROM prodjects WHERE name=?");
  571. pstmt.setString(1, name);
  572. ResultSet rs = pstmt.executeQuery();
  573. rs.next();
  574. int pid = rs.getInt(1);
  575.  
  576. pstmt = connection.prepareStatement("SELECT uid FROM users WHERE username=?");
  577. pstmt.setString(1, request.user.username);
  578. rs = pstmt.executeQuery();
  579. rs.next();
  580. int uid = rs.getInt(1);
  581.  
  582. pstmt = connection.prepareStatement("SELECT description FROM admins WHERE uid=? AND pid=?");
  583. pstmt.setInt(1,uid);
  584. pstmt.setInt(2,pid);
  585. rs = pstmt.executeQuery();
  586.  
  587. if(rs.next()){
  588. Recompense tempRecompense = (Recompense) request.requestObj2;
  589. pstmt = connection.prepareStatement("DELETE FROM recompenses WHERE pid =? AND uid=? AND value=? AND description=?");
  590. pstmt.setInt(1,pid);
  591. pstmt.setInt(2,-1);
  592. pstmt.setInt(3,tempRecompense.val);
  593. pstmt.setString(4,tempRecompense.description);
  594. pstmt.executeUpdate();
  595. }
  596.  
  597. else{
  598.  
  599. return false;
  600. }
  601.  
  602. return true;
  603.  
  604. }catch(SQLException e){
  605. e.printStackTrace();
  606. return false;
  607. }
  608. }
  609.  
  610. public boolean cancelProdject(Request request) throws RemoteException{
  611. System.out.println("cancelProdject");
  612. try{
  613. String projectName = (String) request.requestObj;
  614.  
  615. pstmt = connection.prepareStatement("SELECT uid FROM users WHERE username=?;");
  616. pstmt.setString(1, request.user.username);
  617. resultSet = pstmt.executeQuery();
  618. resultSet.next();
  619. int uid = resultSet.getInt(1);
  620.  
  621. pstmt = connection.prepareStatement("SELECT pid FROM prodjects WHERE name=?;");
  622. pstmt.setString(1, projectName);
  623. resultSet = pstmt.executeQuery();
  624. resultSet.next();
  625. int pid = resultSet.getInt(1);
  626.  
  627. pstmt = connection.prepareStatement("SELECT description FROM admins WHERE uid=? and pid=?;");
  628. pstmt.setInt(1, uid);
  629. pstmt.setInt(2, pid);
  630. resultSet = pstmt.executeQuery();
  631. if(resultSet.next()){
  632. pstmt = connection.prepareStatement("SELECT value, uid FROM pledge WHERE pid=?;");
  633. pstmt.setInt(1, pid);
  634. resultSet = pstmt.executeQuery();
  635. while(resultSet.next()){
  636. pstmt = connection.prepareStatement("UPDATE users SET saldo = saldo + ? WHERE uid=?;");
  637. pstmt.setInt(1, resultSet.getInt(1));
  638. pstmt.setInt(2, resultSet.getInt(2));
  639. pstmt.executeUpdate();
  640. }
  641. pstmt = connection.prepareStatement("DELETE FROM recompenses WHERE pid=?");
  642. pstmt.setInt(1, pid);
  643. pstmt.executeUpdate();
  644. pstmt = connection.prepareStatement("DELETE FROM message WHERE pid=?");
  645. pstmt.setInt(1, pid);
  646. pstmt.executeUpdate();
  647. pstmt = connection.prepareStatement("DELETE FROM extrarecompenses WHERE pid=?");
  648. pstmt.setInt(1, pid);
  649. pstmt.executeUpdate();
  650. pstmt = connection.prepareStatement("DELETE FROM admins WHERE pid=?");
  651. pstmt.setInt(1, pid);
  652. pstmt.executeUpdate();
  653. pstmt = connection.prepareStatement("DELETE FROM prodjects WHERE pid=?");
  654. pstmt.setInt(1, pid);
  655. pstmt.executeUpdate();
  656. pstmt = connection.prepareStatement("DELETE FROM pledge WHERE pid=?");
  657. pstmt.setInt(1, pid);
  658. pstmt.executeUpdate();
  659. return true;
  660. }else{
  661. return false;
  662. }
  663.  
  664.  
  665. }catch (SQLException e){
  666. e.printStackTrace();
  667. return false;
  668. }
  669. }
  670.  
  671. public static void main(String[] args) {
  672. setupConfig();
  673. //CONNECTING TO MYSQL SERVER
  674. try {
  675. connection = DriverManager.getConnection(mysqlHost, mysqlUsername, mysqlpassword);
  676. }catch (SQLException e){e.printStackTrace();}
  677. System.out.println("MYSQL connection ready.");
  678. ghost = new Ghost(connection);
  679. //STARTING RMI
  680. try {
  681. DataServer daSer = new DataServer();
  682. Registry loReg = LocateRegistry.createRegistry(registryPort);
  683. loReg.rebind("data", daSer);
  684. } catch (RemoteException re) {
  685. System.out.println("Exception in DataServer.main: " + re);
  686. }
  687. System.out.println("Data Server ready.");
  688. }
  689.  
  690. public static void setupConfig(){
  691. String filename = "dataServerConfig.txt", line;
  692. StringTokenizer tokenizer;
  693. try {
  694. FileReader inputFile = new FileReader(filename);
  695. BufferedReader bufferReader = new BufferedReader(inputFile);
  696. bufferReader.readLine();
  697. for(int i = 0; i < 4; i++) {
  698. line = bufferReader.readLine();
  699. tokenizer = new StringTokenizer(line, "=");
  700. line = tokenizer.nextToken();
  701. if(line.equals("registry port")) {
  702. registryPort = Integer.parseInt(tokenizer.nextToken());
  703. }else if(line.equals("mysql host")) {
  704. mysqlHost = tokenizer.nextToken();
  705. }else if(line.equals("mysql username")) {
  706. mysqlUsername = tokenizer.nextToken();
  707. }else if(line.equals("mysql password")) {
  708. if(tokenizer.hasMoreTokens())
  709. mysqlpassword = tokenizer.nextToken();
  710. else
  711. mysqlpassword = "";
  712. }
  713. }
  714. }catch (FileNotFoundException e){
  715. System.out.println("Client configuration file: '" + filename + "' not found.");
  716. System.exit(0);
  717. }catch (IOException e){}
  718. }
  719. }
  720.  
  721. class Ghost extends Thread implements Serializable{
  722. public static Connection connection;
  723. public static PreparedStatement pstmt;
  724. public static ResultSet resultSet;
  725.  
  726. public Ghost(Connection connection){
  727. this.connection = connection;
  728. this.start();
  729. }
  730.  
  731. public void run(){
  732. while(true){
  733. try{
  734. int pid, owner, headVal, actVal; String deadline; DateTime deadTime, actTime;
  735. //System.out.println("ghost is working...");
  736. pstmt = connection.prepareStatement("SELECT pid, deadline, headval, actval FROM prodjects WHERE done=?;");
  737. pstmt.setInt(1, 0);
  738. resultSet = pstmt.executeQuery();
  739. while(resultSet.next()){
  740. pid = resultSet.getInt(1);
  741. deadline = resultSet.getString(2);
  742. headVal = resultSet.getInt(3);
  743. actVal = resultSet.getInt(4);
  744. deadTime = DateTime.parse(deadline);
  745. actTime = new DateTime();
  746.  
  747. if(deadTime.isBefore(actTime)){
  748. if (actVal >= headVal) {
  749. pstmt = connection.prepareStatement("SELECT uid FROM admins WHERE pid=? AND description=?;");
  750. pstmt.setInt(1, pid);
  751. pstmt.setString(2, "owner");
  752. resultSet = pstmt.executeQuery();
  753. resultSet.next();
  754. owner = resultSet.getInt(1);
  755.  
  756. pstmt = connection.prepareStatement(
  757. "UPDATE prodjects SET success=? WHERE pid=?;" +
  758. "UPDATE prodjects SET done=? WHERE pid=?;" +
  759. "UPDATE users SET saldo = saldo + ? WHERE uid=?;");
  760. pstmt.setInt(1, 1);
  761. pstmt.setInt(2, pid);
  762. pstmt.setInt(3, 1);
  763. pstmt.setInt(4, pid);
  764. pstmt.setInt(5, actVal);
  765. pstmt.setInt(6, owner);
  766. pstmt.executeUpdate();
  767. pstmt = connection.prepareStatement(
  768. "SELECT value, desciprtion FROM extrarecompenses WHERE pid=? AND uid=?;");
  769. pstmt.setInt(1, pid);
  770. pstmt.setInt(2, -1);
  771. resultSet = pstmt.executeQuery();
  772. int value = -1, valueTmp = -1;String description = "", descriptionTmp;
  773. while (resultSet.next()) {
  774. descriptionTmp = resultSet.getString(2);
  775. valueTmp = resultSet.getInt(1);
  776. if (actVal <= valueTmp) {
  777. value = valueTmp;
  778. description = descriptionTmp;
  779. }else
  780. break;
  781. }
  782. if(value != -1){
  783. pstmt = connection.prepareStatement("SELECT DISTINCT uid FROM pledge WHERE pid=?");
  784. pstmt.setInt(1, pid);
  785. resultSet = pstmt.executeQuery();
  786. while(resultSet.next()){
  787. pstmt = connection.prepareStatement("INSERT INTO recompenses(pid, uid, value, description) VALUES (?, ?, ?, ?, ?);");
  788. pstmt.setInt(1, pid);
  789. pstmt.setInt(2, resultSet.getInt(1));
  790. pstmt.setInt(3, value);
  791. pstmt.setString(4, description);
  792. pstmt.executeUpdate();
  793. }
  794. }
  795. } else {
  796. pstmt = connection.prepareStatement(
  797. "UPDATE prodjects SET success=? WHERE pid=?;" +
  798. "UPDATE prodjects SET done=? WHERE pid=?;");
  799. pstmt.setInt(1, 0);
  800. pstmt.setInt(2, pid);
  801. pstmt.setInt(3, 1);
  802. pstmt.setInt(4, pid);
  803. pstmt.executeUpdate();
  804.  
  805. pstmt = connection.prepareStatement("SELECT value, uid FROM pledge WHERE pid=?;");
  806. pstmt.setInt(1, pid);
  807. resultSet = pstmt.executeQuery();
  808. while(resultSet.next()){
  809. pstmt = connection.prepareStatement("UPDATE users SET saldo = saldo + ? WHERE uid=?;");
  810. pstmt.setInt(1, resultSet.getInt(1));
  811. pstmt.setInt(2, resultSet.getInt(2));
  812. pstmt.executeUpdate();
  813. }
  814. pstmt = connection.prepareStatement("DELETE FROM pledge WHERE pid=?");
  815. pstmt.setInt(1, pid);
  816. pstmt.executeUpdate();
  817.  
  818. }
  819. }
  820. }
  821. }catch (SQLException e){e.printStackTrace();}
  822. try {
  823. Thread.sleep(5000);
  824. }catch (InterruptedException e){e.printStackTrace();}
  825. }
  826. }
  827. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement