Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.rmi.*;
- import java.rmi.registry.LocateRegistry;
- import java.sql.*;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.time.Instant;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.HashMap;
- import java.util.Map.Entry;
- import org.omg.CORBA.Object;
- import sun.security.provider.certpath.OCSPResponse.ResponseStatus;
- import java.rmi.server.*;
- import java.net.*;
- public class RMIServer extends UnicastRemoteObject implements RMI
- {
- static int priority;
- static final String JDBC_DRIVER = "oracle.jdbc.driver.OracleDriver";
- static final String DATABASE_URL ="jdbc:oracle:thin:@localhost:1521:xe";
- static final String USERNAME = "bd";
- static final String PASSWORD = "bd";
- static final int RMIPORT = 2004;
- java.sql.Connection conn = null;
- Statement stmt = null;
- public RMIServer() throws RemoteException
- {
- start_connection();
- Timestamp b = java.sql.Timestamp.valueOf("2000-04-19 12:12:00");
- System.out.println(b);
- CreateAuctionObject as = new CreateAuctionObject(69, "cona", "aberta", 69, "2017-04-19 23:15:00", 69);
- //create_auction(as);
- }
- public static void main (String args[])
- {
- try {
- RMIServer rmiserver = new RMIServer();
- LocateRegistry.createRegistry(RMIPORT);
- Naming.rebind ("rmi://localhost:" + RMIPORT + "/rmiserver",rmiserver );
- } catch (RemoteException re)
- {
- System.err.println("Remote Exception!" + re.getMessage());
- System.exit(0);
- }catch ( MalformedURLException e)
- {
- System.out.println("Malformed url exception: " + e.getMessage());
- }
- }
- public void start_connection() throws RemoteException
- {
- try
- {
- Class.forName(JDBC_DRIVER);
- System.out.println("Connecting to database...");
- conn = DriverManager.getConnection(DATABASE_URL,USERNAME,PASSWORD);
- System.out.println("Success!.");
- System.out.println("Creating statement...");
- stmt = conn.createStatement();
- System.out.println("Success!.");
- }catch(SQLException s)
- {
- System.out.println("SQL Exception: " + s.getMessage());
- }
- catch(Exception e)
- {
- System.out.println("Class.forName:" + e.getMessage());
- }
- }
- public synchronized boolean cancelAuction(HashMap<String, String> obj) throws RemoteException
- {
- String sql_request = "UPDATE AUCTION SET status=0 WHERE auctionid =?";
- PreparedStatement prepStat;
- try
- {
- prepStat = conn.prepareStatement(sql_request);
- prepStat.setString(1, obj.get("auction_id"));
- prepStat.executeUpdate();
- //update_database();
- System.out.println("Auction Canceled");
- } catch(SQLException se)
- {
- System.out.println("CANCEL AUCTION:"+se.getMessage());
- return false;
- }
- return true;
- }
- public synchronized ArrayList<AuctionObject> getCreatedAuctions() throws RemoteException
- {
- 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";
- ArrayList<AuctionObject> created = new ArrayList<>();
- PreparedStatement ps1;
- ResultSet rs;
- try{
- System.out.println("meto 1:");
- ps1 = conn.prepareStatement(sql_request);
- rs = ps1.executeQuery();
- System.out.println("entreiii");
- while(rs.next())
- {
- String username = rs.getString("USERNAME");
- int num_auctions_won = rs.getInt("NUM_AUCTIONS_CREATED");
- System.out.print("username:"+num_auctions_won);
- System.out.println("count:"+username);
- AuctionObject ob = new AuctionObject(num_auctions_won, username);
- created.add(ob);
- }
- }catch (Exception e) {
- }
- return created;
- }
- public synchronized ArrayList<AuctionObject> getWonAuctions() throws RemoteException
- {
- ArrayList<AuctionObject> created = new ArrayList<>();
- 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";
- PreparedStatement ps;
- ResultSet rs;
- try{
- System.out.println("meto 2:");
- ps = conn.prepareStatement(sql_request);
- rs = ps.executeQuery();
- System.out.println("entreiii");
- while(rs.next())
- {
- String username = rs.getString("USERNAME");
- int num_auctions_won = rs.getInt("NUM_AUCTIONS_won");
- System.out.print("username:"+num_auctions_won);
- System.out.println("count:"+username);
- AuctionObject ob = new AuctionObject(num_auctions_won, username);
- created.add(ob);
- }
- }catch (Exception e) {}
- return created;
- }
- public synchronized ArrayList<AuctionObject> getAuctions10() throws RemoteException
- {
- String sql_request3 = "SELECT auctionid , title FROM auction WHERE deadline > ? - 10 AND deadline < ?";
- ArrayList<AuctionObject> daysAuctions = new ArrayList<>();
- PreparedStatement ps;
- ResultSet rs;
- try{
- System.out.println("meto 3:");
- System.out.println("time:");
- Timestamp b= new Timestamp(System.currentTimeMillis());
- ps = conn.prepareStatement(sql_request3);
- ps.setTimestamp(1, b);
- ps.setTimestamp(2, b);
- rs = ps.executeQuery();
- while(rs.next())
- {
- //System.out.println("entrou:"+dateFormat.format(cal.getTime()));
- String tittle = rs.getString("title");
- int auctionID = rs.getInt("auctionid");
- AuctionObject ob = new AuctionObject(auctionID, tittle);
- daysAuctions.add(ob);
- }
- return daysAuctions;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return null;
- }
- public synchronized Boolean deleteUser(BanUser obj) throws RemoteException
- {
- String sql_request = "DELETE FROM client WHERE userid = ?";
- PreparedStatement prepStat;
- try
- {
- prepStat = conn.prepareStatement(sql_request);
- prepStat.setInt(1, obj.getUserID());
- prepStat.executeUpdate();
- //update_database();
- System.out.println("User Deleted");
- } catch(SQLException se)
- {
- System.out.println("DELETE USER:"+se.getMessage());
- return false;
- }
- return true;
- }
- public synchronized ArrayList<AuctionObjectBan> findMinBid(BanUser obj) throws RemoteException
- {
- String sql_request3 = "SELECT auctionid , Min(amount) FROM bid WHERE userid = ? GROUP BY auctionid";
- ArrayList<AuctionObjectBan> daysAuctions = new ArrayList<>();
- PreparedStatement ps;
- ResultSet rs;
- try{
- ps = conn.prepareStatement(sql_request3);
- ps.setInt(1, obj.getUserID());
- rs = ps.executeQuery();
- while(rs.next())
- {
- int auctionid = rs.getInt("auctionid");
- int amount = rs.getInt("Min(amount)");
- System.out.print("cena:"+auctionid);
- System.out.println("cena:"+amount);
- AuctionObjectBan ob = new AuctionObjectBan(auctionid, amount);
- daysAuctions.add(ob);
- }
- return daysAuctions;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return null;
- }
- public synchronized boolean deleteBids(int auctionid, int minbid)
- {
- String sql_request = "DELETE FROM bid WHERE auctionid = ? AND amount >= ?";
- PreparedStatement prepStat;
- try
- {
- prepStat = conn.prepareStatement(sql_request);
- prepStat.setLong(1, auctionid);
- prepStat.setLong(2, minbid);
- prepStat.executeUpdate();
- //update_database();
- System.out.println("Bids Deleted");
- } catch(SQLException se)
- {
- System.out.println("DELETE BIDS:"+se.getMessage());
- return false;
- }
- return true;
- }
- public synchronized boolean cancelAllUserProjects(BanUser obj)
- {
- String sql_request = "UPDATE AUCTION SET status= 0 WHERE onwner = ?";
- PreparedStatement prepStat;
- try
- {
- prepStat = conn.prepareStatement(sql_request);
- prepStat.setInt(1, obj.getUserID());
- prepStat.executeUpdate();
- //update_database();
- System.out.println("AUCTION CANCELED");
- } catch(SQLException se)
- {
- System.out.println("AUCTION CANCELED:"+se.getMessage());
- return false;
- }
- return true;
- }
- public synchronized boolean doALlStuffOfBanUser (HashMap<String, String> obj)
- {
- int i;
- ArrayList<AuctionObjectBan> res;
- BanUser user = new BanUser(Integer.parseInt(obj.get("userid")));
- try {
- res = findMinBid(user);
- System.out.println(res);
- cancelAllUserProjects(user);
- for(i = 0;i<res.size();i++)
- {
- deleteBids(res.get(i).getAuctionID(), res.get(i).getAmount());
- }
- deleteUser(user);
- System.out.println("type : ban_user");
- } catch (RemoteException e1) {
- e1.printStackTrace();
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement