Take a survey

Developing Components > Converting Custom QPACs to Components > Creating a component based on a custom QPAC > Creating Java application logic for the component

Creating Java application logic for the component
After setting up your development environment, the next step in converting a QPAC to a LiveCycle ES component is to create a service implementation. This Java interface defines the service’s operations (public methods), input values, and return values. The public methods that are defined become operations that are exposed by the service. Unlike a custom QPAC, you do not need to implement any Java libraries specific to Adobe.
The following example shows the interface that belongs to the email component. Notice that the name of the interface is EmailService.
Example: Defining the email service interface
package com.adobe.livecycle.sample.email;
import com.adobe.idp.Document;
 
 
//The interface for the email component
public interface EmailService {
	
  //Sends email to the specified email accounts
   void send(String aToAddress, 
			 String aCCAddress, 
			 String aBCCAddress, 
			 String aSubject, 
			 String aMimeType, 
			 String aMessage, 
			 String aAttachmentName, 
			 Document docAttacment) throws Exception;
 
}
Next, you must create the service implementation class that implements the interface that you defined. The service implementation class is where similar application logic found in a custom QPAC’s execute method is defined. That is, this class contains Java application logic that performs the component’s operations.
A service implementation must conform to the following restrictions:
The application logic that is executed by LiveCycle ES when the operation is invoked must be specified in the corresponding methods. For example, the send method that is defined in the EmailService interface must contain Java application logic that sends email messages.
The following Java code shows the Java implementation class named EmailServiceImpl that implements EmailService.
Example: Defining the email service implementation class
package com.adobe.livecycle.sample.email;
 
import java.util.Properties;
 
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import com.adobe.idp.Document;
 
 
public class EmailServiceImpl implements EmailService {
 
	
	//Specify data members for the EmailServiceImpl
	//These members correspond to configuration values
	protected String m_smtpHost;
	protected String m_smtpUser;
	protected String m_smtpPassword;
 
	
	//Set the host Mail server
	public void setSmtpHost(String aHost) {
		   if (aHost != null) {
			   aHost = aHost.trim();
		   }
		   m_smtpHost = aHost;
	   }
 
 	//Get the Host name
    public String getSmtpHost() {
		   return m_smtpHost;
	   }
 
	  
	//Set the smtp user name
    public void setSmtpUser(String aUser) {
	   if (aUser != null) {
		   aUser = aUser.trim();
		   }
		   m_smtpUser = aUser;
	   }
 
	
	//Get the smtp user name
    public String getSmtpUser() {
	   return m_smtpUser;
	 }
 
    
	//Set the smtp password
     public void setSmtpPassword(String aPassword) {
		  if (aPassword != null) {
			   aPassword = aPassword.trim();
		   }
		   m_smtpPassword = aPassword;
	   }
 
	
	//Get the smtp password
    public String getSmtpPassword() {
		   return m_smtpPassword;
	   }
 
	
	//Send the email.
    public void send(String aToAddress, 
					String aCCAddress, 
					String aBCCAddress, 
					String aSubject, 
					String aMimeType, 
					String aMessage, 
					String aAttachmentName, 
					Document docAttachment) throws MessagingException, Exception {
	   {
    	try {
    	
    		Properties _props = new Properties();
			if (getSmtpHost() != null) {
			   _props.put("mail.smtp.host", getSmtpHost());
			  }
			
			if (getSmtpUser() != null) {
			   _props.put("mail.smtp.user", getSmtpUser());
			  }
			
			// Setup mail server if authentication used
			if (!(isEmpty(m_smtpUser) || !(isEmpty(m_smtpPassword)))) 
			  {
				_props.put("mail.smtp.auth", "true");
			  }
 
			//Get the default Session and add the new properties to it.
			 Session _session = Session.getInstance(_props, null);
                        
			//Create a message
			MimeMessage _msg = new MimeMessage(_session);
 
			//Set the sent from address
			InternetAddress fromIntenetAddress = new InternetAddress(getSmtpUser());
			_msg.setFrom(fromIntenetAddress);
 
			InternetAddress[] toInternetAddresses =   InternetAddress.parse(aToAddress);
			_msg.setRecipients(Message.RecipientType.TO, toInternetAddresses);
			   
			if (!isEmpty(aCCAddress)) {
				InternetAddress[] toCCInternetAddresses = InternetAddress.parse(aCCAddress);
				_msg.setRecipients(Message.RecipientType.CC,toCCInternetAddresses);
			   }
 
			if (!isEmpty(aBCCAddress)) {
			  InternetAddress[] toBCCInternetAddresses = InternetAddress.parse(aBCCAddress);
			  _msg.setRecipients(Message.RecipientType.BCC,toBCCInternetAddresses);
			   }
 
			if (!isEmpty(aSubject)) {
			   _msg.setSubject(aSubject);
			 }
 
			if (!isEmpty(aMessage)) {
			  if (!isEmpty(aMimeType)) {
				   _msg.setContent(aMessage, aMimeType);
		     } else {
				   _msg.setText(aMessage);
			  }
			 }
 
			Multipart multipart = new MimeMultipart();
			BodyPart messageBodyPart = new MimeBodyPart();
                        
		   if (docAttachment != null){
			  BodyPart _bodyPartAttachment = new MimeBodyPart();
			  FileDataSource fileSource =  new FileDataSource(docAttachment.getFile());
			  _bodyPartAttachment.setDataHandler( new DataHandler(fileSource));
			
			  if (!isEmpty(aAttachmentName)){
				  _bodyPartAttachment.setFileName(aAttachmentName.trim());
			  } else {
				  _bodyPartAttachment.setFileName("attachment.pdf");   
			  }
 
		  multipart.addBodyPart(_bodyPartAttachment);
		 _msg.setContent(multipart);
		   }
		   
		  //Send the email message
		 _msg.saveChanges(); // implicit with send()
		 messageBodyPart.setContent(aMessage, "text/html; charset=utf-8");
		 multipart.addBodyPart(messageBodyPart);
		
		 //Set the sent date for the email
		 _msg.setSentDate(new java.util.Date());
		
		 //Send the message
		 Transport _transport =_session.getTransport("smtp");
		 _transport.connect(getSmtpHost(), getSmtpUser(), getSmtpPassword());
		 _transport.sendMessage(_msg, _msg.getAllRecipients());
	
    	 } catch (MessagingException ex) {
             throw ex;
         } catch (NullPointerException npe) {
             throw new Exception("Incorrect SMTP server provided");
         }
      }
    }
	   
   //check If passed in string contains only non whitespace
   public static boolean isEmpty(String aValue) {
	   return (aValue == null || aValue.trim().length() == 0);
   }
}
The application logic that is located in the send method is similar to the application logic that is located in the execute method. That is, they both use the Java Mail API to send email messages.
However, there are also some differences. For example, the application logic located in the QPAC’s execute method uses APIs specific to Adobe while the application logic located in the EmailServiceImpl class does not. Notice how input values, such as the aToAddress, are passed to a LiveCycle ES component. Input values are specified as operation parameters:
public void send(String aToAddress, 
					String aCCAddress, 
					String aBCCAddress, 
					String aSubject, 
					String aMimeType, 
					String aMessage, 
					String aAttachmentName, 
Document docAttachment)
In contrast, a QPAC uses an API call specific to Adobe to get input values:
String inEmailAddress = getActionDataAsString(
executionContext,
root,
PARAM_EMAIL_ADDRESS
);
A component contains other classes that enable you to perform tasks, such as control the component’s behavior when it is started and stopped. You can create a LiveCycle ES implementation class that implements the com.adobe.idp.dsc.component.LifeCycle interface and exposes the following methods:
setComponentContext: Sets the context of the component.
onStart: Is invoked when the service is started
onStop: Is invoked when the component is stopped.
For information, see Defining the service’s LifeCycle implementation.

 

Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/livecycle/es/sdkHelp/programmer/sdkHelp/componentUpgrading.160.9.html