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.PersonManagerI; 13 import org.enhydra.pim.business.api.OwnerI; 14 import org.enhydra.pim.business.api.PersonI; 15 import org.enhydra.pim.exception.EnhydraPimException; 16 import org.enhydra.pim.presentation.BasePO; 17 import org.enhydra.pim.spec.PersonManagerFactory; 18 import org.enhydra.xml.xmlc.XMLObject; 19 import org.w3c.dom.Node ; 20 import org.w3c.dom.html.HTMLElement; 21 import org.w3c.dom.html.HTMLInputElement; 22 import org.w3c.dom.html.HTMLTableRowElement; 23 24 import com.lutris.appserver.server.httpPresentation.ClientPageRedirectException; 25 import com.lutris.appserver.server.httpPresentation.HttpPresentationException; 26 27 32 public class View extends BasePO { 33 34 static final String TABLE_LISTVIEW = "listView"; 35 36 static final String ID_FIRSTNAME = "firstName"; 37 static final String FIRSTNAME = "firstname"; 38 39 static final String ID_LASTNAME = "lastName"; 40 static final String LASTNAME = "lastname"; 41 42 static final String ID_NICKNAME = "nickName"; 43 static final String NICKNAME = "nickname"; 44 45 static final String ID_NOTE = "note"; 46 static final String NOTE = "note"; 47 48 static final String ID_ITEMID = "itemId"; 49 static final String ITEMID = "itemId"; 50 51 56 public XMLObject handleDefault() throws HttpPresentationException { 57 Vector pageData = null; 58 try { 59 PersonManagerI personManager = PersonManagerFactory.getPersonManager(); 60 pageData = personManager.getOwnerPersons(this.getUser()); 61 } catch (Exception e) { 62 e.printStackTrace(); 63 return showErrorPage("Error during 'Find' operation :" + e.getMessage()); 64 } 65 return showPage(pageData); 66 } 67 68 69 74 public XMLObject handleViewDeleteItem() throws HttpPresentationException { 75 Vector pageData = null; 76 try { 77 String itemID = this.getComms().request.getParameter(ID_ITEMID); 78 79 PersonManagerI personManager = PersonManagerFactory.getPersonManager(); 80 PersonI person = personManager.findPerson(new BigDecimal (itemID)); 81 if(person!=null) { 82 personManager.removePerson(person); 83 }else { 84 return showErrorPage("Person don't exists."); 85 } 86 } catch (Exception e) { 87 e.printStackTrace(); 88 return showErrorPage("Error during 'Find' operation :" + e.getMessage()); 89 } 90 throw new ClientPageRedirectException(Common.REDIRECT_VIEW); 91 } 92 93 94 95 100 public XMLObject handleDetailDeleteItem() throws HttpPresentationException { 101 Vector pageData = null; 102 try { 103 String itemID = this.getComms().request.getParameter(ID_ITEMID); 104 105 PersonManagerI personManager = PersonManagerFactory.getPersonManager(); 106 PersonI person = personManager.findPerson(new BigDecimal (itemID)); 107 if(person!=null) { 108 personManager.removePerson(person); 109 }else { 110 return showErrorPage("Person don't exists."); 111 } 112 } catch (Exception e) { 113 e.printStackTrace(); 114 return showErrorPage("Error during 'Find' operation :" + e.getMessage()); 115 } 116 throw new ClientPageRedirectException(Common.REDIRECT_VIEW); 117 } 118 119 120 124 protected boolean loggedInUserRequired() { 125 return true; 127 } 128 129 133 public XMLObject handleAddCancel() throws HttpPresentationException { 134 return handleDefault(); 135 } 136 137 138 139 140 145 public XMLObject handleEditUpdate() throws HttpPresentationException { 146 147 String firstName = this.getComms().request.getParameter(FIRSTNAME); 148 String lastName = this.getComms().request.getParameter(LASTNAME); 149 String nickName = this.getComms().request.getParameter(NICKNAME); 150 String note = this.getComms().request.getParameter(NOTE); 151 152 if (nickName == null) 153 nickName = ""; 154 if (note == null) 155 note = ""; 156 157 if ((firstName == null || lastName == null) || (firstName.length() == 0 || firstName.length() == 0)) { 158 return showErrorPage("FirstName and LastName can not be empty"); 159 } 160 161 OwnerI user = null; PersonI person = null; 163 PersonManagerI pManager = null; 164 165 try { 166 user = this.getSessionData().getUser(); 167 try { 168 person = getPersonId(); 169 } catch (Exception ex) { 170 return showErrorPage("Unknown person"); 171 } 172 person.setFirstname(firstName); 173 person.setLastname(lastName); 174 person.setNicname(nickName); 175 person.setNote(note); 176 177 if (user != null) { 178 pManager = PersonManagerFactory.getPersonManager(); 179 person = pManager.updatePerson(person, user); 180 this.getSessionData().setPersonSessionHandle(person); 181 } else { 182 return showErrorPage("Invalid access"); 183 } 184 } catch (Exception e) { 185 return showErrorPage("Error during database writing"); 186 } 187 return showPageDefault(); 188 189 } 190 191 192 197 private PersonI getPersonId() throws HttpPresentationException, EnhydraPimException { 198 try { 199 String itemID = this.getComms().request.getParameter(ID_ITEMID); 200 PersonManagerI personManager = PersonManagerFactory.getPersonManager(); 201 PersonI person = personManager.findPerson(new BigDecimal (itemID)); 202 this.getSessionData().setPersonSessionHandle(person); 203 return person; 204 }catch(Exception ex) { 205 throw new EnhydraPimException(ex.getMessage()); 206 } 207 } 208 209 210 214 public XMLObject handleEditCancelUpdate() throws HttpPresentationException { 215 return handleDefault(); 216 } 217 218 222 public XMLObject handleFindFind() throws HttpPresentationException { 223 224 String firstName = this.getComms().request.getParameter(Find.FIRSTNAME); 225 String lastName = this.getComms().request.getParameter(Find.LASTNAME) ; 226 String nickName = this.getComms().request.getParameter(Find.NICKNAME) ; 227 String contactType = this.getComms().request.getParameter(Find.CONTACTTYPE); 228 String contactData = this.getComms().request.getParameter(Find.CONTACTDATA); 229 String note = null; 230 Vector pageData = null; 231 232 try { 233 PersonManagerI personManager = PersonManagerFactory.getPersonManager(); 234 pageData = personManager.findPersonsByQuery(this.getUser(), firstName, lastName, nickName, note, 235 contactType, contactData); 236 } catch (Exception e) { 237 e.printStackTrace(); 238 return showErrorPage("Error during 'Find' operation :" + e.getMessage()); 239 } 240 return showPage(pageData); 241 } 242 243 247 public XMLObject handleFindCancel() throws HttpPresentationException { 248 return handleDefault(); 249 } 250 251 255 public XMLObject handleLoginLogin() throws HttpPresentationException { 256 return handleDefault(); 257 } 258 259 260 266 public XMLObject showPageDefault() throws HttpPresentationException { 267 OwnerI user = null; XMLObject page = null; 269 user = this.getSessionData().getUser(); 270 try { 271 if (user != null) { 272 PersonManagerI personManager = PersonManagerFactory.getPersonManager(); 273 Vector pageData = personManager.getOwnerPersons(user); 274 page = showPage(pageData); 275 } else { 276 return showErrorPage("Error: unknown user"); 277 } 278 } catch (Exception e) { 279 return showErrorPage("Error during 'View' operation :" + e.getMessage()); 280 } 281 return page; 282 } 283 284 285 289 public XMLObject showPage(Vector pageData) throws HttpPresentationException { 290 291 ViewHTML page = (ViewHTML) myComms.xmlcFactory.create(ViewHTML.class); 292 try { 293 294 HTMLTableRowElement listRow = page.getElementListView(); 295 Node listTable = listRow.getParentNode(); 296 297 listRow.removeAttribute("id"); 299 300 HTMLElement firstNameCellTemplate = page.getElementFirstName(); 302 HTMLElement lastNameCellTemplate = page.getElementLastName(); 303 HTMLElement nickNameCellTemplate = page.getElementNickName(); 304 HTMLElement noteCellTemplate = page.getElementNote(); 305 HTMLInputElement editItemEditTemplate = page.getElementEditItemEdit(); 306 HTMLInputElement editItemDeleteTemplate = page.getElementEditItemDelete(); 307 308 firstNameCellTemplate.removeAttribute("id"); 309 lastNameCellTemplate.removeAttribute("id"); 310 nickNameCellTemplate.removeAttribute("id"); 311 noteCellTemplate.removeAttribute("id"); 312 editItemEditTemplate.removeAttribute("id"); 313 editItemDeleteTemplate.removeAttribute("id"); 314 315 318 for (int i = 0; i < pageData.size(); i++) { 321 PersonI person = (PersonI) pageData.elementAt(i); 322 324 page.setTextFirstName(person.getFirstname()); 325 page.setTextLastName(person.getLastname()); 326 page.setTextNickName(person.getNicname()); 327 328 page.setTextNote(person.getNote().length() > 20 ? person.getNote().substring(0, 17) + "..." : person 329 .getNote()); 330 page.getElementEditItemEdit().setAttribute("onClick", 331 "editItem('" + person.getHandle().toString() + "')"); 332 page.getElementEditItemDetail().setAttribute("onClick", 333 "detailItem('" + person.getHandle().toString() + "')"); 334 page.getElementEditItemDelete().setAttribute("onClick", 335 "deleteItem('" + person.getHandle().toString() + "')"); 336 337 Node clonedListRow = listRow.cloneNode(true); 339 listTable.appendChild(clonedListRow); 340 } 344 345 listRow.getParentNode().removeChild(listRow); 348 } catch (Exception ex) { 349 return showErrorPage("Error during View:"+ex.getMessage()); 350 } 351 return page; 353 } 354 355 } | Popular Tags |