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.

Reference: Sending Event Invitations With Seam from our JCG partner Igor Madjeric at the Igor Madjeric blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

One Response to "Sending Event Invitations With Seam"

  1. Stephan Beal says:

    Be aware that ICS specifies rn line endings, so the above example might not work with strictly conforming clients unless the editor is configured for Windows-style EOLs.

    http://en.wikipedia.org/wiki/ICalendar

    i can recommend ical4j (http://ical4j.sourceforge.net) for generating and parsing ical data. It takes some getting used to but it has full support for ICS (meaning “it supported everything we needed it to do in the last project i used it in”).

Leave a Reply


eight × = 16



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close