001: /*
002: * projectManagement
003: *
004: * Enhydra super-servlet presentation object
005: *
006: */
007:
008: package projectmanagement.presentation.projects;
009:
010: import projectmanagement.presentation.*;
011: import projectmanagement.spec.customer.*;
012:
013: import org.w3c.dom.*;
014: import org.w3c.dom.html.*;
015:
016: // Enhydra SuperServlet imports
017: import com.lutris.appserver.server.httpPresentation.HttpPresentation;
018: import com.lutris.appserver.server.httpPresentation.HttpPresentationComms;
019: import com.lutris.appserver.server.httpPresentation.HttpPresentationException;
020:
021: // Standard imports
022: import java.io.IOException;
023:
024: /**
025: * Generates the blank HTML page.
026: */
027: public class Controls implements HttpPresentation {
028:
029: /**
030: * Constants
031: */
032: private static String CUSTOMER = "customer";
033:
034: public void run(HttpPresentationComms comms)
035: throws HttpPresentationException, IOException {
036: ControlsHTML page = new ControlsHTML();
037:
038: String customer = comms.request.getParameter(CUSTOMER);
039: HTMLSelectElement sel = page.getElementCboCustomers();
040: fillSelection(page, sel, customer);
041:
042: comms.response.writeDOM(page);
043: }
044:
045: private void fillSelection(ControlsHTML page,
046: HTMLSelectElement cboCustomers, String selectedCustomerID)
047: throws ProjectManagementPresentationException {
048: HTMLOptionElement optCustomer = page.getElementOptCustomer();
049:
050: try {
051:
052: CustomerManager customerManager = CustomerManagerFactory
053: .getCustomerManager("projectmanagement.business.customer.CustomerManagerImpl");
054: Customer[] customers = customerManager.getAllCustomers();
055:
056: // Remove the dummy storyboard text from the prototype HTML
057: optCustomer.removeChild(optCustomer.getFirstChild());
058:
059: // set the text to select all customers - default option
060: HTMLOptionElement clonedOption = (HTMLOptionElement) optCustomer
061: .cloneNode(true);
062: clonedOption.setValue("");
063: String all = "All";
064: Node optionTextNode = clonedOption.getOwnerDocument()
065: .createTextNode(all);
066: clonedOption.appendChild(optionTextNode);
067: clonedOption.setAttribute("selected", "selected");
068: // Do only a shallow copy of the option as we don't want the text child
069: // of the node option
070: cboCustomers.appendChild(clonedOption);
071:
072: // set all customers
073:
074: if (customers != null) {
075: for (int i = 0; i < customers.length; i++) {
076: Customer c = customers[i];
077: // Now populate the combo with customers
078: // This algorithm is obscure because options
079: // are not normal HTML elements
080: // First populate the option value (the customer database ID).
081: // Then append a text child as the option
082: // text, which is what is displayed as the text
083: // in each row of the select box
084: clonedOption = (HTMLOptionElement) optCustomer
085: .cloneNode(true);
086: clonedOption.setValue(c.getHandle());
087: optionTextNode = clonedOption.getOwnerDocument()
088: .createTextNode(c.getCompanyName());
089: clonedOption.appendChild(optionTextNode);
090: if (selectedCustomerID != null
091: && c.getHandle().equals(selectedCustomerID)) {
092: clonedOption.setAttribute("selected",
093: "selected");
094: }
095: // Do only a shallow copy of the option as we don't want the text child
096: // of the node option
097: cboCustomers.appendChild(clonedOption);
098: }
099: }
100: cboCustomers.removeChild(optCustomer);
101:
102: /* Catch Null pointer exception ( we canot make a instances of classes from business layer when we run ProjectManagement_pres )
103: * We need to allow ProjectManagement_pres to be functional , response
104: * will be default HTML page
105: */
106: } catch (NullPointerException ex) {
107:
108: } catch (Exception ex) {
109: //this.writeDebugMsg("Error populating list of customers: " + ex);
110: throw new ProjectManagementPresentationException(
111: "Error populating customer list: ", ex);
112: }
113:
114: }
115:
116: }
|