Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.50 KB | None | 0 0
  1. import java.rmi.*;
  2. import java.rmi.registry.LocateRegistry;
  3. import java.sql.*;
  4. import java.text.DateFormat;
  5. import java.text.SimpleDateFormat;
  6. import java.time.Instant;
  7. import java.util.ArrayList;
  8. import java.util.Calendar;
  9. import java.util.HashMap;
  10. import java.util.Map.Entry;
  11.  
  12. import org.omg.CORBA.Object;
  13.  
  14. import sun.security.provider.certpath.OCSPResponse.ResponseStatus;
  15.  
  16. import java.rmi.server.*;
  17. import java.net.*;
  18.  
  19. public class RMIServer extends UnicastRemoteObject implements RMI
  20. {
  21. static int priority;
  22. static final String JDBC_DRIVER = "oracle.jdbc.driver.OracleDriver";
  23. static final String DATABASE_URL ="jdbc:oracle:thin:@localhost:1521:xe";
  24. static final String USERNAME = "bd";
  25. static final String PASSWORD = "bd";
  26. static final int RMIPORT = 2004;
  27. java.sql.Connection conn = null;
  28. Statement stmt = null;
  29.  
  30.  
  31. public RMIServer() throws RemoteException
  32. {
  33. start_connection();
  34. Timestamp b = java.sql.Timestamp.valueOf("2000-04-19 12:12:00");
  35. System.out.println(b);
  36. CreateAuctionObject as = new CreateAuctionObject(69, "cona", "aberta", 69, "2017-04-19 23:15:00", 69);
  37.  
  38. //create_auction(as);
  39.  
  40. }
  41.  
  42. public static void main (String args[])
  43. {
  44. try {
  45. RMIServer rmiserver = new RMIServer();
  46. LocateRegistry.createRegistry(RMIPORT);
  47. Naming.rebind ("rmi://localhost:" + RMIPORT + "/rmiserver",rmiserver );
  48. } catch (RemoteException re)
  49. {
  50. System.err.println("Remote Exception!" + re.getMessage());
  51. System.exit(0);
  52. }catch ( MalformedURLException e)
  53. {
  54. System.out.println("Malformed url exception: " + e.getMessage());
  55. }
  56. }
  57.  
  58. public void start_connection() throws RemoteException
  59. {
  60. try
  61. {
  62. Class.forName(JDBC_DRIVER);
  63.  
  64. System.out.println("Connecting to database...");
  65. conn = DriverManager.getConnection(DATABASE_URL,USERNAME,PASSWORD);
  66. System.out.println("Success!.");
  67. System.out.println("Creating statement...");
  68. stmt = conn.createStatement();
  69. System.out.println("Success!.");
  70.  
  71.  
  72.  
  73.  
  74. }catch(SQLException s)
  75. {
  76. System.out.println("SQL Exception: " + s.getMessage());
  77. }
  78. catch(Exception e)
  79. {
  80. System.out.println("Class.forName:" + e.getMessage());
  81. }
  82. }
  83.  
  84. public synchronized boolean cancelAuction(HashMap<String, String> obj) throws RemoteException
  85. {
  86.  
  87.  
  88. String sql_request = "UPDATE AUCTION SET status=0 WHERE auctionid =?";
  89. PreparedStatement prepStat;
  90.  
  91. try
  92. {
  93.  
  94. prepStat = conn.prepareStatement(sql_request);
  95. prepStat.setString(1, obj.get("auction_id"));
  96. prepStat.executeUpdate();
  97. //update_database();
  98. System.out.println("Auction Canceled");
  99.  
  100.  
  101. } catch(SQLException se)
  102. {
  103. System.out.println("CANCEL AUCTION:"+se.getMessage());
  104. return false;
  105. }
  106. return true;
  107.  
  108. }
  109.  
  110.  
  111. public synchronized ArrayList<AuctionObject> getCreatedAuctions() throws RemoteException
  112. {
  113. String sql_request = "SELECT username , num_auctions_created FROM (SELECT username , num_auctions_created FROM client ORDER BY num_auctions_created DESC) WHERE rownum <= 5";
  114. ArrayList<AuctionObject> created = new ArrayList<>();
  115. PreparedStatement ps1;
  116. ResultSet rs;
  117. try{
  118. System.out.println("meto 1:");
  119. ps1 = conn.prepareStatement(sql_request);
  120. rs = ps1.executeQuery();
  121. System.out.println("entreiii");
  122. while(rs.next())
  123. {
  124. String username = rs.getString("USERNAME");
  125. int num_auctions_won = rs.getInt("NUM_AUCTIONS_CREATED");
  126. System.out.print("username:"+num_auctions_won);
  127. System.out.println("count:"+username);
  128. AuctionObject ob = new AuctionObject(num_auctions_won, username);
  129. created.add(ob);
  130. }
  131.  
  132.  
  133. }catch (Exception e) {
  134.  
  135. }
  136. return created;
  137.  
  138.  
  139. }
  140.  
  141. public synchronized ArrayList<AuctionObject> getWonAuctions() throws RemoteException
  142. {
  143. ArrayList<AuctionObject> created = new ArrayList<>();
  144. String sql_request = "SELECT username , num_auctions_won FROM (SELECT username , num_auctions_won FROM client ORDER BY num_auctions_won DESC) WHERE rownum <= 5";
  145. PreparedStatement ps;
  146. ResultSet rs;
  147.  
  148. try{
  149. System.out.println("meto 2:");
  150. ps = conn.prepareStatement(sql_request);
  151. rs = ps.executeQuery();
  152. System.out.println("entreiii");
  153. while(rs.next())
  154. {
  155. String username = rs.getString("USERNAME");
  156. int num_auctions_won = rs.getInt("NUM_AUCTIONS_won");
  157. System.out.print("username:"+num_auctions_won);
  158. System.out.println("count:"+username);
  159. AuctionObject ob = new AuctionObject(num_auctions_won, username);
  160. created.add(ob);
  161. }
  162. }catch (Exception e) {}
  163. return created;
  164.  
  165. }
  166.  
  167. public synchronized ArrayList<AuctionObject> getAuctions10() throws RemoteException
  168. {
  169. String sql_request3 = "SELECT auctionid , title FROM auction WHERE deadline > ? - 10 AND deadline < ?";
  170. ArrayList<AuctionObject> daysAuctions = new ArrayList<>();
  171. PreparedStatement ps;
  172. ResultSet rs;
  173.  
  174. try{
  175. System.out.println("meto 3:");
  176.  
  177.  
  178. System.out.println("time:");
  179.  
  180. Timestamp b= new Timestamp(System.currentTimeMillis());
  181. ps = conn.prepareStatement(sql_request3);
  182. ps.setTimestamp(1, b);
  183. ps.setTimestamp(2, b);
  184. rs = ps.executeQuery();
  185.  
  186. while(rs.next())
  187. {
  188. //System.out.println("entrou:"+dateFormat.format(cal.getTime()));
  189. String tittle = rs.getString("title");
  190. int auctionID = rs.getInt("auctionid");
  191. AuctionObject ob = new AuctionObject(auctionID, tittle);
  192. daysAuctions.add(ob);
  193. }
  194. return daysAuctions;
  195.  
  196. } catch (SQLException e) {
  197. e.printStackTrace();
  198. }
  199. return null;
  200.  
  201. }
  202.  
  203. public synchronized Boolean deleteUser(BanUser obj) throws RemoteException
  204. {
  205. String sql_request = "DELETE FROM client WHERE userid = ?";
  206. PreparedStatement prepStat;
  207.  
  208. try
  209. {
  210.  
  211. prepStat = conn.prepareStatement(sql_request);
  212. prepStat.setInt(1, obj.getUserID());
  213. prepStat.executeUpdate();
  214. //update_database();
  215. System.out.println("User Deleted");
  216.  
  217.  
  218. } catch(SQLException se)
  219. {
  220. System.out.println("DELETE USER:"+se.getMessage());
  221. return false;
  222. }
  223. return true;
  224.  
  225. }
  226.  
  227. public synchronized ArrayList<AuctionObjectBan> findMinBid(BanUser obj) throws RemoteException
  228. {
  229. String sql_request3 = "SELECT auctionid , Min(amount) FROM bid WHERE userid = ? GROUP BY auctionid";
  230. ArrayList<AuctionObjectBan> daysAuctions = new ArrayList<>();
  231. PreparedStatement ps;
  232. ResultSet rs;
  233.  
  234. try{
  235.  
  236. ps = conn.prepareStatement(sql_request3);
  237. ps.setInt(1, obj.getUserID());
  238. rs = ps.executeQuery();
  239.  
  240. while(rs.next())
  241. {
  242. int auctionid = rs.getInt("auctionid");
  243. int amount = rs.getInt("Min(amount)");
  244. System.out.print("cena:"+auctionid);
  245. System.out.println("cena:"+amount);
  246. AuctionObjectBan ob = new AuctionObjectBan(auctionid, amount);
  247. daysAuctions.add(ob);
  248. }
  249. return daysAuctions;
  250.  
  251. } catch (SQLException e) {
  252. e.printStackTrace();
  253. }
  254. return null;
  255.  
  256. }
  257.  
  258. public synchronized boolean deleteBids(int auctionid, int minbid)
  259. {
  260.  
  261. String sql_request = "DELETE FROM bid WHERE auctionid = ? AND amount >= ?";
  262. PreparedStatement prepStat;
  263.  
  264. try
  265. {
  266.  
  267. prepStat = conn.prepareStatement(sql_request);
  268. prepStat.setLong(1, auctionid);
  269. prepStat.setLong(2, minbid);
  270. prepStat.executeUpdate();
  271. //update_database();
  272. System.out.println("Bids Deleted");
  273.  
  274.  
  275. } catch(SQLException se)
  276. {
  277. System.out.println("DELETE BIDS:"+se.getMessage());
  278. return false;
  279. }
  280. return true;
  281. }
  282.  
  283. public synchronized boolean cancelAllUserProjects(BanUser obj)
  284. {
  285. String sql_request = "UPDATE AUCTION SET status= 0 WHERE onwner = ?";
  286. PreparedStatement prepStat;
  287.  
  288. try
  289. {
  290.  
  291. prepStat = conn.prepareStatement(sql_request);
  292. prepStat.setInt(1, obj.getUserID());
  293. prepStat.executeUpdate();
  294. //update_database();
  295. System.out.println("AUCTION CANCELED");
  296.  
  297.  
  298. } catch(SQLException se)
  299. {
  300. System.out.println("AUCTION CANCELED:"+se.getMessage());
  301. return false;
  302. }
  303. return true;
  304.  
  305. }
  306.  
  307.  
  308. public synchronized boolean doALlStuffOfBanUser (HashMap<String, String> obj)
  309. {
  310.  
  311. int i;
  312. ArrayList<AuctionObjectBan> res;
  313. BanUser user = new BanUser(Integer.parseInt(obj.get("userid")));
  314. try {
  315. res = findMinBid(user);
  316. System.out.println(res);
  317. cancelAllUserProjects(user);
  318.  
  319.  
  320. for(i = 0;i<res.size();i++)
  321. {
  322. deleteBids(res.get(i).getAuctionID(), res.get(i).getAmount());
  323. }
  324.  
  325. deleteUser(user);
  326.  
  327. System.out.println("type : ban_user");
  328.  
  329. } catch (RemoteException e1) {
  330. e1.printStackTrace();
  331. }
  332.  
  333.  
  334. return false;
  335.  
  336. }
  337. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement