KickJava   Java API By Example, From Geeks To Geeks.

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


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.AceLogger;
16
17 /**
18  *
19  * @author bhm
20  */

21 public class CannedMessageSearchAction extends Action
22 {
23     
24     /** Creates a new instance of CannedMessageSearchAction */
25     public CannedMessageSearchAction()
26     {
27     }
28     
29     public ActionForward execute(ActionMapping mapping,
30     ActionForm form,
31     HttpServletRequest request,
32     HttpServletResponse response) throws UnsupportedEncodingException JavaDoc
33     {
34         CannedMessageSearchForm uform = (CannedMessageSearchForm)form;
35         
36         ActionErrors errors = new ActionErrors();
37         ActionMessages messages = new ActionMessages();
38         
39         Connection c = (Connection)request.getSession().getAttribute("connection");
40         if (c == null)
41         {
42             errors.add(ActionErrors.GLOBAL_ERROR,
43             new ActionError("error.not.logged.in"));
44             saveErrors(request, errors);
45             return mapping.findForward("logon");
46         }
47         
48         AccountElement element = (AccountElement)request.getSession().getAttribute("userInfo");
49         if ((element.isAdminLevel() == false) && (element.isCustomerLevel() == false))
50         {
51             errors.add(ActionErrors.GLOBAL_ERROR,
52             new ActionError("error.insufficient.privilege"));
53             saveErrors(request, errors);
54             
55             return mapping.findForward("main_menu");
56         }
57         
58         GroupTable groups = new GroupTable();
59         groups.setConnection(c);
60         if (element.isAdminLevel() == true)
61         {
62             ArrayList group_list = groups.list();
63             if (group_list != null)
64             {
65                 ArrayList list = new ArrayList();
66                 Iterator iter = group_list.iterator();
67                 
68                 while (iter.hasNext() == true)
69                 {
70                     String JavaDoc group = (String JavaDoc)iter.next();
71                     list.add(new LabelValueBean(group, URLEncoder.encode(group, "UTF-8")));
72                 }
73                 
74                 list.add(0, new LabelValueBean("all", URLEncoder.encode("all", "UTF-8")));
75                 list.add(0, new LabelValueBean("", URLEncoder.encode("", "UTF-8")));
76                 uform.setUserGroups(list);
77             }
78         }
79         else
80         {
81             ArrayList group_list = groups.list(element.getDomain());
82             if (group_list != null)
83             {
84                 ArrayList list = new ArrayList();
85                 Iterator iter = group_list.iterator();
86                 
87                 while (iter.hasNext() == true)
88                 {
89                     String JavaDoc group = (String JavaDoc)iter.next();
90                     list.add(new LabelValueBean(group, URLEncoder.encode(group, "UTF-8")));
91                 }
92                 
93                 list.add(0, new LabelValueBean("", URLEncoder.encode("", "UTF-8")));
94                 uform.setUserGroups(list);
95             }
96         }
97         
98                 
99         String JavaDoc domain_constraint = null;
100         if (element.isAdminLevel() == false)
101         {
102             domain_constraint = element.getDomain();
103         }
104         
105         CannedMessageTable tbl = new CannedMessageTable();
106         tbl.setConnection(c);
107         
108         CannedMessageElement e = new CannedMessageElement();
109        
110         String JavaDoc field = uform.getDescription();
111         if ((field != null) && (field.length() > 0))
112         {
113             e.setDescription(field);
114         }
115         
116         field = URLDecoder.decode(uform.getGroup(), "UTF-8");
117         if ((field != null) && (field.length() > 0))
118         {
119             e.setGroup(field);
120         }
121         
122         field = uform.getId();
123         if ((field != null) && (field.length() > 0))
124         {
125             e.setId(field);
126         }
127         
128         field = uform.getMessage();
129         if ((field != null) && (field.length() > 0))
130         {
131             e.setMessage(field);
132         }
133         
134         ArrayList list = tbl.search(e, uform.getSortBy(), domain_constraint);
135         
136         if (list == null)
137         {
138             errors.add(ActionErrors.GLOBAL_ERROR,
139             new ActionError("error.db.failure"));
140             
141             AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
142             "CannedMessageSearchAction.execute()/Search/by-"
143             + element.getName()
144             + ": "
145             + tbl.getErrorMessage());
146         }
147         else
148         {
149             int num_items = list.size();
150             
151             if (num_items == 0)
152             {
153                 messages.add(ActionMessages.GLOBAL_MESSAGE,
154                 new ActionMessage("message.search.empty"));
155             }
156             else
157             {
158                 //store the search result items for the jsp
159
ArrayList name_list = new ArrayList();
160                 for (int i = 0; i < num_items; i++)
161                 {
162                     CannedMessageElement ele = (CannedMessageElement) list.get(i);
163                     
164                     HashMap map = new HashMap();
165                     map.put("id", ele.getId());
166                     map.put("submit", "Find");
167                     map.put("group", ele.getGroup());
168                     name_list.add(map);
169                 }
170                 request.setAttribute("elements", name_list);
171                 
172                 // add related tasks to the navigation bar
173
WebTalkRelatedTasks menu = new WebTalkRelatedTasks();
174                 menu.addLink(new LinkAttribute("Search canned messages", "display_canned_message_search"));
175                 menu.addLink(new LinkAttribute("Administer canned messages", "display_canned_message_management"));
176                 request.setAttribute("menu", menu);
177                 
178                 // forward control to the search result screen
179
return mapping.findForward("canned_message_search_result");
180             }
181         }
182         
183         if (errors.isEmpty() == false)
184         {
185             saveErrors(request, errors);
186         }
187         
188         if (messages.isEmpty() == false)
189         {
190             saveMessages(request, messages);
191         }
192         
193         // add related tasks to the navigation bar
194
WebTalkRelatedTasks menu = new WebTalkRelatedTasks();
195         menu.addLink(new LinkAttribute("Search canned messages", "display_canned_message_search"));
196         menu.addLink(new LinkAttribute("Administer canned messages", "display_canned_message_management"));
197         request.setAttribute("menu", menu);
198         
199         return mapping.getInputForward();
200     }
201 }
202
Popular Tags