1 7 package org.enhydra.pim.presentation.enhydrapim; 8 9 import java.math.BigDecimal ; 10 import java.util.Vector ; 11 12 import org.enhydra.pim.business.ContactManagerI; 13 import org.enhydra.pim.business.ContactTypeManagerI; 14 import org.enhydra.pim.business.PersonManagerI; 15 import org.enhydra.pim.business.api.ContactI; 16 import org.enhydra.pim.business.api.ContactTypeI; 17 import org.enhydra.pim.business.api.PersonI; 18 import org.enhydra.pim.exception.EnhydraPimException; 19 import org.enhydra.pim.presentation.BasePO; 20 import org.enhydra.pim.spec.ContactManagerFactory; 21 import org.enhydra.pim.spec.ContactTypeManagerFactory; 22 import org.enhydra.pim.spec.PersonManagerFactory; 23 import org.enhydra.xml.xmlc.XMLObject; 24 import org.w3c.dom.Node ; 25 import org.w3c.dom.html.HTMLOptionElement; 26 import org.w3c.dom.html.HTMLTableRowElement; 27 28 import com.lutris.appserver.server.httpPresentation.HttpPresentationException; 29 30 35 public class Edit extends BasePO { 36 37 String ID_FIRSTNAME = "firstName"; 38 39 String ID_LASTNAME = "lastName"; 40 41 String ID_NICKNAME = "nickName"; 42 43 String ID_NOTE = "noteTextArea"; 44 45 String ID_CONTACTTYPE = "contacttype"; 46 47 String ID_CONTACTDATA = "contactData"; 48 49 String ID_ITEMID = "itemId"; 50 51 56 public XMLObject handleDefault() throws HttpPresentationException { 57 if (this.getSessionData() != null && this.getSessionData().getPersonSessionHandle() != null) { 58 return showPage(this.getSessionData().getPersonSessionHandle()); 59 } else { 60 return showErrorPage("Unknown Person"); 61 } 62 } 63 64 69 public XMLObject handleViewEditItem() throws HttpPresentationException { 70 try { 71 PersonI person = null; 72 try { 73 person = getPersonId(); 74 if (person != null) { 75 return showPage(person); 76 } else { 77 return showErrorPage("Person don't exists"); 78 } 79 } catch (EnhydraPimException ex) { 80 return showErrorPage("Unknown person"); 81 } 82 } catch (Exception ex) { 83 return showErrorPage("Application Error :" + ex.getMessage()); 84 } 85 } 86 87 92 public XMLObject handleDetailUpdateItem() throws HttpPresentationException { 93 try { 94 PersonI person = null; 95 try { 96 person = getPersonId(); 97 if (person != null) { 98 return showPage(person); 99 } else { 100 return showErrorPage("Person don't exists"); 101 } 102 } catch (EnhydraPimException ex) { 103 return showErrorPage("Unknown person"); 104 } 105 } catch (Exception ex) { 106 return showErrorPage("Application Error :" + ex.getMessage()); 107 } 108 } 109 110 111 112 117 private PersonI getPersonId() throws HttpPresentationException, EnhydraPimException { 118 try { 119 String itemID = this.getComms().request.getParameter(ID_ITEMID); 120 PersonManagerI personManager = PersonManagerFactory.getPersonManager(); 121 PersonI person = personManager.findPerson(new BigDecimal (itemID)); 122 this.getSessionData().setPersonSessionHandle(person); 123 return person; 124 }catch(Exception ex) { 125 throw new EnhydraPimException(ex.getMessage()); 126 } 127 128 } 129 130 131 136 public XMLObject handleEditAddSubItem() throws HttpPresentationException { 137 String error = "Unhandled Error"; 138 try { 139 String contactType = this.getComms().request.getParameter(ID_CONTACTTYPE); 140 String contactData = this.getComms().request.getParameter(ID_CONTACTDATA); 141 if (contactType != null && contactData != null) { 142 PersonI person = this.getSessionData().getPersonSessionHandle(); 143 ContactTypeManagerI cTMan = ContactTypeManagerFactory.getContactTypeManager(); 144 ContactTypeI cTType = cTMan.findContactType(contactType); 145 ContactManagerI cMan = ContactManagerFactory.getContactManager(); 146 ContactI contact = cMan.newContact(contactData, cTType, person, ""); 147 cMan.addContact(contact,person,cTType); 148 this.getSessionData().setContactDataSessionHandle(contact); 149 return showPage(person); 150 } else { 151 return showErrorPage("Fill all data on form"); 152 } 153 } catch (Exception e) { 154 error = error + e.getMessage(); 155 } 156 return showErrorPage(error); 157 } 158 159 164 public XMLObject handleEditDeleteItem() throws HttpPresentationException { 165 String error = "Unhandled Error: "; 166 try { 167 String item = this.getComms().request.getParameter(ID_ITEMID); 168 if (item != null && item != "") { 169 PersonI person = this.getSessionData().getPersonSessionHandle(); 170 ContactManagerI cMan = ContactManagerFactory.getContactManager(); 171 ContactI contact = cMan.findContact(new BigDecimal (item)); 172 cMan.removeContact(contact); 173 return showPage(person); 174 } else { 175 return showErrorPage("Wrong Item - action canceled"); 176 } 177 } catch (Exception e) { 178 error = error + e.getMessage(); 179 } 180 return showErrorPage(error); 181 } 182 183 188 protected boolean loggedInUserRequired() { 189 return true; 190 } 191 192 196 public XMLObject showPage(PersonI person) throws HttpPresentationException { 197 198 EditHTML page = (EditHTML) myComms.xmlcFactory.create(EditHTML.class); 199 try { 200 page.getElementItemIdA().setValue(person.getHandle().toString()); 201 page.getElementFirstName().setValue(person.getFirstname()); 202 page.getElementLastName().setValue(person.getLastname()); 203 page.getElementNickName().setValue(person.getNicname()); 204 page.getElementNoteTextArea().appendChild(page.createTextNode(person.getNote())); 205 HTMLTableRowElement listRow = page.getElementListView(); 208 Node listTable = listRow.getParentNode(); 209 210 listRow.removeAttribute("id"); 212 213 Vector cntDataVector = ContactManagerFactory.getContactManager().getPersonContacts(person); 214 215 for (int i = 0; i < cntDataVector.size(); i++) { 216 ContactI contact = (ContactI) cntDataVector.elementAt(i); 217 218 page.setTextContactTypeField(contact.getContact_type().getContact_type()); 219 page.setTextContactDataFiled(contact.getContact_data()); 220 page.getElementContactTypeDelete().setAttribute("onClick", 221 "DeleteItem('" + contact.getHandle().toString() + "')"); 222 223 Node clonedListRow = listRow.cloneNode(true); 225 listTable.appendChild(clonedListRow); 226 227 } 228 229 listRow.getParentNode().removeChild(listRow); 230 231 HTMLOptionElement selectRow = page.getElementContactTypeSelectValue(); 232 Node selectField = selectRow.getParentNode(); 233 234 236 selectRow.removeAttribute("id"); 237 238 Vector cntTypeVector = ContactTypeManagerFactory.getContactTypeManager().getContactTypes(); 239 for (int i = 0; i < cntTypeVector.size(); i++) { 240 ContactTypeI cType = (ContactTypeI) cntTypeVector.elementAt(i); 241 selectRow.setAttribute("value", cType.getContact_type()); 242 selectRow.getFirstChild().setNodeValue(cType.getContact_type()); 243 Node clonedRow = selectRow.cloneNode(true); 244 selectField.appendChild(clonedRow); 245 } 246 247 selectRow.getParentNode().removeChild(selectRow); 250 251 } catch (Exception ex) { 252 return showErrorPage("Error during View:" + ex.getMessage()); 253 } 254 return page; 256 } 257 258 } | Popular Tags |