KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > pim > presentation > enhydrapim > Edit


1 /*
2  * Created on May 6, 2005
3  *
4  * TODO To change the template for this generated file go to Window -
5  * Preferences - Java - Code Style - Code Templates
6  */

7 package org.enhydra.pim.presentation.enhydrapim;
8
9 import java.math.BigDecimal JavaDoc;
10 import java.util.Vector JavaDoc;
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 JavaDoc;
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 /**
31  * @author P.Djojic May 6, 2005 1:25:47 AM
32  *
33  * TODO Edit
34  */

35 public class Edit extends BasePO {
36
37     String JavaDoc ID_FIRSTNAME = "firstName";
38
39     String JavaDoc ID_LASTNAME = "lastName";
40
41     String JavaDoc ID_NICKNAME = "nickName";
42
43     String JavaDoc ID_NOTE = "noteTextArea";
44
45     String JavaDoc ID_CONTACTTYPE = "contacttype";
46
47     String JavaDoc ID_CONTACTDATA = "contactData";
48
49     String JavaDoc ID_ITEMID = "itemId";
50
51     /*
52      * (non-Javadoc)
53      *
54      * @see org.enhydra.pim.presentation.BasePO#handleDefault()
55      */

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     /*
65      * (non-Javadoc)
66      *
67      * @see org.enhydra.pim.presentation.BasePO#handleDefault()
68      */

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 JavaDoc ex) {
83             return showErrorPage("Application Error :" + ex.getMessage());
84         }
85     }
86
87     /*
88      * (non-Javadoc)
89      *
90      * @see org.enhydra.pim.presentation.BasePO#handleDefault()
91      */

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 JavaDoc ex) {
106             return showErrorPage("Application Error :" + ex.getMessage());
107         }
108     }
109     
110     
111     
112     /**
113      * @return
114      * @throws HttpPresentationException
115      * @throws EnhydraPimException
116      */

117     private PersonI getPersonId() throws HttpPresentationException, EnhydraPimException {
118         try {
119             String JavaDoc itemID = this.getComms().request.getParameter(ID_ITEMID);
120             PersonManagerI personManager = PersonManagerFactory.getPersonManager();
121             PersonI person = personManager.findPerson(new BigDecimal JavaDoc(itemID));
122             this.getSessionData().setPersonSessionHandle(person);
123             return person;
124         }catch(Exception JavaDoc ex) {
125             throw new EnhydraPimException(ex.getMessage());
126         }
127
128     }
129     
130
131     /*
132      * (non-Javadoc)
133      *
134      * @see org.enhydra.pim.presentation.BasePO#handleDefault()
135      */

136     public XMLObject handleEditAddSubItem() throws HttpPresentationException {
137         String JavaDoc error = "Unhandled Error";
138         try {
139             String JavaDoc contactType = this.getComms().request.getParameter(ID_CONTACTTYPE);
140             String JavaDoc 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 JavaDoc e) {
154             error = error + e.getMessage();
155         }
156         return showErrorPage(error);
157     }
158
159     /*
160      * (non-Javadoc)
161      *
162      * @see org.enhydra.pim.presentation.BasePO#handleDefault()
163      */

164     public XMLObject handleEditDeleteItem() throws HttpPresentationException {
165         String JavaDoc error = "Unhandled Error: ";
166         try {
167             String JavaDoc 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 JavaDoc(item));
172                 cMan.removeContact(contact);
173                 return showPage(person);
174             } else {
175                 return showErrorPage("Wrong Item - action canceled");
176             }
177         } catch (Exception JavaDoc e) {
178             error = error + e.getMessage();
179         }
180         return showErrorPage(error);
181     }
182
183     /*
184      * (non-Javadoc)
185      *
186      * @see org.enhydra.pim.presentation.BasePO#loggedInUserRequired()
187      */

188     protected boolean loggedInUserRequired() {
189         return true;
190     }
191
192     /*
193      * (non-Javadoc)
194      *
195      */

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 // page.getElementNoteTextArea().setDefaultValue(person.getNote());
206
// page.setTextNoteTextAreaField(person.getNote());
207
HTMLTableRowElement listRow = page.getElementListView();
208             Node JavaDoc listTable = listRow.getParentNode();
209
210             // Remove ids to prevent duplicates
211
listRow.removeAttribute("id");
212
213             Vector JavaDoc 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                 // Add a deep clone of the row to the DOM
224
Node JavaDoc clonedListRow = listRow.cloneNode(true);
225                 listTable.appendChild(clonedListRow);
226
227             }
228
229             listRow.getParentNode().removeChild(listRow);
230
231             HTMLOptionElement selectRow = page.getElementContactTypeSelectValue();
232             Node JavaDoc selectField = selectRow.getParentNode();
233
234             // Remove ids to prevent duplicates
235

236             selectRow.removeAttribute("id");
237
238             Vector JavaDoc 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 JavaDoc clonedRow = selectRow.cloneNode(true);
244                 selectField.appendChild(clonedRow);
245             }
246
247             // Finally remove the template row and template select option
248
// from the page
249
selectRow.getParentNode().removeChild(selectRow);
250
251         } catch (Exception JavaDoc ex) {
252             return showErrorPage("Error during View:" + ex.getMessage());
253         }
254         // write out HTML
255
return page;
256     }
257
258 }
Popular Tags