001: package projectmanagement.presentation.projects;
002:
003: import projectmanagement.presentation.*;
004: import projectmanagement.spec.*;
005:
006: import projectmanagement.spec.project.*;
007: import projectmanagement.spec.customer.*;
008: import com.lutris.appserver.server.httpPresentation.*;
009: import org.enhydra.xml.xmlc.XMLObject;
010:
011: import org.w3c.dom.*;
012: import org.w3c.dom.html.*;
013:
014: /**
015: * Manages all actions on projects.
016: *
017: * @author Sasa Bojanic
018: * @version 1.0
019: */
020: public class Edit extends BasePO {
021:
022: /**
023: * Constants
024: */
025: private static String CUSTOMER = "cboCustomer";
026: private static String NAME = "txtName";
027: private static String MONEY_PER_HOUR = "txtMoneyPerHour";
028:
029: private static String PROJECT_ID = "projectID";
030: private static String CONTEXT_PAGE = "Context.po";
031: private static String ADD = "add";
032: private static String DELETE = "delete";
033: private static String MODIFY = "modify";
034: private static String DETAILS = "showDetailsPage";
035: private static String EVENT = "event";
036:
037: /**
038: * Superclass method override. Returns 1.
039: */
040: protected int getRequiredAuthLevel() {
041: /*String event="";
042: try {
043: event=this.getComms().request.getParameter(EVENT);
044: } catch (Exception ex) {}
045:
046: if (event!=null && event.equalsIgnoreCase(DETAILS)) {
047: return 1;
048: } else {
049: return 2;
050: }*/
051: return 2;
052: }
053:
054: /**
055: * Default event. Just show the page for editing.
056: */
057: public XMLObject handleDefault() throws HttpPresentationException {
058: return showModifyPage(null, false);
059: }
060:
061: /**
062: * handle show add project page event.
063: *
064: * @return html document
065: * @exception HttpPresentationException
066: */
067: public XMLObject handleShowAddPage()
068: throws HttpPresentationException {
069: return showAddPage(null);
070: }
071:
072: /**
073: * handle show details project page event.
074: *
075: * @return html document
076: * @exception HttpPresentationException
077: */
078: public XMLObject handleShowDetailsPage()
079: throws HttpPresentationException {
080: return showModifyPage(null, true);
081: }
082:
083: /*
084: * Modifies an existing project
085: *
086: * @return html document
087: * @exception HttpPresentationException
088: */
089: public XMLObject handleModify() throws HttpPresentationException {
090: String projectID = this .getComms().request
091: .getParameter(PROJECT_ID);
092: Project project = null;
093:
094: // Try to get the project by its ID
095: try {
096: ProjectManager projectManager = ProjectManagerFactory
097: .getProjectManager("projectmanagement.business.project.ProjectManagerImpl");
098: project = projectManager.findProjectByID(projectID);
099:
100: } catch (Exception ex) {
101: this .getSessionData().setUserMessage(
102: "Please choose a valid project to edit");
103: throw new ClientPageRedirectException(CONTEXT_PAGE);
104: }
105:
106: try {
107: saveProject(project);
108: throw new ClientPageRedirectException(CONTEXT_PAGE);
109: } catch (Exception ex) {
110: return showModifyPage(
111: "To add this project, you must fill out fields properly!",
112: false);
113: }
114: }
115:
116: /*
117: * Adds a project to the project database
118: *
119: * @return html document
120: * @exception HttpPresentationException
121: */
122: public XMLObject handleAdd() throws HttpPresentationException {
123: try {
124: ProjectManager projectManager = ProjectManagerFactory
125: .getProjectManager("projectmanagement.business.project.ProjectManagerImpl");
126: Project project = projectManager.getProject();
127:
128: saveProject(project);
129: throw new ClientPageRedirectException(CONTEXT_PAGE);
130: /*
131: * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run ProjectManagement_pres )
132: * We need to allow ProjectManagement_pres to be functional , response
133: * will be default HTML page with message
134: */
135:
136: } catch (NullPointerException ex) {
137: return showAddPage("You cannot add project while runing ProjectManagement_pres");
138: } catch (Exception ex) {
139: return showAddPage("To add this project, you must fill out fields properly!");
140: }
141: }
142:
143: /*
144: * Deletes a Project from the Project database
145: *
146: * @return html document
147: * @exception HttpPresentationException
148: */
149: public XMLObject handleDelete() throws HttpPresentationException,
150: ProjectManagementPresentationException {
151: String projectID = this .getComms().request
152: .getParameter(PROJECT_ID);
153:
154: try {
155: ProjectManager projectManager = ProjectManagerFactory
156: .getProjectManager("projectmanagement.business.project.ProjectManagerImpl");
157: Project project = projectManager.findProjectByID(projectID);
158:
159: String projectName = project.getName();
160: project.delete();
161: this .getSessionData().setUserMessage(
162: "The project, " + projectName + ", was deleted");
163: // Catch any possible database exception as well as the null pointer
164: // exception if the project is not found and is null after findProjectByID
165: } catch (Exception ex) {
166: this .getSessionData().setUserMessage(
167: "Please choose a valid project to delete");
168: }
169: // Redirect to the catalog page which will display the error message,
170: // if there was one set by the above exception
171: throw new ClientPageRedirectException(CONTEXT_PAGE);
172: }
173:
174: /**
175: * Produce HTML for this PO, populated by the project information
176: * that the user wants to edit
177: *
178: * @param errorMsg, the error messages
179: * @param disabled, if controls are disabled
180: * @return html document
181: * @exception HttpPresentationException
182: */
183: public XMLObject showModifyPage(String errorMsg, boolean disabled)
184: throws HttpPresentationException,
185: ProjectManagementPresentationException {
186:
187: String customer = this .getComms().request
188: .getParameter(CUSTOMER);
189: String name = this .getComms().request.getParameter(NAME);
190: String moneyPerHour = this .getComms().request
191: .getParameter(MONEY_PER_HOUR);
192:
193: String projectID = this .getComms().request
194: .getParameter(PROJECT_ID);
195: // Instantiate the page object
196: EditHTML page = new EditHTML();
197:
198: Project project = null;
199:
200: try {
201: ProjectManager projectManager = ProjectManagerFactory
202: .getProjectManager("projectmanagement.business.project.ProjectManagerImpl");
203: project = projectManager.findProjectByID(projectID);
204: /*
205: * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run ProjectManagement_pres )
206: * We need to allow ProjectManagement_pres to be functional , response
207: * will be default HTML page with message
208: */
209:
210: } catch (NullPointerException ex) {
211: page.setTextErrorText("This is a default HTML page");
212: return page;
213: // Catch any possible database exception in findProjectByID()
214: } catch (ProjectManagementException ex) {
215: this .getSessionData().setUserMessage(
216: "Please choose a valid project to edit");
217: throw new ClientPageRedirectException(CONTEXT_PAGE);
218: }
219:
220: try {
221: // If we received a valid projectID then try to show the project's contents,
222: // otherwise try to use the HTML input parameters
223: page.getElementProjectID().setValue(project.getHandle());
224:
225: HTMLSelectElement sel = page.getElementCboCustomer();
226:
227: if (null == customer || customer.length() == 0) {
228: customer = project.getCustomer().getHandle();
229: }
230:
231: fillSelection(page, sel, customer);
232: sel.setDisabled(disabled);
233:
234: HTMLInputElement el = page.getElementTxtName();
235: el.setDisabled(disabled);
236: if (null != name && name.length() != 0) {
237: el.setValue(name);
238: } else {
239: el.setValue(project.getName());
240: }
241:
242: el = page.getElementTxtMoneyPerHour();
243: el.setDisabled(disabled);
244: if (null != moneyPerHour && moneyPerHour.length() != 0) {
245: el.setValue(moneyPerHour);
246: } else {
247: el.setValue(String.valueOf(project.getMoneyPerHour()));
248: }
249:
250: el = page.getElementBtnSave();
251: el.setDisabled(disabled);
252:
253: if (null == errorMsg) {
254: page.getElementErrorText().getParentNode().removeChild(
255: page.getElementErrorText());
256: } else {
257: page.setTextErrorText(errorMsg);
258: }
259: } catch (ProjectManagementException ex) {
260: throw new ProjectManagementPresentationException(
261: "Error populating page for project editing", ex);
262: }
263:
264: page.getElementEvent().setValue(MODIFY);
265: return page;
266: }
267:
268: /**
269: * Produce HTML for this PO
270: *
271: * @param errorMsg, the error messages
272: * @return html document
273: * @exception HttpPresentationException
274: */
275: public XMLObject showAddPage(String errorMsg)
276: throws HttpPresentationException,
277: ProjectManagementPresentationException {
278:
279: String customer = this .getComms().request
280: .getParameter(CUSTOMER);
281: String name = this .getComms().request.getParameter(NAME);
282: String moneyPerHour = this .getComms().request
283: .getParameter(MONEY_PER_HOUR);
284:
285: // Instantiate the page object
286: EditHTML page = new EditHTML();
287:
288: HTMLSelectElement sel = page.getElementCboCustomer();
289:
290: fillSelection(page, sel, customer);
291:
292: HTMLInputElement el = page.getElementTxtName();
293: if (null != name) {
294: el.setValue(name);
295: } else {
296: el.setValue("");
297: }
298:
299: el = page.getElementTxtMoneyPerHour();
300: if (null != moneyPerHour) {
301: el.setValue(moneyPerHour);
302: } else {
303: el.setValue("");
304: }
305:
306: if (null == errorMsg) {
307: page.getElementErrorText().getParentNode().removeChild(
308: page.getElementErrorText());
309: } else {
310: page.setTextErrorText(errorMsg);
311: }
312:
313: return page;
314: }
315:
316: /**
317: * Method to save a new or existing project to the database
318: *
319: * @param project, the project to be saved
320: * @return html document
321: * @exception HttpPresentationException
322: */
323: protected void saveProject(Project theProject)
324: throws HttpPresentationException {
325:
326: String customer = this .getComms().request
327: .getParameter(CUSTOMER);
328: String name = this .getComms().request.getParameter(NAME);
329: String moneyPerHour = this .getComms().request
330: .getParameter(MONEY_PER_HOUR);
331:
332: if (isNullField(customer) || isNullField(name)
333: || isNullField(moneyPerHour)) {
334: throw new ProjectManagementPresentationException(
335: "Error adding project", new Exception());
336: }
337:
338: try {
339: CustomerManager customerManager = CustomerManagerFactory
340: .getCustomerManager("projectmanagement.business.customer.CustomerManagerImpl");
341:
342: theProject.setCustomer(customerManager
343: .findCustomerByID(customer));
344: theProject.setName(name);
345: theProject
346: .setMoneyPerHour(Double.parseDouble(moneyPerHour));
347:
348: theProject.save();
349: } catch (Exception ex) {
350: throw new ProjectManagementPresentationException(
351: "Error adding project", ex);
352: }
353: }
354:
355: private void fillSelection(EditHTML page,
356: HTMLSelectElement cboCustomers, String selectedCustomerID)
357: throws ProjectManagementPresentationException {
358: //
359: HTMLOptionElement optCustomer = page.getElementOptCustomer();
360:
361: try {
362:
363: CustomerManager customerManager = CustomerManagerFactory
364: .getCustomerManager("projectmanagement.business.customer.CustomerManagerImpl");
365: Customer[] customers = customerManager.getAllCustomers();
366:
367: // Remove the dummy storyboard text from the prototype HTML
368: optCustomer.removeChild(optCustomer.getFirstChild());
369:
370: if (customers != null) {
371: for (int i = 0; i < customers.length; i++) {
372: Customer c = customers[i];
373: // Now populate the combo with customers
374: // This algorithm is obscure because options
375: // are not normal HTML elements
376: // First populate the option value (the customer database ID).
377: // Then append a text child as the option
378: // text, which is what is displayed as the text
379: // in each row of the select box
380: HTMLOptionElement clonedOption = (HTMLOptionElement) optCustomer
381: .cloneNode(true);
382: clonedOption.setValue(c.getHandle());
383: Node optionTextNode = clonedOption
384: .getOwnerDocument().createTextNode(
385: c.getCompanyName());
386: clonedOption.appendChild(optionTextNode);
387: if (c.getHandle().equals(selectedCustomerID)) {
388: clonedOption.setAttribute("selected",
389: "selected");
390: }
391: // Do only a shallow copy of the option as we don't want the text child
392: // of the node option
393: cboCustomers.appendChild(clonedOption);
394: // Alternative way to insert nodes below
395: // insertBefore(newNode, oldNode);
396: // discSelect.insertBefore(clonedOption, templateOption);
397: }
398: }
399: cboCustomers.removeChild(optCustomer);
400: } catch (NullPointerException ex) {
401: } catch (Exception ex) {
402: this .writeDebugMsg("Error populating list of customers: "
403: + ex);
404: throw new ProjectManagementPresentationException(
405: "Error populating customer list: ", ex);
406: }
407:
408: }
409: }
|