Advertisement
Guest User

StandardTransaction.java

a guest
May 7th, 2015
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.04 KB | None | 0 0
  1. package me.ekranos.misc.economy;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7.  
  8. public class StandardTransaction implements Transaction {
  9.  
  10.     private int id = Integer.MIN_VALUE;
  11.     private Deposit source;
  12.     private Deposit target;
  13.     private double amount = Double.MIN_VALUE;
  14.     private String description;
  15.     private boolean executed = false;
  16.     private boolean fetched = false;
  17.  
  18.     public StandardTransaction(int id) {
  19.         this.id = id;
  20.     }
  21.  
  22.     public StandardTransaction(Deposit source, Deposit target, double amount, String description) {
  23.         this.source = source;
  24.         this.target = target;
  25.         this.amount = amount;
  26.         this.description = description;
  27.         setFetched(true);
  28.     }
  29.  
  30.     @Override
  31.     public long getId() {
  32.         return id;
  33.     }
  34.  
  35.     @Override
  36.     public Deposit getSource() {
  37.         fetch();
  38.         return source;
  39.     }
  40.  
  41.     @Override
  42.     public Deposit getTarget() {
  43.         fetch();
  44.         return target;
  45.     }
  46.  
  47.     @Override
  48.     public double getAmount() {
  49.         fetch();
  50.         return amount;
  51.     }
  52.  
  53.     @Override
  54.     public String getDescription() {
  55.         fetch();
  56.         return description;
  57.     }
  58.  
  59.     @Override
  60.     public boolean isExecuted() {
  61.         return executed;
  62.     }
  63.  
  64.     public void setExecuted(boolean executed) {
  65.         this.executed = executed;
  66.     }
  67.  
  68.     public boolean isFetched() {
  69.         return fetched;
  70.     }
  71.  
  72.     public void setFetched(boolean fetched) {
  73.         this.fetched = fetched;
  74.     }
  75.  
  76.     public Transaction fetch() {
  77.         if (isFetched()) return this;
  78.  
  79.         try (Connection connection = StandardEconomyService.getConnection()) {
  80.             PreparedStatement statement = connection.prepareStatement("SELECT * FROM transactions WHERE id = ?");
  81.             statement.setInt(1, id);
  82.             ResultSet result = statement.executeQuery();
  83.  
  84.             if (result.next()) {
  85.                 source = new StandardDeposit(result.getInt("source"));
  86.                 target = new StandardDeposit(result.getInt("target"));
  87.                 amount = result.getDouble("amount");
  88.                 description = result.getString("description");
  89.             }
  90.         } catch (SQLException e) {
  91.             e.printStackTrace();
  92.         }
  93.  
  94.         setFetched(true);
  95.         return this;
  96.     }
  97.  
  98.     public Transaction save() {
  99.         try (Connection connection = StandardEconomyService.getConnection()) {
  100.             PreparedStatement statement = connection.prepareStatement("INSERT INTO transactions (source, target, amount, description) VALUES (?, ?, ?, ?)");
  101.             statement.setInt(1, source.getId());
  102.             statement.setInt(2, target.getId());
  103.             statement.setDouble(3, amount);
  104.             statement.setString(4, description);
  105.             statement.execute();
  106.             setExecuted(true);
  107.         } catch (SQLException e) {
  108.             e.printStackTrace();
  109.         }
  110.  
  111.         return this;
  112.     }
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement