1 package com.quikj.application.communicator.applications.webtalk.controller; 2 3 import javax.servlet.http.HttpServletRequest ; 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 import javax.xml.parsers.*; 16 import org.xml.sax.*; 17 import org.w3c.dom.*; 18 19 23 public class CannedMessageManagementForm extends ActionForm 24 { 25 26 27 private String id; 28 29 30 private String submit; 31 32 33 private String group; 34 35 36 private String description; 37 38 39 private String message; 40 41 42 private ArrayList userGroups = new ArrayList(); 43 44 45 public CannedMessageManagementForm() 46 { 47 reset(); 48 } 49 50 56 public String getId() 57 { 58 return this.id; 59 } 60 61 68 public void setId(String id) 69 { 70 this.id = id.trim(); 71 } 72 73 79 public String getSubmit() 80 { 81 return this.submit; 82 } 83 84 91 public void setSubmit(String submit) 92 { 93 this.submit = submit; 94 } 95 96 public ActionErrors validate(ActionMapping mapping, 97 HttpServletRequest request) 98 { 99 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 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 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 group = (String ) 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 group = (String ) 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 e) 203 { 204 errors.add("id", new ActionError("error.internal.error")); 205 } 206 return errors; 207 } 208 209 private void validateXML(String input, String dtd_file, 210 final ActionErrors errors) 211 { 212 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 213 214 dbf.setValidating(true); 216 dbf.setIgnoringComments(true); 217 dbf.setIgnoringElementContentWhitespace(true); 218 dbf.setCoalescing(true); 219 dbf.setNamespaceAware(true); 220 221 try 222 { 223 DocumentBuilder doc_builder = dbf.newDocumentBuilder(); 225 226 doc_builder.setErrorHandler(new org.xml.sax.ErrorHandler () 227 { 228 public void fatalError(SAXParseException e) 229 { 230 errors.add("message", new ActionError( 231 "error.cannedmessage.erroneous.content", 232 new Integer (e.getLineNumber() - 3), new String (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 (e.getLineNumber() - 3), new String (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 (e.getLineNumber() - 3), new String (e 249 .getLocalizedMessage()))); 250 } 251 252 } 253 254 ); 255 256 File file = new File(dtd_file); 257 URL url = file.toURL(); 258 259 if (input.trim().startsWith("<") == false) 263 { 264 input = "<text>" + AceXMLHelper.encodeXMLString(input) 265 + "</text>"; 266 } 267 268 StringBuffer buf = new StringBuffer ( 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 element = buf.toString(); 277 278 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 331 public String getGroup() 332 { 333 return this.group; 334 } 335 336 343 public void setGroup(String group) 344 { 345 this.group = group.trim(); 346 } 347 348 354 public String getDescription() 355 { 356 return this.description; 357 } 358 359 366 public void setDescription(String description) 367 { 368 this.description = description.trim(); 369 } 370 371 377 public String getMessage() 378 { 379 return this.message; 380 } 381 382 389 public void setMessage(String message) 390 { 391 this.message = message.trim(); 392 } 393 394 400 public ArrayList getUserGroups() 401 { 402 return this.userGroups; 403 } 404 405 412 public void setUserGroups(ArrayList userGroups) 413 { 414 this.userGroups = userGroups; 415 } 416 417 } | Popular Tags |