1 19 package org.openharmonise.rm.commands; 20 21 import java.io.StringWriter ; 22 import java.util.*; 23 24 import javax.xml.transform.*; 25 import javax.xml.transform.dom.DOMSource ; 26 import javax.xml.transform.stream.StreamResult ; 27 28 import org.openharmonise.commons.dsi.*; 29 import org.openharmonise.commons.net.Email; 30 import org.openharmonise.rm.DataAccessException; 31 import org.openharmonise.rm.config.*; 32 import org.openharmonise.rm.dsi.DataStoreInterfaceFactory; 33 import org.openharmonise.rm.factory.*; 34 import org.openharmonise.rm.metadata.*; 35 import org.openharmonise.rm.publishing.*; 36 import org.openharmonise.rm.resources.*; 37 import org.openharmonise.rm.resources.publishing.Template; 38 import org.openharmonise.rm.resources.xml.XSLResource; 39 import org.w3c.dom.*; 40 41 49 public class CmdEmail extends AbstractCmd { 50 51 54 public static final String TAG_EMAILRECIPIENT = "EmailRecipient"; 55 56 59 public static final String ATTRIB_SEND_TYPE = "sendType"; 60 61 65 static final String USE_SUBMITTED_FORM = "submitted"; 66 67 70 static final String EMAIL_PROPNAME = "RECIPIENT"; 71 72 75 static final String SENDER_PROPNAME = "SENDER"; 76 77 81 static final String EMAILFORMID_PNAME = "publish_using_template_id"; 82 83 86 static final String RECEPIENTLIST_PNAME = "recipient"; 87 88 91 static final String SENDER_PNAME = "sender"; 92 93 96 static final String SUBJECT_PNAME = "subject"; 97 98 101 static final String CONTENTTYPE_PNAME = "content_type"; 102 103 107 static final String XSL_PNAME = "xsl_file"; 108 109 112 static final String OUTTEXT_PNAME = "out_text"; 113 114 117 static final String SENDXML_PNAME = "xml_recipient"; 119 122 static final String SUBJECT_INMARKER = "#!N"; 124 127 static final String SUBJECT_IPMARKER = "#!P:"; 129 132 static final String PNAME_EMAIL_HOST = "EMAIL_HOST"; 133 134 135 139 public CmdEmail() { 140 super(); 141 } 142 143 146 public Object execute(Context context) throws CommandException { 147 int nTemplateId = -1; 148 String publish_using_form_id = this.getParameter(EMAILFORMID_PNAME); 149 String xsl_filename = this.getParameter(XSL_PNAME); 150 String sender_address = this.getParameter(SENDER_PNAME); 151 152 if ((publish_using_form_id != null) && 153 publish_using_form_id.equals(USE_SUBMITTED_FORM)) { 154 String sTemplateId = getParameter(AbstractCmd.PARAM_TEMPLATE_ID); 155 if(sTemplateId != null) { 156 nTemplateId = Integer.parseInt(sTemplateId); 157 } 158 159 } else if (publish_using_form_id != null) { 160 try { 161 nTemplateId = Integer.parseInt(publish_using_form_id); 162 } catch (NumberFormatException e) { 163 throw new RuntimeException ( 164 " The parameter check_with_form is invalid " + ":" + 165 publish_using_form_id + " must be a valid number" + 166 ", or " + USE_SUBMITTED_FORM + 167 " to use the submitted form"); 168 } 169 } else { 170 throw new CommandExecutionException( 171 "A form id must be supplied in order to publish the user" + 172 " parameter:" + EMAILFORMID_PNAME + " must be set."); 173 } 174 175 List recipient_addresses = this.getParameters(RECEPIENTLIST_PNAME); 176 177 Object cmdObj = getCommandObject(context); 178 179 try { 180 if ((recipient_addresses == null) || 181 (recipient_addresses.size() == 0)) { 182 org.w3c.dom.Document state = getState(); 183 Element root = state.getDocumentElement(); 184 185 NodeList nlEmailRecip = root.getElementsByTagName( 186 TAG_EMAILRECIPIENT); 187 188 if (nlEmailRecip.getLength() > 0) { 189 Element elEmailRecip = (Element) nlEmailRecip.item(0); 190 191 if (recipient_addresses == null) { 192 recipient_addresses = new Vector(16); 193 } 194 195 String sSendType = elEmailRecip.getAttribute(ATTRIB_SEND_TYPE); 196 197 if (sSendType.equalsIgnoreCase("test")) { 198 Text txt = (Text) elEmailRecip.getFirstChild(); 199 200 if (txt.getNodeValue().indexOf("@") < 0) { 201 throw new CommandException( 202 "Need valid email address."); 203 } 204 205 recipient_addresses.add(txt.getNodeValue()); 206 } else if (sSendType.equalsIgnoreCase("user")) { 207 208 Profile pro = ((AbstractProfiledObject) cmdObj).getProfile(); 209 210 AbstractDataStoreInterface dbintrf = DataStoreInterfaceFactory.getDataStoreInterface(); 211 212 213 } 214 } 215 else { 216 AbstractProfiledObject obj = (AbstractProfiledObject)cmdObj ; 218 Profile pro = obj.getProfile() ; 219 AbstractPropertyInstance prop = pro.getPropertyInstance( EMAIL_PROPNAME ) ; 220 if( prop != null ) { 221 recipient_addresses = prop.getValues() ; 222 } 223 if( sender_address == null ) { 224 prop = pro.getPropertyInstance( SENDER_PROPNAME ) ; 225 if( prop != null ) { 226 sender_address = (String ) prop.getValue() ; 227 } 228 } 229 } 230 } 231 232 String subject_text = buildSubjectLine((AbstractObject) cmdObj); 233 234 if ((subject_text == null) || (subject_text.length() == 0)) { 235 subject_text = ((AbstractObject)cmdObj).getName(); 236 } 237 238 Template template = (Template) HarmoniseObjectFactory.instantiateHarmoniseObject(getDataStoreInteface(),Template.class.getName(),nTemplateId); 239 240 HarmoniseOutput doc = new HarmoniseOutput(this.getDataStoreInteface()) ; 241 242 243 org.w3c.dom.Element workflow_obj_root = template.publishObjectToElement((Publishable) cmdObj,doc,getState()); 244 245 246 doc.appendChild( doc.copyNode( workflow_obj_root ) ) ; 247 248 String obj_as_txt = processXSLT(doc.getDocumentElement(), xsl_filename); 249 250 Iterator recipient_addresses_it = recipient_addresses.iterator(); 251 252 while (recipient_addresses_it.hasNext()) { 253 String recipient_address = (String ) recipient_addresses_it.next(); 254 Email email = new Email(ConfigSettings.getProperty(PNAME_EMAIL_HOST),recipient_address, sender_address, 255 subject_text, obj_as_txt); 256 String ctype = this.getParameter(CONTENTTYPE_PNAME); 257 258 if (ctype != null) { 259 260 email.SetContentType(ctype); 261 } 262 263 email.send(); 264 } 265 266 String xml_recepient = null; 267 268 269 } catch (DataAccessException e) { 270 throw new CommandExecutionException("DataAccessException",e); 271 } catch (HarmoniseFactoryException e) { 272 throw new CommandExecutionException(e); 273 } catch (PublishException e) { 274 throw new CommandExecutionException("PublishException",e); 275 } catch (ConfigException e) { 276 throw new CommandExecutionException("ConfigException",e); 277 } catch (DataStoreException e) { 278 throw new CommandExecutionException("DataStoreException",e); 279 } catch (InvalidPropertyInstanceException e) { 280 throw new CommandExecutionException(e.getLocalizedMessage(),e); 281 } 282 283 return null; 284 } 285 286 287 290 public String getName() { 291 return "Email"; 292 } 293 294 297 public boolean isValidCommandObject(Object obj) { 298 return (obj instanceof Publishable); 299 } 300 301 310 private String processXSLT(Node xml_node, String xsl_filename) throws CommandException { 311 StringWriter out_str = new StringWriter (); 312 313 try { 314 XSLResource xsl = (XSLResource) HarmoniseObjectFactory.instantiateHarmoniseObject(getDataStoreInteface(),XSLResource.class.getName(),xsl_filename); 315 316 Transformer trans = xsl.getTemplates().newTransformer(); 317 318 DOMSource ds = new DOMSource (xml_node); 319 320 StreamResult res = new StreamResult (out_str); 321 322 trans.transform(ds, res); 323 } catch (HarmoniseFactoryException e) { 324 throw new CommandException("Error getting xsl from factory - " + xsl_filename,e); 325 } catch (TransformerConfigurationException e) { 326 throw new CommandException("TransformerConfigurationException",e); 327 } catch (DataAccessException e) { 328 throw new CommandException("DataAccessException",e); 329 } catch (TransformerException e) { 330 throw new CommandException("TransformerException",e); 331 } 332 333 return out_str.toString(); 334 } 335 336 343 private String buildSubjectLine(AbstractObject obj) throws DataAccessException { 344 String subject_line = null; 345 subject_line = this.getParameter(SUBJECT_PNAME); 346 347 if (subject_line == null) { 348 return ""; 349 } else { 350 return (processSubjectLine(obj,subject_line)); 351 } 352 } 353 354 362 private String processSubjectLine(AbstractObject obj, String subject_line) throws DataAccessException { 363 StringBuffer strbuf = new StringBuffer (); 364 365 StringTokenizer tokens = new StringTokenizer(subject_line, " \t\n\r", 366 true); 367 368 while (tokens.hasMoreTokens()) { 369 String subject_bit = tokens.nextToken(); 370 371 if (subject_bit.startsWith(SUBJECT_IPMARKER)) { 372 strbuf.append(getProperty((AbstractProfiledObject) obj,subject_bit)); 373 } else if (subject_bit.startsWith(SUBJECT_INMARKER)) { 374 strbuf.append(obj.getName()); 375 } else { 376 strbuf.append(subject_bit); 377 } 378 } 379 380 return strbuf.toString(); 381 } 382 383 391 private String getProperty(AbstractProfiledObject profObj,String prop_desc) throws DataAccessException { 392 String property_name = prop_desc.substring(SUBJECT_IPMARKER.length()) 393 .trim(); 394 395 List property_descs = null; 396 boolean got_pvalue = true; 397 398 399 Profile object_profile = profObj.getProfile(); 400 401 402 try { 403 property_descs = object_profile.getPropertyInstance(prop_desc).getValues(); 404 } catch (InvalidPropertyInstanceException e) { 405 throw new DataAccessException(e.getLocalizedMessage(),e); 406 } 407 408 got_pvalue = (property_descs.size() > 0); 409 410 411 if (got_pvalue) { 412 return (separatedList(property_descs, ",")); 413 } else { 414 return "*NoValue*"; 415 } 416 } 417 418 427 private String separatedList(Collection collection, String seperator) { 428 StringBuffer strbuf = new StringBuffer (); 429 430 Iterator coll_it = collection.iterator(); 431 432 while (coll_it.hasNext()) { 433 strbuf.append((String ) coll_it.next()); 434 435 if (coll_it.hasNext()) { 436 strbuf.append(seperator); 437 } 438 } 439 440 return strbuf.toString(); 441 } 442 443 } | Popular Tags |