/tables/HoldRequest.java

https://github.com/sconealone/cs304 · Java · 418 lines · 213 code · 53 blank · 152 comment · 21 complexity · b39fc36c6fcb17b4f1beff84dba3770e MD5 · raw file

  1. package tables;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.ResultSetMetaData;
  6. import java.sql.SQLException;
  7. import java.text.SimpleDateFormat;
  8. import java.util.ArrayList;
  9. import java.util.Calendar;
  10. import java.util.Collection;
  11. import java.util.GregorianCalendar;
  12. import users.Conn;
  13. /**
  14. * This class represents the HoldRequest table in the database.
  15. *
  16. * @author Christiaan Fernando
  17. *
  18. */
  19. public class HoldRequest implements Table {
  20. private final SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
  21. // The fields for HoldRequest in the table are hid, bid, callNo, issueDate,
  22. // in that order.
  23. private Integer hid;
  24. private Borrower borr;
  25. private Book b;
  26. private Calendar issueDate;
  27. private Connection c;
  28. private PreparedStatement ps;
  29. private ResultSet rs;
  30. /**
  31. * Default Constructor.
  32. */
  33. public HoldRequest() {
  34. c = Conn.getInstance().getConnection();
  35. }
  36. /**
  37. * HoldRequest Constructor.
  38. *
  39. * This constructor takes in a Calendar, Book, Borrower and creates a
  40. * HoldRequest Object. This object has not been added to the SQL table yet,
  41. * so insert() will need to be called on this object in the future if it
  42. * needs to be added.
  43. *
  44. * @param issueDate
  45. * Issue Date for the HoldRequest
  46. * @param b
  47. * Book for the HoldRequest
  48. * @param borr
  49. * Borrower for the HoldRequest
  50. */
  51. public HoldRequest(Borrower borr, Book b, Calendar issueDate) {
  52. this.issueDate = issueDate;
  53. this.b = b;
  54. this.borr = borr;
  55. c = Conn.getInstance().getConnection();
  56. }
  57. /**
  58. * HoldRequest Constructor.
  59. *
  60. * This constructor takes in an Integer and find the HoldRequest entry in
  61. * the SQL table with this hid. This assumes that the entry already exists.
  62. * If it does not, this will return an error value of -1 as the hid.
  63. *
  64. * @param hid
  65. * HoldRequest id in the SQL table.
  66. * @throws SQLException
  67. */
  68. public HoldRequest(Integer hid) throws SQLException {
  69. c = Conn.getInstance().getConnection();
  70. ps = c.prepareStatement("SELECT * FROM HoldRequest WHERE hid = ?");
  71. ps.setInt(1, hid);
  72. rs = ps.executeQuery();
  73. if (rs.next()) {
  74. this.hid = hid;
  75. b = new Book();
  76. borr = new Borrower();
  77. borr.setBid(rs.getInt(2));
  78. borr = (Borrower) borr.get();
  79. b.setCallNumber(rs.getString(4));
  80. b = (Book) b.get();
  81. issueDate = new GregorianCalendar();
  82. this.issueDate.setTime(rs.getDate(3));
  83. } else {
  84. this.hid = -1;
  85. }
  86. }
  87. /**
  88. * Creates a new HoldRequest object based on a result set.
  89. * Call next() on result set before passing in.
  90. * @param rs
  91. * @throws SQLException
  92. */
  93. private HoldRequest(ResultSet rs) throws SQLException {
  94. c = Conn.getInstance().getConnection();
  95. hid = rs.getInt(1);
  96. b = new Book();
  97. borr = new Borrower();
  98. borr.setBid(rs.getInt(2));
  99. borr = (Borrower) borr.get();
  100. b.setCallNumber(rs.getString(4));
  101. b = (Book) b.get();
  102. issueDate = new GregorianCalendar();
  103. this.issueDate.setTime(rs.getDate(3));
  104. }
  105. /**
  106. * Returns a String representation of the table.
  107. *
  108. * Returns a 2-D String representation of the HoldRequest table.
  109. *
  110. */
  111. @Override
  112. public String[][] display() throws SQLException {
  113. String sql = "SELECT * FROM HoldRequest";
  114. ps = c.prepareStatement(sql);
  115. rs = ps.executeQuery();
  116. ResultSetMetaData md = rs.getMetaData();
  117. int numCols = md.getColumnCount();
  118. ArrayList<String[]> requestsGrowable = new ArrayList<String[]>();
  119. String[] header = new String[numCols];
  120. for (int i = 0; i < numCols; i++) {
  121. header[i] = md.getColumnName(i + 1);
  122. }
  123. requestsGrowable.add(header);
  124. int colIndex, paramIndex;
  125. while (rs.next()) {
  126. String[] row = new String[numCols];
  127. colIndex = 0;
  128. paramIndex = 1;
  129. // hid
  130. row[colIndex++] = "" + rs.getInt(paramIndex++);
  131. // bid
  132. row[colIndex++] = "" + rs.getInt(paramIndex++);
  133. //issuedDate
  134. java.sql.Date sqlDate = rs.getDate(paramIndex++);
  135. row[colIndex++] = (sqlDate == null)
  136. ? "null" : sdf.format(sqlDate);
  137. // callNo
  138. row[colIndex++] = rs.getString(paramIndex++);
  139. requestsGrowable.add(row);
  140. }
  141. int numRows = requestsGrowable.size();
  142. String[][] requests = new String[numRows][];
  143. for (int i = 0; i < numRows; i++) {
  144. requests[i] = requestsGrowable.get(i);
  145. }
  146. return requests;
  147. }
  148. /**
  149. * Updates the SQL table.
  150. *
  151. * This updates this HoldRequest object in the HoldRequest table. This
  152. * assumes the item already exists.
  153. */
  154. @Override
  155. public void update() throws SQLException {
  156. ps = c.prepareStatement("UPDATE holdRequest SET bid = ?, issuedDate = ?, callNumber = ? WHERE hid = ?");
  157. ps.setInt(4, hid);
  158. ps.setInt(1, borr.getBid());
  159. ps.setDate(2, new java.sql.Date(issueDate.getTime().getTime()));
  160. ps.setString(3, b.getCallNumber());
  161. int rowCount = ps.executeUpdate();
  162. if (rowCount == 0) {
  163. ps.close();
  164. }
  165. }
  166. /**
  167. * Deletes from the SQL table.
  168. *
  169. * This deletes the HoldRequest object from the HoldRequest table.
  170. */
  171. @Override
  172. public boolean delete() throws SQLException {
  173. ps = c.prepareStatement("DELETE FROM HoldRequest WHERE hid = ?");
  174. ps.setInt(1, hid);
  175. int rowCount = ps.executeUpdate();
  176. return rowCount == 1;
  177. }
  178. /**
  179. * Inserts into the SQL table.
  180. *
  181. * This inserts the HoldRequest object into the HoldRequest table. This
  182. * assumes the item doesn't already exist.
  183. */
  184. @Override
  185. public boolean insert() throws SQLException {
  186. ps = c.prepareStatement("INSERT INTO HoldRequest VALUES (hidCounter.nextVal,?,?,?)");
  187. ps.setInt(1, borr.getBid());
  188. ps.setString(3, b.getCallNumber());
  189. ps.setDate(2, new java.sql.Date(issueDate.getTime().getTime()));
  190. int numRowsChanged = ps.executeUpdate();
  191. if (numRowsChanged == 1) {
  192. ps.close();
  193. ps = c.prepareStatement("SELECT hidCounter.currval FROM DUAL");
  194. ResultSet rs = ps.executeQuery();
  195. if (rs.next()) {
  196. hid = rs.getInt(1);
  197. return true;
  198. }
  199. }
  200. return false;
  201. }
  202. /**
  203. * Return all HoldRequests.
  204. *
  205. * This returns all HoldRequest objects in the SQL database.
  206. *
  207. * @throws SQLException
  208. *
  209. */
  210. @Override
  211. public Collection<Table> getAll() throws SQLException {
  212. Collection<Table> holdRequests = new ArrayList<Table>();
  213. ps = c.prepareStatement("SELECT * FROM HoldRequest");
  214. rs = ps.executeQuery();
  215. while (rs.next()) {
  216. holdRequests.add(new HoldRequest(rs));
  217. }
  218. return holdRequests;
  219. }
  220. /**
  221. * Returns the ResultSetMetaData object for the HoldRequest table.
  222. *
  223. * Returns an object that contains the meta data for the HoldRequest table.
  224. * This is an internal helper method to be used by the display method.
  225. *
  226. * @return
  227. * @throws SQLException
  228. */
  229. public ResultSetMetaData getMeta() throws SQLException {
  230. ps = c.prepareStatement("SELECT * FROM HoldRequest");
  231. rs = ps.executeQuery();
  232. return rs.getMetaData();
  233. }
  234. /**
  235. * Return the HoldRequest object corresponding with the set id.
  236. *
  237. * Given a HoldRequest object with an initialized id field, this returns the
  238. * HoldRequest object with that id field that exists in the SQL database.
  239. * This is used if either the default constructor was called and the
  240. * parameters are required, or if some of the parameters are changed and the
  241. * user wants the original database object.
  242. *
  243. * @throws SQLException
  244. *
  245. */
  246. @Override
  247. public Table get() throws SQLException {
  248. if (hid != 0) {
  249. HoldRequest tempHR = new HoldRequest(hid);
  250. if (tempHR.getHid() > 0) {
  251. return (new HoldRequest((Integer) hid));
  252. }
  253. }
  254. return null;
  255. }
  256. /**
  257. * Return all HoldRequest objects from a given Borrower.
  258. *
  259. * Given a borrower, this returns all the HoldRequests made by that
  260. * borrower.
  261. *
  262. * @param borr
  263. * Borrower whose bid is shared with the HoldRequest
  264. * @return ArrayList of HoldRequests
  265. * @throws SQLException
  266. */
  267. public Collection<Table> getAll(Borrower borr) throws SQLException {
  268. Collection<Table> holdRequests = new ArrayList<Table>();
  269. ps = c.prepareStatement("SELECT * FROM HoldRequest WHERE bid = ?");
  270. ps.setInt(1, borr.getBid());
  271. rs = ps.executeQuery();
  272. while (rs.next()) {
  273. holdRequests.add(new HoldRequest(rs));
  274. }
  275. return holdRequests;
  276. }
  277. /**
  278. * Return all HoldRequest objects for a given Book.
  279. *
  280. * Given a book, this returns all the HoldRequests for a particular book.
  281. *
  282. * @param b
  283. * Book whose callNo is shared with the HoldRequest
  284. * @return ArrayList of HoldRequests
  285. * @throws SQLException
  286. */
  287. public Collection<Table> getAll(Book b) throws SQLException {
  288. Collection<Table> holdRequests = new ArrayList<Table>();
  289. ps = c.prepareStatement("SELECT * FROM HoldRequest WHERE callNumber = ?");
  290. ps.setString(1, b.getCallNumber());
  291. rs = ps.executeQuery();
  292. while (rs.next()) {
  293. holdRequests.add(new HoldRequest(rs));
  294. }
  295. return holdRequests;
  296. }
  297. /**
  298. * @return the hid
  299. */
  300. public Integer getHid() {
  301. return hid;
  302. }
  303. /**
  304. * @param hid
  305. * the hid to set
  306. */
  307. public void setHid(Integer hid) {
  308. this.hid = hid;
  309. }
  310. /**
  311. * @return the issueDate
  312. */
  313. public Calendar getIssueDate() {
  314. return issueDate;
  315. }
  316. /**
  317. * @param issueDate
  318. * the issueDate to set
  319. */
  320. public void setIssueDate(Calendar issueDate) {
  321. this.issueDate = issueDate;
  322. }
  323. /**
  324. * @return the b
  325. */
  326. public Book getB() {
  327. return b;
  328. }
  329. /**
  330. * @param b
  331. * the b to set
  332. */
  333. public void setB(Book b) {
  334. this.b = b;
  335. }
  336. /**
  337. * @return the borr
  338. */
  339. public Borrower getBorr() {
  340. return borr;
  341. }
  342. /**
  343. * @param borr
  344. * the borr to set
  345. */
  346. public void setBorr(Borrower borr) {
  347. this.borr = borr;
  348. }
  349. /**
  350. * Returns the attributes of the class as a string.
  351. * @return
  352. */
  353. @Override
  354. public String toString() {
  355. String holdrequest = "";
  356. holdrequest += "hid = " + hid
  357. + "\ncall number = " + ((b == null)
  358. ? null : b.getCallNumber())
  359. + "\nbid = " + ((borr == null)
  360. ? null : borr.getBid())
  361. + "\nissue date = " + ((issueDate == null)
  362. ? null : sdf.format(issueDate.getTime()));
  363. return holdrequest;
  364. }
  365. }