KickJava   Java API By Example, From Geeks To Geeks.

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


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

23 public class CannedMessageManagementForm extends ActionForm
24 {
25
26     /** Holds value of property id. */
27     private String JavaDoc id;
28
29     /** Holds value of property submit. */
30     private String JavaDoc submit;
31
32     /** Holds value of property group. */
33     private String JavaDoc group;
34
35     /** Holds value of property description. */
36     private String JavaDoc description;
37
38     /** Holds value of property message. */
39     private String JavaDoc message;
40
41     /** Holds value of property userGroups. */
42     private ArrayList userGroups = new ArrayList();
43
44     /** Creates a new instance of CustomerManagementForm */
45     public CannedMessageManagementForm()
46     {
47         reset();
48     }
49
50     /**
51      * Getter for property id.
52      *
53      * @return Value of property id.
54      *
55      */

56     public String JavaDoc getId()
57     {
58         return this.id;
59     }
60
61     /**
62      * Setter for property id.
63      *
64      * @param id
65      * New value of property id.
66      *
67      */

68     public void setId(String JavaDoc id)
69     {
70         this.id = id.trim();
71     }
72
73     /**
74      * Getter for property submit.
75      *
76      * @return Value of property submit.
77      *
78      */

79     public String JavaDoc getSubmit()
80     {
81         return this.submit;
82     }
83
84     /**
85      * Setter for property submit.
86      *
87      * @param submit
88      * New value of property submit.
89      *
90      */

91     public void setSubmit(String JavaDoc submit)
92     {
93         this.submit = submit;
94     }
95
96     public ActionErrors validate(ActionMapping mapping,
97             HttpServletRequest JavaDoc request)
98     {
99         // Check for mandatory data
100
ActionErrors errors = new ActionErrors();
101
102         try
103         {
104             if ((id == null) || (id.length() == 0))
105             {
106                 errors.add("id", new ActionError("error.cannedmessage.no.id"));
107             }
108
109             if (DataCheckUtility.followsTableIdRules(id) == false)
110             {
111                 errors.add("id", new ActionError(
112                         "error.cannedmessage.invalid.id"));
113             }
114
115             // for create or modify
116
if ((submit.equals("Create") == true)
117                     || (submit.equals("Modify") == true))
118             {
119                 if ((group == null) || (group.length() == 0))
120                 {
121                     errors.add("group", new ActionError(
122                             "error.cannedmessage.no.group"));
123                 }
124
125                 if ((description == null) || (description.length() == 0))
126                 {
127                     errors.add("description", new ActionError(
128                             "error.cannedmessage.no.description"));
129                 }
130
131                 if ((message == null) || (message.length() == 0))
132                 {
133                     errors.add("message", new ActionError(
134                             "error.cannedmessage.no.content"));
135                 }
136                 else
137                 {
138                     String JavaDoc dtd_file = request.getSession().getServletContext()
139                             .getRealPath("")
140                             + File.separator
141                             + "aceapp"
142                             + File.separator
143                             + "data" + File.separator + "canned_message.dtd";
144
145                     validateXML(message, dtd_file, errors);
146                 }
147             }
148
149             if (errors.size() > 0)
150             {
151                 Connection c = (Connection) request.getSession().getAttribute(
152                         "connection");
153                 AccountElement element = (AccountElement) request.getSession()
154                         .getAttribute("userInfo");
155
156                 GroupTable groups = new GroupTable();
157                 groups.setConnection(c);
158                 if (element.isAdminLevel() == true)
159                 {
160                     ArrayList group_list = groups.list();
161                     if (group_list != null)
162                     {
163                         ArrayList list = new ArrayList();
164                         Iterator iter = group_list.iterator();
165
166                         while (iter.hasNext() == true)
167                         {
168                             String JavaDoc group = (String JavaDoc) iter.next();
169                             list.add(new LabelValueBean(group, URLEncoder
170                                     .encode(group, "UTF-8")));
171                         }
172
173                         list.add(0, new LabelValueBean("all", URLEncoder
174                                 .encode("all", "UTF-8")));
175                         list.add(0, new LabelValueBean("", URLEncoder.encode(
176                                 "", "UTF-8")));
177                         setUserGroups(list);
178                     }
179                 }
180                 else
181                 {
182                     ArrayList group_list = groups.list(element.getDomain());
183                     if (group_list != null)
184                     {
185                         ArrayList list = new ArrayList();
186                         Iterator iter = group_list.iterator();
187
188                         while (iter.hasNext() == true)
189                         {
190                             String JavaDoc group = (String JavaDoc) iter.next();
191                             list.add(new LabelValueBean(group, URLEncoder
192                                     .encode(group, "UTF-8")));
193                         }
194
195                         list.add(0, new LabelValueBean("", URLEncoder.encode(
196                                 "", "UTF-8")));
197                         setUserGroups(list);
198                     }
199                 }
200             }
201         }
202         catch (Exception JavaDoc e)
203         {
204             errors.add("id", new ActionError("error.internal.error"));
205         }
206         return errors;
207     }
208
209     private void validateXML(String JavaDoc input, String JavaDoc dtd_file,
210             final ActionErrors errors)
211     {
212         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
213
214         // set various configuration options
215
dbf.setValidating(true);
216         dbf.setIgnoringComments(true);
217         dbf.setIgnoringElementContentWhitespace(true);
218         dbf.setCoalescing(true);
219         dbf.setNamespaceAware(true);
220
221         try
222         {
223             //Create the parser and set its error handler
224
DocumentBuilder doc_builder = dbf.newDocumentBuilder();
225
226             doc_builder.setErrorHandler(new org.xml.sax.ErrorHandler JavaDoc()
227             {
228                 public void fatalError(SAXParseException e)
229                 {
230                     errors.add("message", new ActionError(
231                             "error.cannedmessage.erroneous.content",
232                             new Integer JavaDoc(e.getLineNumber() - 3), new String JavaDoc(e
233                                     .getLocalizedMessage())));
234                 }
235
236                 public void error(SAXParseException e)
237                 {
238                     errors.add("message", new ActionError(
239                             "error.cannedmessage.erroneous.content",
240                             new Integer JavaDoc(e.getLineNumber() - 3), new String JavaDoc(e
241                                     .getLocalizedMessage())));
242                 }
243
244                 public void warning(SAXParseException e)
245                 {
246                     errors.add("message", new ActionError(
247                             "error.cannedmessage.erroneous.content",
248                             new Integer JavaDoc(e.getLineNumber() - 3), new String JavaDoc(e
249                                     .getLocalizedMessage())));
250                 }
251
252             }
253
254             );
255
256             File file = new File(dtd_file);
257             URL url = file.toURL();
258
259             // if the entered text does not begin with a <element>, then add the
260
// <text>
261
// automatically
262
if (input.trim().startsWith("<") == false)
263             {
264                 input = "<text>" + AceXMLHelper.encodeXMLString(input)
265                         + "</text>";
266             }
267
268             // create an input source to be parsed
269
StringBuffer JavaDoc buf = new StringBuffer JavaDoc(
270                     "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
271             buf.append("<!DOCTYPE canned-msg SYSTEM \"" + url.toString()
272                     + "\">\n");
273             buf.append("<canned-msg>\n");
274             buf.append(input);
275             buf.append("</canned-msg>");
276             String JavaDoc element = buf.toString();
277
278             //Parse the input
279
Document doc = doc_builder.parse(new ByteArrayInputStream(element
280                     .getBytes()));
281         }
282         catch (ParserConfigurationException ex)
283         {
284             errors.add("message", new ActionError(
285                     "error.cannedmessage.processing.content"));
286
287             AceLogger.Instance().log(
288                     AceLogger.ERROR,
289                     AceLogger.SYSTEM_LOG,
290                     "CannedMessageManagementForm.validateXML(): XML Parser Error: "
291                             + ex.getMessage()
292                             + ", while processing user canned message input: "
293                             + input);
294         }
295         catch (SAXException ex)
296         {
297         }
298         catch (MalformedURLException ex)
299         {
300             errors.add("message", new ActionError(
301                     "error.cannedmessage.processing.content"));
302         }
303         catch (IOException ex)
304         {
305             errors.add("message", new ActionError(
306                     "error.cannedmessage.processing.content"));
307
308             AceLogger.Instance().log(
309                     AceLogger.ERROR,
310                     AceLogger.SYSTEM_LOG,
311                     "CannedMessageManagementForm.validateXML(): IO error accessing DTD file: "
312                             + dtd_file + ", error: " + ex.getMessage());
313         }
314     }
315
316     public void reset()
317     {
318         id = null;
319         group = null;
320         description = null;
321         message = null;
322         submit = "Find";
323     }
324
325     /**
326      * Getter for property group.
327      *
328      * @return Value of property group.
329      *
330      */

331     public String JavaDoc getGroup()
332     {
333         return this.group;
334     }
335
336     /**
337      * Setter for property group.
338      *
339      * @param group
340      * New value of property group.
341      *
342      */

343     public void setGroup(String JavaDoc group)
344     {
345         this.group = group.trim();
346     }
347
348     /**
349      * Getter for property description.
350      *
351      * @return Value of property description.
352      *
353      */

354     public String JavaDoc getDescription()
355     {
356         return this.description;
357     }
358
359     /**
360      * Setter for property description.
361      *
362      * @param description
363      * New value of property description.
364      *
365      */

366     public void setDescription(String JavaDoc description)
367     {
368         this.description = description.trim();
369     }
370
371     /**
372      * Getter for property message.
373      *
374      * @return Value of property message.
375      *
376      */

377     public String JavaDoc getMessage()
378     {
379         return this.message;
380     }
381
382     /**
383      * Setter for property message.
384      *
385      * @param message
386      * New value of property message.
387      *
388      */

389     public void setMessage(String JavaDoc message)
390     {
391         this.message = message.trim();
392     }
393
394     /**
395      * Getter for property userGroups.
396      *
397      * @return Value of property userGroups.
398      *
399      */

400     public ArrayList getUserGroups()
401     {
402         return this.userGroups;
403     }
404
405     /**
406      * Setter for property userGroups.
407      *
408      * @param userGroups
409      * New value of property userGroups.
410      *
411      */

412     public void setUserGroups(ArrayList userGroups)
413     {
414         this.userGroups = userGroups;
415     }
416
417 }
Popular Tags