KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > airsent > presentation > admin > AdminDetails


1 /*
2  * Copyright (c) 1999-2001 Lutris Technologies, Inc. All Rights
3  * Reserved.
4  *
5  * This source code file is distributed by Lutris Technologies, Inc. for
6  * use only by licensed users of product(s) that include this source
7  * file. Use of this source file or the software that uses it is covered
8  * by the terms and conditions of the Lutris Enhydra Development License
9  * Agreement included with this product.
10  *
11  * This Software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
12  * ANY KIND, either express or implied. See the License for the specific terms
13  * governing rights and limitations under the License.
14  *
15  * Contributor(s):
16  *
17  * $Id: AdminDetails.java,v 1.1 2004/08/16 09:33:16 slobodan Exp $
18  */

19
20 package com.lutris.airsent.presentation.admin;
21
22 import com.lutris.airsent.presentation.BasePO;
23 import org.enhydra.xml.xmlc.*;
24 import com.lutris.appserver.server.httpPresentation.*;
25 import com.lutris.appserver.server.session.*;
26
27 import org.w3c.dom.*;
28 import org.w3c.dom.html.*;
29
30
31 import com.lutris.airsent.spec.AirSentException;
32
33 import com.lutris.airsent.presentation.AirSentPresentationException;
34 import com.lutris.airsent.presentation.AirSentConstants;
35 import java.util.*;
36
37 import com.lutris.airsent.spec.delivery.Delivery;
38 import com.lutris.airsent.spec.messenger.Messenger;
39 import com.lutris.airsent.spec.*;
40 /**
41  * Admin Details.java provides Delivery details to the admin,
42  * as well as allowing the Admin to assign a messenger.
43  *
44  */

45 public class AdminDetails extends BasePO {
46
47     /**
48      * Constants
49      */

50     private static final int AUTH_LEVEL = AirSentConstants.ADMINISTRATOR_USER;
51
52     /**
53      * Superclass method override.
54      * returns the authorization level necessary to access this po.
55      *
56      * @return int required authorization level
57      */

58     public int getRequiredAuthLevel() {
59     return AUTH_LEVEL;
60     }
61
62     /**
63      * Default event. Just show the page.
64      */

65     public XMLObject handleDefault() throws HttpPresentationException {
66     return showPage(null);
67     }
68
69     /**
70      *
71      * @param error messages
72      * @return XMLObject page.
73      */

74     public XMLObject showPage(String JavaDoc errorMsg)
75         throws HttpPresentationException {
76     AdminDetailsHTML page = (AdminDetailsHTML) myComms.xmlcFactory.create(AdminDetailsHTML.class);
77     HTMLInputElement deliveryField = page.getElementDelivery();
78
79     try {
80         String JavaDoc deliveryID = getComms().request.getParameter(AirSentConstants.DELIVERYID);
81  
82             HomeManager hf = getApplication().getHomeManager();
83         Delivery delivery = hf.getDeliveryManager().findByHandle(deliveryID);
84         
85         deliveryField.setValue(deliveryID);
86         if (null != errorMsg || null != (errorMsg = getSessionData().getAndClearUserMessage())) {
87         page.setTextErrorText(errorMsg);
88         } else {
89         page.getElementErrorText().getParentNode().removeChild(page.getElementErrorText());
90         }
91             
92            
93             
94         page.setTextPickupname(delivery.getPickUp().getName());
95         page.setTextPickupaddress(delivery.getPickUp().getStreet1());
96         page.setTextDropoffname(delivery.getDropOff().getName());
97         page.setTextDropoffaddress(delivery.getDropOff().getStreet1());
98
99             Messenger messenger = delivery.getMessenger();
100             String JavaDoc messengerName = AirSentConstants.NOT_ASSIGNED;
101             
102             HTMLSelectElement messengerSelect = page.getElementMessengerSelect();
103             HTMLOptionElement messengerOption = page.getElementMessengerOption();
104             messengerOption.removeAttribute("id");
105             messengerOption.removeChild(messengerOption.getFirstChild());
106             
107         Messenger[] messengerList = hf.getMessengerManager().findAll();
108             int selected = 0;
109         for (int i = 0; i < messengerList.length; i++) {
110                 HTMLOptionElement clonedOption = (HTMLOptionElement) messengerOption.cloneNode(true);
111                 String JavaDoc badge = messengerList[i].getBadge();
112                 String JavaDoc name = messengerList[i].getFirstName();
113         if (messenger != null) {
114                     if (badge.equals(messenger.getBadge()) == true) {
115                         messengerName = name;
116                         selected = i + 1;
117                     }
118                 }
119         clonedOption.setValue(badge);
120         Node optionTextNode = clonedOption.getOwnerDocument().createTextNode(name);
121         clonedOption.appendChild(optionTextNode);
122         messengerSelect.appendChild(clonedOption);
123         }
124             if (messenger == null) {
125                 HTMLOptionElement clonedOption = (HTMLOptionElement) messengerOption.cloneNode(true);
126                 clonedOption.setValue("none");
127         Node optionTextNode = clonedOption.getOwnerDocument().createTextNode(AirSentConstants.NOT_ASSIGNED);
128         clonedOption.appendChild(optionTextNode);
129         messengerSelect.appendChild(clonedOption);
130                 selected = messengerList.length + 1;
131             }
132             messengerSelect.setSelectedIndex(selected);
133             messengerOption.getParentNode().removeChild(messengerOption);
134             page.setTextDropoffmessenger(messengerName);
135 /*
136  * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run AirSent_pres )
137  * We need to allow AirSent_pres to be functional , response
138  * will be default HTML page with message
139  *
140  */

141            
142         }catch(NullPointerException JavaDoc e){
143                 page.setTextErrorText("You cannot assign or change the messenger assigned to a given order while running AirSent_pres");
144     } catch (Exception JavaDoc ex) {
145         throw new AirSentPresentationException("Error adding messenger", ex);
146     }
147     return page;
148     
149     }
150
151     /**
152      * Process the handleEdit event.
153      *
154      * @return page XMLObject
155      */

156     public XMLObject handleEdit() throws HttpPresentationException {
157     String JavaDoc messengerID = getComms().request.getParameter(AirSentConstants.MESSENGER_NAME);
158     String JavaDoc deliveryID = getComms().request.getParameter(AirSentConstants.DELIVERYID);
159     Messenger messenger = null;
160     Delivery delivery = null;
161
162     if ((messengerID != null) && (messengerID.equals("none") == true)) {
163         return showPage("Select a messenger");
164     }
165         
166     try {
167         if ((delivery = getApplication().getHomeManager().getDeliveryManager().findByHandle(deliveryID)) != null) {
168         if ((messenger = getApplication().getHomeManager().getMessengerManager().findByBadge(messengerID)) != null) {
169             sendMessage(delivery, messenger);
170             delivery.setMessenger(messenger);
171             delivery.save();
172             
173             throw new ClientPageRedirectException(AirSentConstants.ADMIN_MAIN_PAGE);
174         }
175         }
176     }catch(NullPointerException JavaDoc e){return showPage("You cannot assign or change the messenger assigned to a given order , while running AirSent_pres");
177     } catch (Exception JavaDoc ex) {
178         throw new AirSentPresentationException("Error adding messenger",
179                            ex);
180     }
181
182     return showPage("Error occured updating delivery.");
183     }
184
185
186     
187     /**
188      *Informs the given messenger via sms that they have
189      *been assigned a delivery. If the delivery already
190      *had a messenger and is being reassiged, send a
191      *message to both messengers.
192      */

193     private void sendMessage(Delivery delivery, Messenger messenger)
194     throws AirSentPresentationException {
195     
196     Messenger previousMessenger = null;
197     String JavaDoc changeMessage = "You have been un-assigned delivery: ";
198     String JavaDoc newMessage = "You have been assigned delivery: ";
199     
200     try {
201     
202         if((previousMessenger = delivery.getMessenger()) != null) {
203         getApplication().getHomeManager().getSMSManager().send(changeMessage + delivery.getHandle(),
204                                        previousMessenger.getAddress().getLocalNumber());
205         getApplication().getHomeManager().getSMSManager().send(newMessage + delivery.getHandle(),
206                                        messenger.getAddress().getLocalNumber());
207         } else {
208         getApplication().getHomeManager().getSMSManager().send(newMessage + delivery.getHandle(), messenger.getAddress().getLocalNumber());
209         }
210         
211     } catch (Exception JavaDoc ex) {
212         throw new AirSentPresentationException("Error adding messenger",
213                            ex);
214     }
215     }
216
217     
218     /**
219      * this takes an html element, finds the first text child node.
220      * if this doesn't exist it creates a new text node. it then
221      * sets the text in the text node.
222      *
223      * @param Element e - the Parent element that has the text node child
224      * @param String text - the text to set in the text node.
225      */

226     public static void setText(Element e, String JavaDoc text) {
227         if (null == text) {
228             text = "";
229         }
230     
231         org.w3c.dom.Text JavaDoc t = null;
232         try {
233         t = XMLCUtil.getFirstText(e);
234         } catch (Throwable JavaDoc th) {
235         }
236         if (t != null)
237         t.setData(text);
238         else {
239         t = e.getOwnerDocument().createTextNode(text);
240         e.insertBefore(t, null);
241         }
242     }
243
244
245 }
246
247
248
249
250
251
252
Popular Tags