001: package auction.controller;
002:
003: import auction.exceptions.BusinessException;
004: import auction.model.Bid;
005: import java.awt.Rectangle;
006: import java.util.List;
007: import javax.swing.JScrollBar;
008: import javax.swing.ListSelectionModel;
009: import net.xoetrope.optional.annotation.Find;
010: import net.xoetrope.optional.data.pojo.XPersistentListHelper;
011: import net.xoetrope.optional.data.pojo.XPojoModel;
012: import net.xoetrope.xui.*;
013: import net.xoetrope.swing.*;
014:
015: import auction.dao.BookDAO;
016: import auction.dao.CategoryDAO;
017: import auction.dao.DAOFactory;
018: import auction.dao.UserDAO;
019: import auction.model.Category;
020: import auction.model.Language;
021: import auction.model.User;
022: import auction.model.Book;
023: import java.util.Collection;
024: import java.util.Date;
025: import net.xoetrope.optional.data.pojo.XPersistentDataBindingContext;
026: import net.xoetrope.optional.data.pojo.XPojoHelper;
027: import net.xoetrope.optional.data.pojo.XPersistentPojoModel;
028: import net.xoetrope.xui.*;
029: import net.xoetrope.swing.*;
030: import net.xoetrope.xui.data.XListBinding;
031: import net.xoetrope.xui.data.XModel;
032:
033: /**
034: * A controller of the book details page.
035: */
036: public class BookDetails extends CEPage {
037: // DAOs
038: private BookDAO bookDAO;
039:
040: // UI components
041: @Find
042: private XList categoryList;
043: @Find
044: private XList bidList;
045: @Find
046: private XLabel bidderLabel;
047: @Find
048: private XLabel bidAmountLabel;
049: @Find
050: private XLabel dateOfBidLabel;
051: @Find
052: private XImage bookCover;
053: @Find
054: private XLabel stateLabel;
055:
056: // model nodes
057: private XPersistentPojoModel selectedBookModel;;
058: private XPersistentPojoModel maxBidModel;
059: private XPersistentPojoModel newBidAmountModel;
060:
061: // helpers
062: private XPersistentListHelper bidListHelper;
063: private XPersistentListHelper categoryListHelper;
064:
065: public BookDetails() {
066: super ();
067: }
068:
069: protected void init() {
070: super .init();
071: // get model nodes
072: selectedBookModel = (XPersistentPojoModel) pojoHelper
073: .get("hibernateDAOFactory/bookDAO/selectedBook");
074: maxBidModel = (XPersistentPojoModel) pojoHelper
075: .get("hibernateDAOFactory/bookDAO/maxBid");
076: newBidAmountModel = (XPersistentPojoModel) pojoHelper
077: .get("hibernateDAOFactory/bookDAO/newBidAmount");
078: // get DAOs
079: bookDAO = factory.getBookDAO();
080: }
081:
082: public void pageCreated() {
083: // create helpers
084: bidListHelper = new XPersistentListHelper(bidList, this ,
085: "pojo/hibernateDAOFactory/bookDAO/selectedBid");
086: categoryListHelper = new XPersistentListHelper(categoryList,
087: this );
088: // set selection nodes
089: bidList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
090: categoryList
091: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
092: }
093:
094: public void pageActivated() {
095: // remove the selection from the category list.
096: categoryList.clearSelection();
097: categoryList.setFocusable(false);
098: // update the cover of the selected book
099: String imageName = (String) ((XModel) selectedBookModel
100: .get("image")).get();
101: bookCover.setImageName(imageName);
102: bookCover.setSize(bookCover.getPreferredSize()); //@todo tmp???
103: bookCover.repaint();
104: }
105:
106: /**
107: * Shows the "place new bid" dialog.
108: */
109: public void placeBid() {
110: Book selectedBook = (Book) selectedBookModel.get();
111:
112: if (selectedBook.isActive()) {
113: long newBidValue = 0;
114: Bid maxBid = (Bid) maxBidModel.get();
115: if (maxBid != null)
116: newBidValue = (maxBid.getAmount() + 1);
117: else
118: newBidValue = (Long) ((XModel) selectedBookModel
119: .get("initialPrice")).get();
120:
121: // store the value into the model node
122: newBidAmountModel.set(String.valueOf(newBidValue));
123: showPlaceBidDialog(NewBid.PLACE_BID);
124: } else {
125: showMessageDialog("Selected auction is not active");
126: }
127: }
128:
129: /**
130: * Shows the "buy it now dialog"
131: */
132: public void buyItNow() {
133: Book selectedBook = (Book) selectedBookModel.get();
134:
135: if (selectedBook.isActive()) {
136: Long reservePrice = selectedBook.getReservePrice();
137: newBidAmountModel.set(String.valueOf(reservePrice));
138: showPlaceBidDialog(NewBid.BUY_IT_NOW);
139: } else {
140: showMessageDialog("Selected auction is not active");
141: }
142: }
143:
144: /**
145: * Gets the value of the new bid stored in the
146: * "newBidAmountModel" node
147: * @return the value of the new bid
148: */
149: private Long getNewBidAmount() {
150: Long newBidAmount = null;
151: String newBidAmountS = (String) newBidAmountModel.get();
152: try {
153: newBidAmount = Long.parseLong(newBidAmountS);
154: } catch (RuntimeException ex) {
155: newBidAmount = null;
156: }
157: return newBidAmount;
158: }
159:
160: /**
161: * Displays the dialog for placing a new bid.
162: * @param the type of dialog, either "place a new bid" or
163: * "buy it now"
164: */
165: private void showPlaceBidDialog(int type) {
166: String msg = null;
167:
168: // set the dialog caption
169: String caption = "";
170: if (type == NewBid.BUY_IT_NOW)
171: caption = "Buy it now";
172: else if (type == NewBid.PLACE_BID)
173: caption = "Place a new bid";
174:
175: if (showDialog("NewBid", caption) == XDialog.OK_CLICKED) {
176: // get and validate the value of the new bid
177: Long newBidAmount = getNewBidAmount();
178: if (newBidAmount == null) {
179: showMessageDialog("wrong value of the new bid");
180: return;
181: }
182:
183: try {
184: // explicitly start a new db transaction
185: // in order to place a new bid
186: pojoContext.beginWorkUnit(false);
187:
188: selectedBookModel.merge();
189: Book selectedBook = (Book) selectedBookModel.get();
190: Bid maxBid = (Bid) maxBidModel.get();
191: try {
192: Bid newBid = selectedBook.placeBid(
193: getCurrentUser(), newBidAmount.longValue(),
194: maxBid);
195: if (newBid.isSuccessful()) {
196: updateBinding(getBinding(stateLabel));
197: msg = "Congratulations, you have won the auction!";
198: }
199: } catch (BusinessException ex) {
200: msg = ex.getMessage();
201: }
202:
203: // get the max bid from the db to update the UI with it
204: maxBid = bookDAO.getMaxBid(selectedBook.getId());
205: if (maxBid == null)
206: maxBid = new Bid();
207: maxBidModel.set(maxBid);
208:
209: // update the UI
210: bidListHelper.updateListHolder();
211: updateBoundComponentValues();
212: // select the first bid on the list of bids
213: bidList.setSelectedIndex(0);
214:
215: pojoContext.endWorkUnit();
216: } catch (Exception ex) {
217: handleException(ex);
218: }
219: }
220: // check if there's anything to show in the message dialog
221: if (msg != null)
222: showMessageDialog(msg);
223: }
224:
225: /**
226: * Invoked whenever the selection on the
227: * list of bids is changed
228: */
229: public void bidSelectionChanged() {
230: if (bidListHelper.selectionChanged()) {
231: // update bid related components
232: updateBinding(getBinding(bidderLabel));
233: updateBinding(getBinding(bidAmountLabel));
234: updateBinding(getBinding(dateOfBidLabel));
235: }
236: }
237:
238: }
|