001: package auction.controller;
002:
003: import auction.dao.BookDAO;
004: import auction.dao.CategoryDAO;
005: import auction.model.Bid;
006: import com.xoetrope.swing.XCheckCombo;
007: import com.xoetrope.swing.XCheckList;
008: import com.xoetrope.swing.list.XAltListCellRenderer;
009: import java.util.ArrayList;
010: import java.util.List;
011: import javax.swing.ListSelectionModel;
012: import javax.swing.tree.TreePath;
013: import net.xoetrope.optional.annotation.Find;
014: import net.xoetrope.optional.data.pojo.XPersistentListHelper;
015: import net.xoetrope.optional.data.pojo.XPersistentPojoModel;
016:
017: import auction.dao.BookDAO;
018: import auction.dao.CategoryDAO;
019: import auction.dao.DAOFactory;
020: import auction.dao.UserDAO;
021: import auction.model.Category;
022: import auction.model.Language;
023: import auction.model.User;
024: import auction.model.Book;
025: import java.util.Collection;
026: import java.util.Date;
027: import net.xoetrope.optional.data.pojo.XPersistenceController;
028: import net.xoetrope.optional.data.pojo.XPojoHelper;
029: import net.xoetrope.optional.data.pojo.XPojoModel;
030: import net.xoetrope.xui.*;
031: import net.xoetrope.swing.*;
032: import net.xoetrope.xui.data.XDataBinding;
033: import net.xoetrope.xui.data.XListBinding;
034: import net.xoetrope.xui.data.XModel;
035: import net.xoetrope.xui.data.XModelAdapter;
036:
037: /**
038: * A controller of the SeachPage.
039: */
040: public class Search extends CEPage {
041: // height of the result list cell.
042: private static final int CELL_HEIGHT = 50;
043:
044: // DAOs
045: private BookDAO bookDAO;
046:
047: // model nodes
048: private XPersistentPojoModel resultListModel;
049: private XPersistentPojoModel maxBidModel;
050: private XPersistentPojoModel newBidAmountModel;
051:
052: // components
053: @Find
054: private XCheckList languageList;
055: @Find
056: private XTree categoryTree;
057: @Find
058: private XEdit authorEdit;
059: @Find
060: private XEdit titleEdit;
061: @Find
062: private XCheckbox myAuctionsCheckbox;
063: @Find
064: private XCheckbox activeAuctionsCheckbox;
065: @Find
066: private XList resultList;
067: @Find
068: private XButton showDetailsButton;
069:
070: // helpers
071: private XPersistentListHelper resultListHelper;
072:
073: public Search() {
074: super ();
075: }
076:
077: protected void init() {
078: super .init();
079: // create DAOs
080: bookDAO = factory.getBookDAO();
081: // get model node references
082: resultListModel = (XPersistentPojoModel) pojoHelper
083: .get("hibernateDAOFactory/bookDAO/resultList");
084: maxBidModel = (XPersistentPojoModel) pojoHelper
085: .get("hibernateDAOFactory/bookDAO/maxBid");
086: newBidAmountModel = (XPersistentPojoModel) pojoHelper
087: .get("hibernateDAOFactory/bookDAO/newBidAmount");
088: }
089:
090: public void pageCreated() {
091: XAltListCellRenderer listRenderer = new ResultListCellRenderer(
092: resultListModel);
093: // set the result list renderer
094: resultList.setCellRenderer(listRenderer);
095: resultList.setFixedCellHeight(CELL_HEIGHT);
096: resultList
097: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
098: resultListHelper = new XPersistentListHelper(resultList, this ,
099: "pojo/hibernateDAOFactory/bookDAO/selectedBook");
100: // set selection mode
101: resultList
102: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
103: }
104:
105: public void pageActivated() {
106: // select the first language on the language list
107: if (languageList.getItemCount() > 0)
108: languageList.selectItem(0, true);
109: }
110:
111: /**
112: * "Search" button action handler.
113: */
114: public void search() {
115: try {
116: // explicitly start a new transaction
117: pojoContext.beginWorkUnit(false);
118:
119: List<Book> result = bookDAO.search(getSelectedTitle(),
120: getSelectedAuthor(), getSelectedLanguageNames(),
121: getAllSubcategoryIds(), getUser(), getOnlyActive());
122: resultListModel.set(result);
123:
124: resultList.clearSelection();
125: resultListHelper.updateBinding();
126:
127: pojoContext.endWorkUnit();
128: } catch (Exception ex) {
129: handleException(ex);
130: }
131: }
132:
133: /**
134: * Invoked whenever the selection on the result list is changed.
135: */
136: public void resultListSelectionChanged() {
137: if (!resultListHelper.selectionChanged())
138: return;
139: setMaxBid();
140: showDetailsButton.setEnabled(!resultList.isSelectionEmpty());
141: }
142:
143: /**
144: * Shows the "book details" page
145: */
146: public void showDetailsPage() {
147: Toolbar toolbar = (Toolbar) pageMgr.getPage("Toolbar");
148: toolbar.showDetailsPage();
149: }
150:
151: /**
152: * Gets all selected nodes from the specified tree.
153: * @param tree the XTree object whose selected nodes
154: * will be returned
155: * @return table containing selected model nodes.
156: */
157: private XModel[] getSelectedNodes(XTree tree) {
158: TreePath[] paths = tree.getSelectionPaths();
159: XModel[] nodes = new XModel[paths != null ? paths.length : 0];
160: for (int i = 0; i < nodes.length; i++)
161: nodes[i] = ((XModelAdapter) paths[i].getLastPathComponent())
162: .getModel();
163: return nodes;
164: }
165:
166: /**
167: * Retrieves the max bid of the selected book from
168: * the database and stores it into a "maxBid" model node.
169: */
170: private void setMaxBid() {
171: if (resultListHelper.selectionEmpty())
172: return; // nothing's selected on the result list
173:
174: try {
175: // explicitly start a new db transaction
176: pojoContext.beginWorkUnit(false);
177:
178: Book selectedBook = (Book) resultListHelper
179: .getSelectedPojo();
180: Bid maxBid = bookDAO.getMaxBid(selectedBook.getId());
181: long bidAmount = (maxBid != null ? maxBid.getAmount() : 0);
182: newBidAmountModel.set(bidAmount + 1);
183: maxBidModel.set(maxBid);
184:
185: pojoContext.endWorkUnit();
186: } catch (Exception ex) {
187: handleException(ex);
188: }
189: }
190:
191: /**
192: * Gets the book title from the search from
193: * @return the book title
194: */
195: private String getSelectedTitle() {
196: String title = titleEdit.getText();
197: return (title.length() > 0 ? title : null);
198: }
199:
200: /**
201: * Gets the author from the search form
202: * @return the author
203: */
204: private String getSelectedAuthor() {
205: String author = authorEdit.getText();
206: return (author.length() > 0 ? author : null);
207: }
208:
209: /**
210: * Gets whether the "activeAuctionsCheckbox" is checked
211: * @return true if the checkbox is checked, false otherwise
212: */
213: public boolean getOnlyActive() {
214: return (activeAuctionsCheckbox.isSelected());
215: }
216:
217: /**
218: * Gets the user show auction are to be shown in
219: * the result list.
220: * @return User the current user if the "myAuctionCheckBox" is checked,
221: * null otherwise
222: */
223: public User getUser() {
224: return (myAuctionsCheckbox.isSelected() ? (User) currentUserModel
225: .get()
226: : null);
227: }
228:
229: /**
230: * Gets the names of the selected languages
231: * @return table containing the names of the languages
232: */
233: private String[] getSelectedLanguageNames() {
234: return languageList.getSelectedItems();
235: }
236:
237: /**
238: * Gets all subcategories of the specified list of categories
239: * @param categories the list of categories whose subcategories
240: * are to be retrieved
241: * @return list containing the subcategories
242: */
243: private List<Category> getAllSubcategories(List<Category> categories) {
244: List<Category> allCategories = new ArrayList<Category>();
245: while (categories.size() > 0) {
246: allCategories.addAll(categories);
247:
248: List<Category> childCategories = new ArrayList<Category>();
249: for (Category category : categories)
250: childCategories.addAll(category.getChildCategories());
251: categories = childCategories;
252: }
253:
254: return allCategories;
255: }
256:
257: /**
258: * Gets the list of selected categories from the category tree.
259: * @return list containing the selected categories
260: */
261: public List<Category> getSelectedCategories() {
262: XModel[] categoryNodes = getSelectedNodes(categoryTree);
263: List<Category> selectedCategories = new ArrayList<Category>();
264: for (int i = 0; i < categoryNodes.length; i++)
265: selectedCategories.add((Category) categoryNodes[i].get());
266: return selectedCategories;
267: }
268:
269: /**
270: * Gets the identifiers of all selected categories.
271: * @return table containing identifiers of the categories.
272: */
273: private Long[] getAllSubcategoryIds() {
274: List<Category> categories = getAllSubcategories(getSelectedCategories());
275: Long[] categoryIds = new Long[categories.size()];
276: int i = 0;
277: for (Category category : categories)
278: categoryIds[i++] = category.getId();
279:
280: return categoryIds;
281: }
282:
283: }
|