KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > application > communicator > applications > webtalk > controller > CannedMessageManagementAction


1
2 package com.quikj.application.communicator.applications.webtalk.controller;
3
4 import javax.servlet.http.*;
5 import org.apache.struts.action.*;
6 import org.apache.struts.util.*;
7 import java.sql.*;
8 import java.util.*;
9 import java.io.UnsupportedEncodingException JavaDoc;
10 import java.net.*;
11
12 import com.quikj.application.communicator.admin.model.*;
13 import com.quikj.application.communicator.admin.controller.*;
14 import com.quikj.application.communicator.applications.webtalk.model.*;
15 import com.quikj.server.framework.*;
16 /**
17  *
18  * @author bhm
19  */

20 public class CannedMessageManagementAction extends Action
21 {
22     
23     /** Creates a new instance of CannedMessageManagementAction */
24     public CannedMessageManagementAction()
25     {
26     }
27     
28     public ActionForward execute(ActionMapping mapping,
29     ActionForm form,
30     HttpServletRequest request,
31     HttpServletResponse response) throws UnsupportedEncodingException JavaDoc
32     {
33         CannedMessageManagementForm uform = (CannedMessageManagementForm)form;
34         
35         ActionErrors errors = new ActionErrors();
36         
37         Connection c = (Connection)request.getSession().getAttribute("connection");
38         if (c == null)
39         {
40             errors.add(ActionErrors.GLOBAL_ERROR,
41             new ActionError("error.not.logged.in"));
42             saveErrors(request, errors);
43             return mapping.findForward("logon");
44         }
45         
46         AccountElement element = (AccountElement)request.getSession().getAttribute("userInfo");
47         if ((element.isAdminLevel() == false) && (element.isCustomerLevel() == false))
48         {
49             errors.add(ActionErrors.GLOBAL_ERROR,
50             new ActionError("error.insufficient.privilege"));
51             saveErrors(request, errors);
52             
53             return mapping.findForward("main_menu");
54         }
55         
56         // allow only admin level to access all data
57
// when move to MySQL 4.0.4 or above, can use multi-table update/delete and eliminate group_constraint
58
String JavaDoc domain_constraint = null;
59         ArrayList group_constraint = null;
60                 
61         GroupTable groups = new GroupTable();
62         groups.setConnection(c);
63         if (element.isAdminLevel() == true)
64         {
65             ArrayList group_list = groups.list();
66             if (group_list != null)
67             {
68                 ArrayList list = new ArrayList(group_list.size() + 2);
69                 list.add(0, new LabelValueBean("", URLEncoder.encode("", "UTF-8")));
70                 list.add(1, new LabelValueBean("all", URLEncoder.encode("all", "UTF-8")));
71                 
72                 Iterator iter = group_list.iterator();
73                 
74                 while (iter.hasNext() == true)
75                 {
76                     String JavaDoc group = (String JavaDoc)iter.next();
77                     list.add(new LabelValueBean(group, URLEncoder.encode(group, "UTF-8")));
78                 }
79                               
80                 uform.setUserGroups(list);
81             }
82         }
83         else
84         {
85             group_constraint = new ArrayList();
86             domain_constraint = element.getDomain();
87             
88             ArrayList group_list = groups.list(element.getDomain());
89             if (group_list != null)
90             {
91                 ArrayList list = new ArrayList(group_list.size() + 1);
92                 list.add(0, new LabelValueBean("", URLEncoder.encode("", "UTF-8")));
93                 group_constraint.add("");
94                 
95                 Iterator iter = group_list.iterator();
96                 
97                 while (iter.hasNext() == true)
98                 {
99                     String JavaDoc group = (String JavaDoc)iter.next();
100                     group_constraint.add(group);
101                     list.add(new LabelValueBean(group, URLEncoder.encode(group, "UTF-8")));
102                 }
103                                                 
104                 uform.setUserGroups(list);
105             }
106         }
107                 
108         if (uform.getSubmit().equals("Find") == true)
109         {
110             CannedMessageTable tbl = new CannedMessageTable();
111             tbl.setConnection(c);
112             
113             CannedMessageElement e = tbl.query(uform.getId(), domain_constraint);
114             
115             if (e == null)
116             {
117                 errors.add(ActionErrors.GLOBAL_ERROR,
118                 new ActionError("error.cannedmessage.not.exist"));
119                 
120                 if (tbl.getErrorMessage() != null)
121                 {
122                     AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
123                     "CannedMessageManagementAction.execute()/Find/by-"
124                     + element.getName()
125                     + ": "
126                     + tbl.getErrorMessage());
127                 }
128             }
129             else
130             {
131                 uform.setGroup(URLEncoder.encode(e.getGroup(), "UTF-8"));
132                 uform.setDescription(e.getDescription());
133                 
134                 String JavaDoc message = e.getMessage().trim();
135                 if (message.startsWith("<text>") == true)
136                 {
137                     message = AceXMLHelper.decodeXMLString(message.substring("<text>".length(),
138                     message.indexOf("</text>")));
139                 }
140                 
141                 uform.setMessage(message);
142                 uform.setId(e.getId());
143             }
144         }
145         else if (uform.getSubmit().equals("Modify") == true)
146         {
147             CannedMessageTable tbl = new CannedMessageTable();
148             tbl.setConnection(c);
149             
150             CannedMessageElement e = new CannedMessageElement();
151             e.setGroup(URLDecoder.decode(uform.getGroup(), "UTF-8"));
152             e.setDescription(uform.getDescription());
153             
154             String JavaDoc message = uform.getMessage().trim();
155             if (message.startsWith("<") == false)
156             {
157                 message = "<text>" + AceXMLHelper.encodeXMLString(message) + "</text>";
158             }
159             
160             e.setMessage(message);
161             e.setId(uform.getId());
162             
163             if (tbl.modify(e, group_constraint) == false)
164             {
165                 if (tbl.getErrorMessage() == null)
166                 {
167                     errors.add(ActionErrors.GLOBAL_ERROR,
168                     new ActionError("error.cannedmessage.not.exist"));
169                 }
170                 else
171                 {
172                     errors.add(ActionErrors.GLOBAL_ERROR,
173                     new ActionError("error.db.failure"));
174                     
175                     AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
176                     "CannedMessageManagementAction.execute()/Modify/by-"
177                     + element.getName()
178                     + ": "
179                     + tbl.getErrorMessage());
180                 }
181             }
182             else
183             {
184                 AceLogger.Instance().log(AceLogger.INFORMATIONAL, AceLogger.USER_LOG,
185                 "User " + element.getName() + " modified webtalk canned message " +
186                 uform.getId());
187                 
188                 // forward control to the webtalk main menu
189
ActionMessages messages = new ActionMessages();
190                 messages.add(ActionMessages.GLOBAL_MESSAGE,
191                 new ActionMessage("message.cannedmessage.modified"));
192                 
193                 saveMessages(request, messages);
194                 return mapping.findForward("webtalk_main_menu");
195             }
196         }
197         else if (uform.getSubmit().equals("Create") == true)
198         {
199             CannedMessageTable tbl = new CannedMessageTable();
200             tbl.setConnection(c);
201             
202             CannedMessageElement e = new CannedMessageElement();
203             
204             e.setGroup(URLDecoder.decode(uform.getGroup(), "UTF-8"));
205             e.setDescription(uform.getDescription());
206             
207             String JavaDoc message = uform.getMessage().trim();
208             if (message.startsWith("<") == false)
209             {
210                 message = "<text>" + AceXMLHelper.encodeXMLString(message) + "</text>";
211             }
212             e.setMessage(message);
213             e.setId(uform.getId());
214             
215             if (tbl.create(e) == false)
216             {
217                 errors.add(ActionErrors.GLOBAL_ERROR,
218                 new ActionError("error.cannedmessage.create.failure"));
219                 
220                 AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
221                 "CannedMessageManagementAction.execute()/Create/by-"
222                 + element.getName()
223                 + ": "
224                 + tbl.getErrorMessage());
225             }
226             else
227             {
228                 AceLogger.Instance().log(AceLogger.INFORMATIONAL, AceLogger.USER_LOG,
229                 "User " + element.getName() + " created webtalk canned message " +
230                 uform.getId());
231                 
232                 // forward control to the webtalk main menu
233
ActionMessages messages = new ActionMessages();
234                 messages.add(ActionMessages.GLOBAL_MESSAGE,
235                 new ActionMessage("message.cannedmessage.created"));
236                 saveMessages(request, messages);
237                 
238                 return mapping.findForward("webtalk_main_menu");
239             }
240         }
241         else if (uform.getSubmit().equals("Delete") == true)
242         {
243             CannedMessageTable tbl = new CannedMessageTable();
244             tbl.setConnection(c);
245             
246             if (tbl.delete(uform.getId(), group_constraint) == false)
247             {
248                 if (tbl.getErrorMessage() == null)
249                 {
250                     errors.add(ActionErrors.GLOBAL_ERROR,
251                     new ActionError("error.cannedmessage.not.exist"));
252                 }
253                 else
254                 {
255                     errors.add(ActionErrors.GLOBAL_ERROR,
256                     new ActionError("error.db.failure"));
257                     
258                     AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
259                     "CannedMessageManagementAction.execute()/Delete/by-"
260                     + element.getName()
261                     + ": "
262                     + tbl.getErrorMessage());
263                 }
264             }
265             else
266             {
267                 AceLogger.Instance().log(AceLogger.INFORMATIONAL, AceLogger.USER_LOG,
268                 "User " + element.getName() + " deleted webtalk canned message " +
269                 uform.getId());
270                 
271                 // forward control to the webtalk main menu
272
ActionMessages messages = new ActionMessages();
273                 messages.add(ActionMessages.GLOBAL_MESSAGE,
274                 new ActionMessage("message.cannedmessage.deleted"));
275                 saveMessages(request, messages);
276                 
277                 return mapping.findForward("webtalk_main_menu");
278             }
279         }
280         
281         if (errors.isEmpty() == false)
282         {
283             saveErrors(request, errors);
284         }
285         
286         // add related tasks to the navigation bar
287
WebTalkRelatedTasks menu = new WebTalkRelatedTasks();
288         menu.addLink(new LinkAttribute("Search canned messages", "display_canned_message_search"));
289         request.setAttribute("menu", menu);
290         
291         return mapping.getInputForward();
292     }
293     
294 }
295
Popular Tags