Skip to main content

Sending Event Invitations With Seam

These days one of my colleagues had problems with sending event invitation using mail templates with seam (version 2.x). In basic this should not be a hard task, so I will in short explain what need to be done for sending event invitation using seam mail templates.
When you send mail invitation you need to send an email with attachment which contains information about particular event. I will create simple template and sender class which will be used for sending invitation.

Seam 2.x include additional components which are responsible for sending mails and creating templates. To use this features we need to include seam mail components in application, with maven we could do it like this:


<dependency>
    <groupId>org.jboss.seam</groupId>
    <artifactId>jboss-seam-mail</artifactId>
</dependency>


The seam templating mechanism allow us to create mail template like we do it for standard jsp pages. It is easy and simple to learn, and you also can use standard jsp tags, JSF if you use it. In this example i will not go deeper in the usage of seam mail templating mechanism, Below you can find simple example of template used for sending invitation. 


<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<m:message xmlns="http://www.w3.org/1999/xhtml"
      xmlns:m="http://jboss.com/products/seam/mail"
      xmlns:h="http://java.sun.com/jsf/html">


      <m:header name="Content-Class" value="urn:content-classes:calendarmessage"/>
      <m:from name="Test Mail" address="no-reply-mail@invitation.example" />
      <m:to name="Igor Madjeric">#{eventInvitation.recipient}</m:to>


<m:subject>
     <h:outputText value="Test invitation" />
</m:subject>


<m:body>
      <m:attachment contentType="text/calendar;method=CANCEL" fileName="invitation.ics">
BEGIN:VCALENDAR
METHOD:REQUEST
PRODID:-//Direct Scouts GmbH//INA//DE
VERSION:2.0
CALSCALE:GREGORIAN
BEGIN:VEVENT
DTSTAMP:#{eventInvitation.currentDateAsString}
DTSTART:#{eventInvitation.startAsString}
DTEND:#{eventInvitation.endAsString}
SUMMARY;CHARSET=UTF-8:Test invitation
UID:de827ded-5fc8-4ceb-af1b-b8d9cfbcbca8
ATTENDEE;ROLE=OWNER;PARTSTAT=NEEDS-ACTION;RSVP=FALSE:MAILTO:#{eventInvitation.recipient}
ORGANIZER:MAILTO:xxx@gmail.com
LOCATION;CHARSET=UTF-8:#{eventInvitation.location}
DESCRIPTION;CHARSET=UTF-8:#{eventInvitation.description}
SEQUENCE:0
PRIORITY:5
CLASS:PUBLIC
STATUS:CONFIRMED
TRANSP:OPAQUE
BEGIN:VALARM
ACTION:DISPLAY
DESCRIPTION:REMINDER
TRIGGER;RELATED=START:-PT00H15M00S
END:VALARM
END:VEVENT
END:VCALENDAR
     </m:attachment>
</m:body>
</m:message>

As you can see, it is not complicated, it is like making JSP page. When you creating invitation you need make attention on UID, its a unique identifier for event for which you create invitation, so if you later need to change something about that event you just need to use same UID. For this example I've created EventInvitation model class which contains data needed for event. They do not contains a lot of data but you can extend it if you need more.

package ba.codecentric.mail.sender.model;

import java.text.SimpleDateFormat;
import java.util.Date;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

@Name("eventInvitation")
@Scope(ScopeType.PAGE)
public class EventInvitation {

   SimpleDateFormat iCalendarDateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmm'00'");
   private String recipient;
   private String location;
   private String description;
   /* Start and stop dates */
   private Date start;
   private Date end;
   public String getRecipient() {
      return recipient;
   }
   public void setRecipient(String recipient) {
      this.recipient = recipient;
   }
   public String getLocation() {
      return location;
   }
   public void setLocation(String location) {
      this.location = location;
   }
   public String getDescription() {
      return description;
   }
   public void setDescription(String description) {
      this.description = description;
   }
   public String getStartAsString() {
      return iCalendarDateFormat.format(start);
   }
   public String getEndAsString() {
      return iCalendarDateFormat.format(end);
   }
   public Date getStart() {
      return start;
   }
   public void setStart(Date start) {
      this.start = start;
   }
   public Date getEnd() {
      return end;
   }
   public void setEnd(Date end) {
      this.end = end;
   }
   public String getCurrentDateAsString() {
      return iCalendarDateFormat.format(new Date());
   }
   @Override
   public String toString() {
       return "EventInvitation [recipient=" + recipient + ", location="+ location + ", description=" + description + ", start=" + start + ", end=" + end + "]";
   }
}

It is simple seam component with page scope, leave as longs as page. From template you can see  that we used methods ..AsString for setting dates values. That is because, we cant simple use raw date for representing date in invitation, instead of that we format dates using next format "yyyyMMdd'T'HHmm'00'".

For filling dates I've used next simple form:

<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:s="http://jboss.com/products/seam/taglib"
xmlns:rich="http://richfaces.org/rich"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:f="http://java.sun.com/jsf/core"
template="/includes/template.xhtml">

<!-- main content -->
<ui:define name="MainContent">
<div class="WelcomeContent">
<a4j:form>
<rich:panel header="Welcom To Seam Mail Invitation Sender" style="width: 315px">
Start:<rich:calendar value="#{eventInvitation.start}"
popup="true"
datePattern="dd/M/yy hh:mm a"
showApplyButton="true"
cellWidth="24px"
cellHeight="22px"
style="width:200px"/>
<br />
End:<rich:calendar value="#{eventInvitation.end}"
popup="true"
datePattern="dd/M/yy hh:mm a"
showApplyButton="true"
cellWidth="24px"
cellHeight="22px"
style="width:200px"/>
<br />
Location:<h:inputText value="#{eventInvitation.location}" id="location"/>
<br />
Description:<h:inputText value="#{eventInvitation.description}" id="description"/>
<br />
Recipient:<h:inputText value="#{eventInvitation.recipient}" id="recipient"/>
<a4j:commandButton value="Send Invitation"
action="#{mailInvitationSender.sendInvitation}" reRender="info" />
<h:panelGroup id="info">
<h:outputText value="Status: #{mailInvitationSender.status} " rendered="#{not empty mailInvitationSender.status}" />
</h:panelGroup>
</rich:panel>
</a4j:form>
</div>
</ui:define>
</ui:composition>

Nothing complicated just simple page for filling data. And at end we will take look in sender class.

package ba.codecentric.mail.sender.controller.impl;


import javax.ejb.Remove;
import javax.ejb.Stateful;


import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.faces.Renderer;
import org.jboss.seam.log.Log;


import ba.codecentric.mail.sender.controller.LocalMailInvitationSender;
import ba.codecentric.mail.sender.model.EventInvitation;


@Name("mailInvitationSender")
@Scope(ScopeType.CONVERSATION)
@Stateful
public class StandardMailInvitationSender implements LocalMailInvitationSender {


private static final String STATUS_SUCCESS = "SUCCESS";
private static final String STATUS_FAIL = "FAIL";


private static String INVITATION_TEMPLATE = "/invitation.xhtml";


@Logger
private static Log LOG;


// Component used for rendering template.
@In(create = true)
private Renderer renderer;

@In
private EventInvitation eventInvitation;

private String status;


public String getStatus() {
return status;
}


public void setStatus(String status) {
this.status = status;
}


@Override
public void sendInvitation() {
LOG.info("Send invitation method is called!");
try {
LOG.debug(eventInvitation);
renderer.render(INVITATION_TEMPLATE);
status = STATUS_SUCCESS;
} catch (Exception e) {
LOG.error(e);
status = STATUS_FAIL;
}
LOG.info("Invitation sending:" + status);
}


@Remove
public void done() {
LOG.debug("Bean removed!");
}


}

This is simple class which use renderer for creating mail based on template. So there is nothing special. Of course you need to configure mail session in components.xml but that is simple configuration. You need to add next line in components.xml:

<mail:mail-session session-jndi-name="java:/Mail" />

And that's all. Your application is ready for sending invitations :).
Note: line above in components.xml will create mail session component which will be used by seam for sending mails. For example if you use JBoss 4.xx you may edit configuration in "mail-service.xml" file. 
But how to configure mail session is out of scope of this post, if you need more information about this topic you can check one of my older post Configure Seam Mail.

Comments

Popular posts from this blog

Checking file's "magic numbers"

Few days ago I had very interesting task. Our customer required that we perform checking of so called file's "magic numbers" to determinate does uploaded file correspond to it's extension.  We are already allowed only to upload files with some predefined extensions (PDF, DOC ...). But this can not prevent some evil user to update an exe file after renaming it to PDF or DOC. So first of all I will explain what are "magic numbers", and then I will show how we handle them.

Simple Workflow Engine With Spring

Few months ago, during working on one of the company project, we had need to developed  REST services which is used for sending an email depending on data sent by client application. During developing this service we decide to create simple workflow engine which will be charged for sending an email, but also this engine can be used for any kind of simple flows. In this article i will explain step by step how you can implement your simple workflow engine which can handle sequence flow.

Running Spring Boot Web App on the Random Port from Port Range

By default the spring boot web application is listening on the port 8080 for the incoming connection. This behavior can be changed by providing server.port property value during starting of the application or as part of the application.properties or through the code by implementing EmbeddedServletContainerCustomizer. But it would be even better if we could specified a range of the ports which can be used for the starting the application. It would be great if I could specify a property like server.portRange=8100..8200 to define a list of the port on which I want to start my service. In this blog post I will describe how this can be done.