KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 JavaDoc;
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 /**
28  * @author P.Djojic May 6, 2005 1:27:03 AM
29  *
30  * TODO View
31  */

32 public class View extends BasePO {
33
34     static final String JavaDoc TABLE_LISTVIEW = "listView";
35
36     static final String JavaDoc ID_FIRSTNAME = "firstName";
37     static final String JavaDoc FIRSTNAME = "firstname";
38
39     static final String JavaDoc ID_LASTNAME = "lastName";
40     static final String JavaDoc LASTNAME = "lastname";
41
42     static final String JavaDoc ID_NICKNAME = "nickName";
43     static final String JavaDoc NICKNAME = "nickname";
44
45     static final String JavaDoc ID_NOTE = "note";
46     static final String JavaDoc NOTE = "note";
47
48     static final String JavaDoc ID_ITEMID = "itemId";
49     static final String JavaDoc ITEMID = "itemId";
50
51     /*
52      * (non-Javadoc)
53      *
54      * @see org.enhydra.pim.presentation.BasePO#handleDefault()
55      */

56     public XMLObject handleDefault() throws HttpPresentationException {
57         Vector JavaDoc pageData = null;
58         try {
59             PersonManagerI personManager = PersonManagerFactory.getPersonManager();
60             pageData = personManager.getOwnerPersons(this.getUser());
61         } catch (Exception JavaDoc e) {
62             e.printStackTrace();
63             return showErrorPage("Error during 'Find' operation :" + e.getMessage());
64         }
65         return showPage(pageData);
66     }
67
68     
69     /*
70      * (non-Javadoc)
71      *
72      * @see org.enhydra.pim.presentation.BasePO#handleDefault()
73      */

74     public XMLObject handleViewDeleteItem() throws HttpPresentationException {
75         Vector JavaDoc pageData = null;
76         try {
77             String JavaDoc itemID = this.getComms().request.getParameter(ID_ITEMID);
78             
79             PersonManagerI personManager = PersonManagerFactory.getPersonManager();
80             PersonI person = personManager.findPerson(new BigDecimal JavaDoc(itemID));
81             if(person!=null) {
82                 personManager.removePerson(person);
83             }else {
84                 return showErrorPage("Person don't exists.");
85             }
86         } catch (Exception JavaDoc 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     /*
96      * (non-Javadoc)
97      *
98      * @see org.enhydra.pim.presentation.BasePO#handleDefault()
99      */

100     public XMLObject handleDetailDeleteItem() throws HttpPresentationException {
101         Vector JavaDoc pageData = null;
102         try {
103             String JavaDoc itemID = this.getComms().request.getParameter(ID_ITEMID);
104             
105             PersonManagerI personManager = PersonManagerFactory.getPersonManager();
106             PersonI person = personManager.findPerson(new BigDecimal JavaDoc(itemID));
107             if(person!=null) {
108                 personManager.removePerson(person);
109             }else {
110                 return showErrorPage("Person don't exists.");
111             }
112         } catch (Exception JavaDoc 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     /*
121      * (non-Javadoc)
122      *
123      */

124     protected boolean loggedInUserRequired() {
125         // TODO Auto-generated method stub
126
return true;
127     }
128
129     /*
130      * (non-Javadoc)
131      *
132      */

133     public XMLObject handleAddCancel() throws HttpPresentationException {
134         return handleDefault();
135     }
136
137
138
139     
140     /*
141      * (non-Javadoc)
142      *
143      * @see org.enhydra.pim.presentation.BasePO#handleDefault()
144      */

145     public XMLObject handleEditUpdate() throws HttpPresentationException {
146
147         String JavaDoc firstName = this.getComms().request.getParameter(FIRSTNAME);
148         String JavaDoc lastName = this.getComms().request.getParameter(LASTNAME);
149         String JavaDoc nickName = this.getComms().request.getParameter(NICKNAME);
150         String JavaDoc 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; //user
162
PersonI person = null;
163         PersonManagerI pManager = null;
164
165         try {
166             user = this.getSessionData().getUser();
167             try {
168                 person = getPersonId();
169             } catch (Exception JavaDoc 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 JavaDoc e) {
185             return showErrorPage("Error during database writing");
186         }
187         return showPageDefault();
188         
189     }
190     
191     
192     /**
193      * @return
194      * @throws HttpPresentationException
195      * @throws EnhydraPimException
196      */

197     private PersonI getPersonId() throws HttpPresentationException, EnhydraPimException {
198         try {
199         String JavaDoc itemID = this.getComms().request.getParameter(ID_ITEMID);
200         PersonManagerI personManager = PersonManagerFactory.getPersonManager();
201         PersonI person = personManager.findPerson(new BigDecimal JavaDoc(itemID));
202         this.getSessionData().setPersonSessionHandle(person);
203         return person;
204         }catch(Exception JavaDoc ex) {
205             throw new EnhydraPimException(ex.getMessage());
206         }
207     }
208     
209
210     /*
211      * (non-Javadoc)
212      *
213      */

214     public XMLObject handleEditCancelUpdate() throws HttpPresentationException {
215         return handleDefault();
216     }
217
218     /*
219      * (non-Javadoc)
220      *
221      */

222     public XMLObject handleFindFind() throws HttpPresentationException {
223
224         String JavaDoc firstName = this.getComms().request.getParameter(Find.FIRSTNAME);
225         String JavaDoc lastName = this.getComms().request.getParameter(Find.LASTNAME) ;
226         String JavaDoc nickName = this.getComms().request.getParameter(Find.NICKNAME) ;
227         String JavaDoc contactType = this.getComms().request.getParameter(Find.CONTACTTYPE);
228         String JavaDoc contactData = this.getComms().request.getParameter(Find.CONTACTDATA);
229         String JavaDoc note = null;
230         Vector JavaDoc 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 JavaDoc e) {
237             e.printStackTrace();
238             return showErrorPage("Error during 'Find' operation :" + e.getMessage());
239         }
240         return showPage(pageData);
241     }
242
243     /*
244      * (non-Javadoc)
245      *
246      */

247     public XMLObject handleFindCancel() throws HttpPresentationException {
248         return handleDefault();
249     }
250
251     /*
252      * (non-Javadoc)
253      *
254      */

255     public XMLObject handleLoginLogin() throws HttpPresentationException {
256         return handleDefault();
257     }
258
259     
260     /**
261      * display page
262      *
263      * @return html document
264      * @exception HttpPresentationException
265      */

266     public XMLObject showPageDefault() throws HttpPresentationException {
267         OwnerI user = null; //user
268
XMLObject page = null;
269         user = this.getSessionData().getUser();
270         try {
271             if (user != null) {
272                 PersonManagerI personManager = PersonManagerFactory.getPersonManager();
273                 Vector JavaDoc pageData = personManager.getOwnerPersons(user);
274                 page = showPage(pageData);
275             } else {
276                 return showErrorPage("Error: unknown user");
277             }
278         } catch (Exception JavaDoc e) {
279             return showErrorPage("Error during 'View' operation :" + e.getMessage());
280         }
281         return page;
282     }
283     
284     
285     /*
286      * (non-Javadoc)
287      *
288      */

289     public XMLObject showPage(Vector JavaDoc pageData) throws HttpPresentationException {
290         
291         ViewHTML page = (ViewHTML) myComms.xmlcFactory.create(ViewHTML.class);
292         try {
293
294             HTMLTableRowElement listRow = page.getElementListView();
295             Node JavaDoc listTable = listRow.getParentNode();
296
297             // Remove ids to prevent duplicates
298
listRow.removeAttribute("id");
299
300             // Remove id attributes from the cells in the template row
301
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             // // Remove the dummy storyboard text from the prototype HTML
316
// templateOption.removeChild(templateOption.getFirstChild());
317

318             // Get collection of Discs and loop through collection
319
// to add each disc as a row in the table.
320
for (int i = 0; i < pageData.size(); i++) {
321                 PersonI person = (PersonI) pageData.elementAt(i);
322                 // set text of new cells to values from string array
323

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                 // Add a deep clone of the row to the DOM
338
Node JavaDoc clonedListRow = listRow.cloneNode(true);
339                 listTable.appendChild(clonedListRow);
340                 // Alternative way to insert nodes below
341
// insertBefore(newNode, oldNode);
342
// discTable.insertBefore(clonedNode, templateRow);
343
}
344
345             // Finally remove the template row and template select option
346
// from the page
347
listRow.getParentNode().removeChild(listRow);
348         } catch (Exception JavaDoc ex) {
349             return showErrorPage("Error during View:"+ex.getMessage());
350         }
351         // write out HTML
352
return page;
353     }
354
355 }
Popular Tags