Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException;
- import org.joda.time.DateTime;
- import java.io.*;
- import java.lang.reflect.Array;
- import java.rmi.*;
- import java.rmi.registry.LocateRegistry;
- import java.rmi.registry.Registry;
- import java.rmi.server.ExportException;
- import java.rmi.server.UnicastRemoteObject;
- import java.sql.*;
- import java.sql.Connection;
- import java.util.*;
- /**
- * Created by joel on 10/11/15.
- */
- public class DataServer extends UnicastRemoteObject implements Data {
- public static Connection connection = null;
- public static PreparedStatement pstmt;
- public static ResultSet resultSet;
- public static Ghost ghost;
- public static int registryPort;
- public static String mysqlHost;
- public static String mysqlUsername;
- public static String mysqlpassword;
- public DataServer() throws RemoteException {
- super();
- }
- public ArrayList<Project> listActiveProdjects(Request request) throws RemoteException{
- try{
- ArrayList<Project> arr = new ArrayList<>();
- pstmt = connection.prepareStatement("SELECT name, headval, actval FROM prodjects WHERE done=?");
- pstmt.setInt(1,0);
- ResultSet rs = pstmt.executeQuery();
- while(rs.next()){
- Project temp = new Project(rs.getString(1), rs.getInt(2), rs.getInt(3));
- arr.add(temp);
- }
- return arr;
- }catch(SQLException e) {
- System.out.println("Returned exception: " + e);
- return null;
- }
- }
- public ArrayList<Project> listOldProdjects(Request request) throws RemoteException{
- try{
- ArrayList<Project> arr = new ArrayList<>();
- pstmt = connection.prepareStatement("SELECT name, headval, actval, success FROM prodjects WHERE done=?");
- pstmt.setInt(1,1);
- ResultSet rs = pstmt.executeQuery();
- while(rs.next()){
- Project temp = new Project(rs.getString(1), rs.getInt(2), rs.getInt(3), rs.getInt(4));
- arr.add(temp);
- }
- return arr;
- }catch(SQLException e) {
- System.out.println("Returned exception: " + e);
- return null;
- }
- }
- public Project consultProdject(Request request) throws RemoteException{
- try{
- //get project name, deadline and description from table prodjects
- pstmt = connection.prepareStatement("SELECT name, deadline, description, pid FROM prodjects WHERE name=?");
- pstmt.setString(1, (String)request.requestObj);
- ResultSet rs = pstmt.executeQuery();
- rs.next();
- String name = rs.getString(1);
- String deadline = rs.getString(2);
- String description = rs.getString(3);
- int pid = rs.getInt(4);
- //get project possible rewards from table recompenses
- pstmt = connection.prepareStatement("SELECT description, value FROM recompenses WHERE pid=? AND uid=?");
- pstmt.setInt(1,pid);
- pstmt.setInt(2, -1);
- rs= pstmt.executeQuery();
- ArrayList <Recompense> tempRecompenses = new ArrayList<>();
- while(rs.next()){
- String recompenseDesc = rs.getString(1);
- int value = rs.getInt(2);
- tempRecompenses.add(new Recompense(value, recompenseDesc));
- }
- //get project extra
- pstmt = connection.prepareStatement("SELECT description, value FROM extrarecompenses WHERE pid=? AND uid=?");
- pstmt.setInt(1,pid);
- pstmt.setInt(2,-1);
- rs = pstmt.executeQuery();
- ArrayList <Recompense> tempExtraRecompenses = new ArrayList<>();
- while(rs.next()){
- String extraDesc = rs.getString(1);
- int extraValue = rs.getInt(2);
- tempExtraRecompenses.add(new Recompense(extraValue,extraDesc));
- }
- Project proj = new Project(name,
- deadline,
- description,
- tempRecompenses,
- tempExtraRecompenses);
- return proj;
- }catch(SQLException e){
- e.printStackTrace();
- return null;
- }
- }
- public boolean loginClient(Request request) throws RemoteException {
- System.out.println("loginClient");
- try {
- User user = (User) request.user;
- System.out.println("User: "+request.user.username+" pass: "+request.user.password);
- pstmt = connection.prepareStatement("SELECT password FROM users WHERE username=?;");
- pstmt.setString(1, user.username);
- resultSet = pstmt.executeQuery();
- while(resultSet.next()) {
- String str = resultSet.getString(1);
- if(str.equals(user.password)){
- return true;
- }
- }
- }catch (SQLException e){e.printStackTrace();}
- return false;
- }
- public boolean registerClient(Request request) throws RemoteException{
- System.out.println("registerClient");
- try {
- User user = (User) request.user;
- pstmt = connection.prepareStatement("INSERT INTO users(username, password) VALUES (?, ?);");
- pstmt.setString(1, user.username);
- pstmt.setString(2, user.password);
- pstmt.executeUpdate();
- }catch (SQLException e){
- return false;
- }
- return true;
- }
- public int getSaldo(Request request) throws RemoteException{
- System.out.println("getSaldo");
- try {
- User user = (User) request.requestObj;
- String username=user.username;
- pstmt = connection.prepareStatement("SELECT password, saldo FROM users WHERE username=?;");
- pstmt.setString(1, username);
- resultSet = pstmt.executeQuery();
- while(resultSet.next()) {
- String str = resultSet.getString(1);
- int saldo = resultSet.getInt(2);
- if(str.equals(user.password)){
- return saldo;
- }
- }
- }catch (SQLException e){e.printStackTrace();}
- return -1;
- }
- public ArrayList <Recompense> getRecompenses(Request request) throws RemoteException{
- System.out.println("getRecompenses");
- try{
- User usernameTemp = (User) request.requestObj;
- ArrayList <Recompense> arr = new ArrayList<>();
- pstmt = connection.prepareStatement("SELECT uid FROM users WHERE username=?;");
- pstmt.setString(1, usernameTemp.username);
- resultSet = pstmt.executeQuery();
- int uid = -1;
- resultSet.next();
- uid = resultSet.getInt(1);
- pstmt = connection.prepareStatement("SELECT value, description, delivered FROM recompenses WHERE uid=?;");
- pstmt.setInt(1, uid);
- resultSet = pstmt.executeQuery();
- while(resultSet.next()){
- arr.add(new Recompense(resultSet.getInt(1), resultSet.getString(2), resultSet.getInt(3)));
- }
- return arr;
- }catch (SQLException e){
- return null;
- }
- }
- public User getProjID(Request request){
- try{
- User userTemp = new User();
- String temp = (String) request.requestObj;
- pstmt = connection.prepareStatement("SELECT pid FROM prodjects WHERE name=?;");
- pstmt.setString(1, temp);
- resultSet = pstmt.executeQuery();
- resultSet.next();
- int pid = resultSet.getInt(1);
- pstmt = connection.prepareStatement("SELECT pid, uid FROM admins WHERE pid=?");
- pstmt.setInt(1,pid);
- resultSet = pstmt.executeQuery();
- resultSet.next();
- int uid = resultSet.getInt(2);
- pstmt = connection.prepareStatement("SELECT uid, username FROM users WHERE uid=?");
- pstmt.setInt(1,uid);
- resultSet = pstmt.executeQuery();
- resultSet.next();
- String ola= resultSet.getString(2);
- System.out.println("USERNAM GETPROJID IN FUNCTION"+ola);
- return userTemp;
- }catch(SQLException e){
- System.out.println("SQL exception: " + e);
- return null;
- }
- }
- public String[] getProjChoices(Request request){
- try{
- String temp = (String) request.requestObj;
- pstmt = connection.prepareStatement("SELECT choice1, choice2 FROM prodjects WHERE name=?");
- pstmt.setString(1, temp);
- resultSet = pstmt.executeQuery();
- resultSet.next();
- String choice1 = resultSet.getString(1);
- String choice2 = resultSet.getString(2);
- String[] tempArray = new String[2];
- tempArray[0] = choice1;
- tempArray[1] = choice2;
- return tempArray;
- }catch(SQLException e){
- System.out.println("SQL exception: " + e);
- return null;
- }
- }
- public boolean pledge(Request request) throws RemoteException{
- System.out.println("pledge");
- try{
- String timestampStr = "";
- Pledge pledge = (Pledge) request.requestObj;
- //CHECKING IF INTERRUPTED
- pstmt = connection.prepareStatement("SELECT pid FROM prodjects WHERE name=?");
- pstmt.setString(1, pledge.name);
- resultSet = pstmt.executeQuery();
- resultSet.next();
- int pid = resultSet.getInt(1);
- pstmt = connection.prepareStatement("SELECT timest FROM users WHERE timest=?");
- pstmt.setString(1, request.timeStamp.toString());
- resultSet = pstmt.executeQuery();
- if(resultSet.next()){
- pstmt = connection.prepareStatement("UPDATE users SET saldo = saldo - ?, timest = ? WHERE username=?;");
- pstmt.setInt(1, pledge.value);
- pstmt.setString(2, "-1");
- pstmt.setString(3, pledge.user.username);
- pstmt.executeUpdate();
- if (pledge.choice == 1) {
- pstmt = connection.prepareStatement("UPDATE prodjects SET timest = ?, actval = actval - ?, choicecount1 = choicecount1 - ? WHERE pid=?;");
- } else {
- pstmt = connection.prepareStatement("UPDATE prodjects SET timest = ?, actval = actval - ?, choicecount2 = choicecount2 - ? WHERE pid=?;");
- }
- pstmt.setString(1, "-1");
- pstmt.setInt(2, pledge.value);
- pstmt.setInt(3, pledge.value);
- pstmt.setInt(4, pid);
- pstmt.executeUpdate();
- }
- pstmt = connection.prepareStatement("DELETE FROM pledge WHERE timest=?");
- pstmt.setString(1, request.timeStamp.toString());
- pstmt.executeUpdate();
- pstmt = connection.prepareStatement("DELETE FROM recompenses WHERE timest=?");
- pstmt.setString(1, request.timeStamp.toString());
- pstmt.executeUpdate();
- pstmt = connection.prepareStatement("SELECT saldo FROM users WHERE username=?");
- pstmt.setString(1, pledge.user.username);
- resultSet = pstmt.executeQuery();
- resultSet.next();
- int saldo = resultSet.getInt(1);
- if(saldo >= pledge.value) {
- pstmt = connection.prepareStatement("SELECT uid FROM users WHERE username=?");
- pstmt.setString(1, pledge.user.username);
- resultSet = pstmt.executeQuery();
- resultSet.next();
- int uid = resultSet.getInt(1);
- pstmt = connection.prepareStatement("SELECT uid FROM users WHERE username=?");
- pstmt.setString(1, pledge.target.username);
- resultSet = pstmt.executeQuery();
- resultSet.next();
- int uidtarget = resultSet.getInt(1);
- pstmt = connection.prepareStatement("SELECT description, value FROM recompenses WHERE pid=? AND uid=? ORDER BY rid;");
- pstmt.setInt(1, pid);
- pstmt.setInt(2, -1);
- resultSet = pstmt.executeQuery();
- String description = "";
- int value = -1;
- while (resultSet.next()) {
- description = resultSet.getString(1);
- value = resultSet.getInt(2);
- if (value <= pledge.value)
- break;
- }
- pstmt = connection.prepareStatement("INSERT INTO pledge(pid, uid, uidtarget, choice, value, timest) VALUES (?, ?, ?, ?, ?, ?);");
- pstmt.setInt(1, pid);
- pstmt.setInt(2, uid);
- pstmt.setInt(3, uidtarget);
- pstmt.setInt(4, pledge.choice);
- pstmt.setInt(5, pledge.value);
- pstmt.setString(6, request.timeStamp.toString());
- pstmt.executeUpdate();
- if (pledge.choice == 1) {
- pstmt = connection.prepareStatement("UPDATE prodjects SET timest = ?, actval = actval + ?, choicecount1 = choicecount1 + ? WHERE pid=?;");
- } else {
- pstmt = connection.prepareStatement("UPDATE prodjects SET timest = ?, actval = actval + ?, choicecount2 = choicecount2 + ? WHERE pid=?;");
- }
- pstmt.setString(1, request.timeStamp.toString());
- pstmt.setInt(2, pledge.value);
- pstmt.setInt(3, pledge.value);
- pstmt.setInt(4, pid);
- pstmt.executeUpdate();
- pstmt = connection.prepareStatement("UPDATE users SET saldo = saldo - ?, timest = ? WHERE uid=?;");
- pstmt.setInt(1, pledge.value);
- pstmt.setString(2, request.timeStamp.toString());
- pstmt.setInt(3, uid);
- pstmt.executeUpdate();
- if (value != -1) {
- pstmt = connection.prepareStatement("INSERT INTO recompenses(pid, uid, value, description, timest) VALUES (?, ?, ?, ?, ?);");
- pstmt.setInt(1, pid);
- pstmt.setInt(2, uidtarget);
- pstmt.setInt(3, value);
- pstmt.setString(4, description);
- pstmt.setString(5, request.timeStamp.toString());
- pstmt.executeUpdate();
- }
- return true;
- }else{
- return false;
- }
- }catch (SQLException e){
- System.out.println(e);
- e.printStackTrace();
- return false;
- }
- }
- public boolean sendMessage(Request request) throws RemoteException{
- System.out.println("Send Message");
- try{
- String projectName = (String) request.requestObj;
- Message tempMessage = (Message) request.requestObj2;
- pstmt = connection.prepareStatement("SELECT pid, name FROM prodjects WHERE name=?");
- pstmt.setString(1, projectName);
- ResultSet rs = pstmt.executeQuery();
- rs.next();
- int pid = rs.getInt(1);
- String tempUsername = request.user.username;
- pstmt = connection.prepareStatement("SELECT username, uid FROM users WHERE username=?");
- pstmt.setString(1,tempUsername);
- rs = pstmt.executeQuery();
- rs.next();
- int uid = rs.getInt(2);
- System.out.println(uid);
- pstmt = connection.prepareStatement("INSERT INTO message (pid, uid, qest)" +
- "VALUES (?,?,?)");
- pstmt.setInt(1,pid);
- pstmt.setInt(2,uid);
- pstmt.setString(3,tempMessage.quest);
- pstmt.executeUpdate();
- return true;
- }catch(SQLException e){
- System.out.println("SQL exception: " + e);
- return false;
- }
- }
- public boolean answerMessage(Request request) throws RemoteException{
- System.out.println("Answering message");
- try{
- String projName = (String) request.requestObj;
- Message tempMessage = (Message) request.requestObj2;
- pstmt = connection.prepareStatement("SELECT pid, name FROM prodjects WHERE name=?");
- pstmt.setString(1, projName);
- ResultSet rs = pstmt.executeQuery();
- rs.next();
- int pid = rs.getInt(1);
- pstmt = connection.prepareStatement("SELECT uid FROM users WHERE username=?");
- pstmt.setString(1, request.user.username);
- rs = pstmt.executeQuery();
- rs.next();
- int uid = rs.getInt(1);
- pstmt = connection.prepareStatement("SELECT description FROM admins WHERE uid=? AND pid=?");
- pstmt.setInt(1,uid);
- pstmt.setInt(2,pid);
- rs = pstmt.executeQuery();
- if(rs.next()){
- pstmt = connection.prepareStatement("SELECT pid, uid, qest, mid FROM message WHERE pid=? AND uid=? AND qest=?");
- pstmt.setInt(1,pid);
- pstmt.setInt(2,uid);
- pstmt.setString(3, tempMessage.quest);
- rs = pstmt.executeQuery();
- rs.next();
- int mid = rs.getInt(4);
- pstmt = connection.prepareStatement("UPDATE message SET answ = ? WHERE mid=?");
- pstmt.setString(1,tempMessage.answer);
- pstmt.setInt(2, mid);
- pstmt.executeUpdate();
- return true;
- }
- return false;
- }catch(SQLException e){
- e.printStackTrace();
- return false;
- }
- }
- public boolean createProdject(Request request) throws RemoteException{
- System.out.println("createProdject");
- try {
- Project project = (Project) request.requestObj;
- pstmt = connection.prepareStatement("INSERT INTO prodjects(" +
- "name, deadline, headval, description, choice1, choice2) VALUES (?, ?, ?, ?, ?, ?);");
- pstmt.setString(1, project.name);
- pstmt.setString(2, project.deadline.toString());
- pstmt.setInt(3, project.headVal);
- pstmt.setString(4, project.description);
- pstmt.setString(5, project.choice1);
- pstmt.setString(6, project.choice2);
- pstmt.executeUpdate();
- int pid = -1;
- pstmt = connection.prepareStatement("SELECT pid FROM prodjects WHERE name = ?;");
- pstmt.setString(1, project.name);
- resultSet = pstmt.executeQuery();
- while(resultSet.next()){
- pid = resultSet.getInt(1);
- }
- System.out.println(pid);
- if(pid != -1) {
- if(project.recompenses != null){
- for (int i = 0; i < project.recompenses.size(); i++) {
- pstmt = connection.prepareStatement("INSERT INTO recompenses(" +
- "pid, uid, value, description) VALUES (?, ?, ?, ?);");
- pstmt.setInt(1, pid);
- pstmt.setInt(2, -1);
- pstmt.setInt(3, project.recompenses.get(i).val);
- pstmt.setString(4, project.recompenses.get(i).description);
- pstmt.executeUpdate();
- }
- }
- if (project.extraRecompenses != null){
- for (int i = 0; i < project.extraRecompenses.size(); i++) {
- pstmt = connection.prepareStatement("INSERT INTO extrarecompenses(" +
- "pid, uid, value, description) VALUES (?, ?, ?, ?);");
- pstmt.setInt(1, pid);
- pstmt.setInt(2, -1);
- pstmt.setInt(3, project.extraRecompenses.get(i).val);
- pstmt.setString(4, project.extraRecompenses.get(i).description);
- pstmt.executeUpdate();
- }
- }
- for (int i = 0; i < project.admins.size(); i++) {
- pstmt = connection.prepareStatement("INSERT INTO admins(" +
- "pid, uid, description, username) VALUES (?, ?, ?, ?);");
- PreparedStatement tmppstmt = connection.prepareStatement("SELECT uid FROM users WHERE username=?;");
- tmppstmt.setString(1, project.admins.get(i).username);
- resultSet = tmppstmt.executeQuery();
- int uid = -1;
- while (resultSet.next()) {
- uid = resultSet.getInt(1);
- }
- pstmt.setInt(1, pid);
- pstmt.setInt(2, uid);
- pstmt.setString(3, project.admins.get(i).description);
- pstmt.setString(4, project.admins.get(i).username);
- pstmt.executeUpdate();
- }
- }
- }catch (SQLException e){
- System.out.println("SQL exception "+e);
- return false;
- }
- return true;
- }
- public boolean addRecompense(Request request) throws RemoteException{
- System.out.println("Add reward!\n");
- try{
- String name = (String) request.requestObj;
- pstmt = connection.prepareStatement("SELECT pid, name FROM prodjects WHERE name=?");
- pstmt.setString(1, name);
- ResultSet rs = pstmt.executeQuery();
- rs.next();
- int pid = rs.getInt(1);
- pstmt = connection.prepareStatement("SELECT uid FROM users WHERE username=?");
- pstmt.setString(1, request.user.username);
- rs = pstmt.executeQuery();
- rs.next();
- int uid = rs.getInt(1);
- pstmt = connection.prepareStatement("SELECT description FROM admins WHERE uid=? AND pid=?");
- pstmt.setInt(1,uid);
- pstmt.setInt(2,pid);
- rs = pstmt.executeQuery();
- if(rs.next()){
- Recompense tempRecompense = (Recompense) request.requestObj2;
- pstmt = connection.prepareStatement("INSERT INTO recompenses (pid,uid,value,description)" +
- "VALUES (?,?,?,?)");
- pstmt.setInt(1,pid);
- pstmt.setInt(2,-1);
- pstmt.setInt(3, tempRecompense.val);
- pstmt.setString(4, tempRecompense.description);
- pstmt.executeUpdate();
- }
- else{
- return false;
- }
- return true;
- }catch(SQLException e){
- e.printStackTrace();
- return false;
- }
- }
- public boolean removeRecompense(Request request) throws RemoteException{
- System.out.println("Remove reward!\n");
- try{
- String name = (String) request.requestObj;
- pstmt = connection.prepareStatement("SELECT pid, name FROM prodjects WHERE name=?");
- pstmt.setString(1, name);
- ResultSet rs = pstmt.executeQuery();
- rs.next();
- int pid = rs.getInt(1);
- pstmt = connection.prepareStatement("SELECT uid FROM users WHERE username=?");
- pstmt.setString(1, request.user.username);
- rs = pstmt.executeQuery();
- rs.next();
- int uid = rs.getInt(1);
- pstmt = connection.prepareStatement("SELECT description FROM admins WHERE uid=? AND pid=?");
- pstmt.setInt(1,uid);
- pstmt.setInt(2,pid);
- rs = pstmt.executeQuery();
- if(rs.next()){
- Recompense tempRecompense = (Recompense) request.requestObj2;
- pstmt = connection.prepareStatement("DELETE FROM recompenses WHERE pid =? AND uid=? AND value=? AND description=?");
- pstmt.setInt(1,pid);
- pstmt.setInt(2,-1);
- pstmt.setInt(3,tempRecompense.val);
- pstmt.setString(4,tempRecompense.description);
- pstmt.executeUpdate();
- }
- else{
- return false;
- }
- return true;
- }catch(SQLException e){
- e.printStackTrace();
- return false;
- }
- }
- public boolean cancelProdject(Request request) throws RemoteException{
- System.out.println("cancelProdject");
- try{
- String projectName = (String) request.requestObj;
- pstmt = connection.prepareStatement("SELECT uid FROM users WHERE username=?;");
- pstmt.setString(1, request.user.username);
- resultSet = pstmt.executeQuery();
- resultSet.next();
- int uid = resultSet.getInt(1);
- pstmt = connection.prepareStatement("SELECT pid FROM prodjects WHERE name=?;");
- pstmt.setString(1, projectName);
- resultSet = pstmt.executeQuery();
- resultSet.next();
- int pid = resultSet.getInt(1);
- pstmt = connection.prepareStatement("SELECT description FROM admins WHERE uid=? and pid=?;");
- pstmt.setInt(1, uid);
- pstmt.setInt(2, pid);
- resultSet = pstmt.executeQuery();
- if(resultSet.next()){
- pstmt = connection.prepareStatement("SELECT value, uid FROM pledge WHERE pid=?;");
- pstmt.setInt(1, pid);
- resultSet = pstmt.executeQuery();
- while(resultSet.next()){
- pstmt = connection.prepareStatement("UPDATE users SET saldo = saldo + ? WHERE uid=?;");
- pstmt.setInt(1, resultSet.getInt(1));
- pstmt.setInt(2, resultSet.getInt(2));
- pstmt.executeUpdate();
- }
- pstmt = connection.prepareStatement("DELETE FROM recompenses WHERE pid=?");
- pstmt.setInt(1, pid);
- pstmt.executeUpdate();
- pstmt = connection.prepareStatement("DELETE FROM message WHERE pid=?");
- pstmt.setInt(1, pid);
- pstmt.executeUpdate();
- pstmt = connection.prepareStatement("DELETE FROM extrarecompenses WHERE pid=?");
- pstmt.setInt(1, pid);
- pstmt.executeUpdate();
- pstmt = connection.prepareStatement("DELETE FROM admins WHERE pid=?");
- pstmt.setInt(1, pid);
- pstmt.executeUpdate();
- pstmt = connection.prepareStatement("DELETE FROM prodjects WHERE pid=?");
- pstmt.setInt(1, pid);
- pstmt.executeUpdate();
- pstmt = connection.prepareStatement("DELETE FROM pledge WHERE pid=?");
- pstmt.setInt(1, pid);
- pstmt.executeUpdate();
- return true;
- }else{
- return false;
- }
- }catch (SQLException e){
- e.printStackTrace();
- return false;
- }
- }
- public static void main(String[] args) {
- setupConfig();
- //CONNECTING TO MYSQL SERVER
- try {
- connection = DriverManager.getConnection(mysqlHost, mysqlUsername, mysqlpassword);
- }catch (SQLException e){e.printStackTrace();}
- System.out.println("MYSQL connection ready.");
- ghost = new Ghost(connection);
- //STARTING RMI
- try {
- DataServer daSer = new DataServer();
- Registry loReg = LocateRegistry.createRegistry(registryPort);
- loReg.rebind("data", daSer);
- } catch (RemoteException re) {
- System.out.println("Exception in DataServer.main: " + re);
- }
- System.out.println("Data Server ready.");
- }
- public static void setupConfig(){
- String filename = "dataServerConfig.txt", line;
- StringTokenizer tokenizer;
- try {
- FileReader inputFile = new FileReader(filename);
- BufferedReader bufferReader = new BufferedReader(inputFile);
- bufferReader.readLine();
- for(int i = 0; i < 4; i++) {
- line = bufferReader.readLine();
- tokenizer = new StringTokenizer(line, "=");
- line = tokenizer.nextToken();
- if(line.equals("registry port")) {
- registryPort = Integer.parseInt(tokenizer.nextToken());
- }else if(line.equals("mysql host")) {
- mysqlHost = tokenizer.nextToken();
- }else if(line.equals("mysql username")) {
- mysqlUsername = tokenizer.nextToken();
- }else if(line.equals("mysql password")) {
- if(tokenizer.hasMoreTokens())
- mysqlpassword = tokenizer.nextToken();
- else
- mysqlpassword = "";
- }
- }
- }catch (FileNotFoundException e){
- System.out.println("Client configuration file: '" + filename + "' not found.");
- System.exit(0);
- }catch (IOException e){}
- }
- }
- class Ghost extends Thread implements Serializable{
- public static Connection connection;
- public static PreparedStatement pstmt;
- public static ResultSet resultSet;
- public Ghost(Connection connection){
- this.connection = connection;
- this.start();
- }
- public void run(){
- while(true){
- try{
- int pid, owner, headVal, actVal; String deadline; DateTime deadTime, actTime;
- //System.out.println("ghost is working...");
- pstmt = connection.prepareStatement("SELECT pid, deadline, headval, actval FROM prodjects WHERE done=?;");
- pstmt.setInt(1, 0);
- resultSet = pstmt.executeQuery();
- while(resultSet.next()){
- pid = resultSet.getInt(1);
- deadline = resultSet.getString(2);
- headVal = resultSet.getInt(3);
- actVal = resultSet.getInt(4);
- deadTime = DateTime.parse(deadline);
- actTime = new DateTime();
- if(deadTime.isBefore(actTime)){
- if (actVal >= headVal) {
- pstmt = connection.prepareStatement("SELECT uid FROM admins WHERE pid=? AND description=?;");
- pstmt.setInt(1, pid);
- pstmt.setString(2, "owner");
- resultSet = pstmt.executeQuery();
- resultSet.next();
- owner = resultSet.getInt(1);
- pstmt = connection.prepareStatement(
- "UPDATE prodjects SET success=? WHERE pid=?;" +
- "UPDATE prodjects SET done=? WHERE pid=?;" +
- "UPDATE users SET saldo = saldo + ? WHERE uid=?;");
- pstmt.setInt(1, 1);
- pstmt.setInt(2, pid);
- pstmt.setInt(3, 1);
- pstmt.setInt(4, pid);
- pstmt.setInt(5, actVal);
- pstmt.setInt(6, owner);
- pstmt.executeUpdate();
- pstmt = connection.prepareStatement(
- "SELECT value, desciprtion FROM extrarecompenses WHERE pid=? AND uid=?;");
- pstmt.setInt(1, pid);
- pstmt.setInt(2, -1);
- resultSet = pstmt.executeQuery();
- int value = -1, valueTmp = -1;String description = "", descriptionTmp;
- while (resultSet.next()) {
- descriptionTmp = resultSet.getString(2);
- valueTmp = resultSet.getInt(1);
- if (actVal <= valueTmp) {
- value = valueTmp;
- description = descriptionTmp;
- }else
- break;
- }
- if(value != -1){
- pstmt = connection.prepareStatement("SELECT DISTINCT uid FROM pledge WHERE pid=?");
- pstmt.setInt(1, pid);
- resultSet = pstmt.executeQuery();
- while(resultSet.next()){
- pstmt = connection.prepareStatement("INSERT INTO recompenses(pid, uid, value, description) VALUES (?, ?, ?, ?, ?);");
- pstmt.setInt(1, pid);
- pstmt.setInt(2, resultSet.getInt(1));
- pstmt.setInt(3, value);
- pstmt.setString(4, description);
- pstmt.executeUpdate();
- }
- }
- } else {
- pstmt = connection.prepareStatement(
- "UPDATE prodjects SET success=? WHERE pid=?;" +
- "UPDATE prodjects SET done=? WHERE pid=?;");
- pstmt.setInt(1, 0);
- pstmt.setInt(2, pid);
- pstmt.setInt(3, 1);
- pstmt.setInt(4, pid);
- pstmt.executeUpdate();
- pstmt = connection.prepareStatement("SELECT value, uid FROM pledge WHERE pid=?;");
- pstmt.setInt(1, pid);
- resultSet = pstmt.executeQuery();
- while(resultSet.next()){
- pstmt = connection.prepareStatement("UPDATE users SET saldo = saldo + ? WHERE uid=?;");
- pstmt.setInt(1, resultSet.getInt(1));
- pstmt.setInt(2, resultSet.getInt(2));
- pstmt.executeUpdate();
- }
- pstmt = connection.prepareStatement("DELETE FROM pledge WHERE pid=?");
- pstmt.setInt(1, pid);
- pstmt.executeUpdate();
- }
- }
- }
- }catch (SQLException e){e.printStackTrace();}
- try {
- Thread.sleep(5000);
- }catch (InterruptedException e){e.printStackTrace();}
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement