View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.example.bookstore.transformers;
8   
9   import org.mule.RequestContext;
10  import org.mule.api.transformer.TransformerException;
11  import org.mule.example.bookstore.Book;
12  import org.mule.example.bookstore.Order;
13  import org.mule.transformer.AbstractTransformer;
14  import org.mule.transformer.types.DataTypeFactory;
15  import org.mule.transport.email.MailProperties;
16  
17  /**
18   * Composes an e-mail notification message to be sent based on the Book Order.
19   */
20  public class OrderToEmailTransformer extends AbstractTransformer
21  {
22      public OrderToEmailTransformer()
23      {
24          super();
25          registerSourceType(DataTypeFactory.create(Order.class));
26          setReturnDataType(DataTypeFactory.STRING);
27      }
28      
29      @Override
30      protected Object doTransform(Object src, String outputEncoding) throws TransformerException
31      {
32          Order order = (Order) src;
33          Book book = order.getBook();
34          
35          String body =  "Thank you for placing your order for " +
36                         book.getTitle() + " with the Mule-powered On-line Bookstore. " +
37                         "Your order will be shipped  to " +
38                         order.getAddress() + " by the next business day.";
39          
40          String email = order.getEmail();
41          RequestContext.getEventContext().getMessage().setOutboundProperty(MailProperties.TO_ADDRESSES_PROPERTY,
42                                                                            email);
43          logger.info("Sending e-mail notification to " + email);
44          return body;
45      }
46  }