org.springframework.mail.MailPreparationException: Could not prepare mail; nested exception is java.lang.ClassCastException: java.lang.String cannot be cast to org.alfresco.service.cmr.repository.NodeRef
A quick look at the source code revealed why:
... // See if an email template has been specified String text = null; NodeRef templateRef = (NodeRef)ruleAction.getParameterValue(PARAM_TEMPLATE); if (templateRef != null) ...
It doesn't even attempt to differentiate between a template in the repository vs one in the classpath. It automatically assumes it is in the repo (hence the cast to a NodeRef before anything else is done). To get around this, I had to manually process the template to generate the text to be used in the email.
To do this, a model needs to be generated for the template to use. I used the code that generates the model in the MailActionExecutor.java minus a few objects I deemed unnecessary for my use case. Then I passed in the path to the ftl and the model to the template service and it generated the output string. I then set it to the PARAM_TEXT parameter for the email.
Here is the code:
private static final String EXPIRE_EMAIL_TEMPLATE = "alfresco/extension/burris/common/templates/expiration-email-template.ftl"; ... // Process the template - the mail action executor expects it to be in the repository which is why this has to be done Mapmodel = new HashMap ...( 8, 1.0f ); NodeRef person = personService.getPerson( authenticationService.getCurrentUserName() ); model.put( "person", new TemplateNode( person, serviceRegistry, null ) ); model.put( "document", new TemplateNode( nodeRef, serviceRegistry, null ) ); NodeRef parent = nodeService.getPrimaryParent( nodeRef ).getParentRef(); model.put( "space", new TemplateNode( parent, serviceRegistry, null ) ); model.put( "message", new I18NMessageMethod() ); String text = templateService.processTemplate( "freemarker", EXPIRE_EMAIL_TEMPLATE, model ); // Send email notice detailing the state change Action emailAction = actionService.createAction( MailActionExecuter.NAME ); emailAction.setParameterValue( MailActionExecuter.PARAM_SUBJECT, subject.toString() ); emailAction.setParameterValue( MailActionExecuter.PARAM_TO_MANY, emailList ); emailAction.setParameterValue( MailActionExecuter.PARAM_TEXT, text ); if ( log.isDebugEnabled() ) log.debug( "Sending email..." ); actionService.executeAction( emailAction, nodeRef );
No comments:
Post a Comment