All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kflow.contrib.mule-flightsearch-sample.1.1.1.source-code.mule-flightsearch-sample.xml Maven / Gradle / Ivy

The newest version!
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http"
      xmlns="http://www.mulesoft.org/schema/mule/core"
      xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
      xmlns:smtps="http://www.mulesoft.org/schema/mule/smtps"
      xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting"
      xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
      xmlns:spring="http://www.springframework.org/schema/beans" xmlns:core="http://www.mulesoft.org/schema/mule/core"
      xmlns:script="http://www.mulesoft.org/schema/mule/scripting" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      version="CE-3.2.1"
      xmlns:smtp="http://www.mulesoft.org/schema/mule/smtp"
      xmlns:email="http://www.mulesoft.org/schema/mule/email"
      xsi:schemaLocation="http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
	http://www.mulesoft.org/schema/mule/smtp http://www.mulesoft.org/schema/mule/smtp/3.1/mule-smtp.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd
http://www.mulesoft.org/schema/mule/smtps http://www.mulesoft.org/schema/mule/smtps/current/mule-smtps.xsd
http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/email http://www.mulesoft.org/schema/mule/email/3.1/mule-email.xsd
       http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd ">
    <flow name="flights" >
        <vm:inbound-endpoint address="vm://flightSearch" exchange-pattern="request-response"/>
        <scripting:component doc:name="Groovy">
            <scripting:script engine="Groovy">
                <scripting:text >
                        //search engine emulation and validation
                        try {
                        def dDate = payload.getSimpleAttributeValue("departureDate");
                        def rDate = payload.getSimpleAttributeValue("returnDate");
                        def from = payload.getSimpleAttributeValue("from");
                        def to = payload.getSimpleAttributeValue("to");
                        def invalidDates = '&lt;font color=&quot;red&quot;&gt;Invalid dates!&lt;/font&gt;';
                        def invalidDestination = '&lt;font color=&quot;red&quot;&gt;Invalid destination!&lt;/font&gt;';
                        if(from.equals(to)){
                            payload.setSimpleAttribute("valid", "false");
                            payload.setSimpleAttribute("validationError", invalidDestination);
                            return "FAIL";
                        }
                        if(rDate != null){
                            def df = new java.text.SimpleDateFormat("dd-MM-yyyy");
                            if(!df.parse(rDate).after(df.parse(dDate))){
                                payload.setSimpleAttribute("valid", "false");
                                payload.setSimpleAttribute("validationError", invalidDates);
                                return "FAIL";
                            }
                        }
                        payload.setSimpleAttribute("valid", "true");
                        def dict = payload.findAttributeByKey("flights");
                        if(dict != null){
                            payload.removeAttribute(dict);
                        }
                        def p = Integer.valueOf(payload.getSimpleAttributeValue("passengers"));
                        def c = Integer.valueOf(payload.getSimpleAttributeValue("clazz"));
                        def hours1 = ["10:00", "14:30", "20:10"];
                        def hours2 = ["12:15", "17:35", "23:55"];
                        def format = " &#160;  &#160; Price:  %s EUR &#160; &#160; &#160; &lt;b&gt;%s - %s&lt;/b&gt; %s %s &#160; &#160; &#160; &lt;b&gt;%s - %s&lt;/b&gt; %s %s"  ;
                        def formatOneWay = "Price:  %s EUR &#160; &#160; &#160; &lt;b&gt;%s - %s&lt;/b&gt; %s %s ";
                        def prices = [WAW:100, JFK:1000, BCN:200, CDG:150];
                        for (i in 0..hours1.size()-1){
                            def h1 = hours1[i];
                            def h2 = hours2[i];
                            def price = prices[from];
                            if(rDate != null)
                                price += prices[to];
                            price *= p * c;
                            price += 50*i;
                            def flight = "" ;
                            if(rDate == null)
                                flight = String.format(formatOneWay, price, from, to, dDate, h1);
                            else
                                flight = String.format(format, price, from, to, dDate, h1, to, from, rDate, h2 );
                            payload.addDictionaryAttributeItem("flights", flight, flight);
                        }
                        return payload;
                    } catch (Exception e) {
                    e.printStackTrace();
                    throw e;
                    }
                </scripting:text>
            </scripting:script>
        </scripting:component>
    </flow>
    <smtp:gmail-connector name="gmailConn" contentType="text/html"
                          fromAddress="[email protected]" replyToAddresses="[email protected]"
                          subject="AW TEST">
    </smtp:gmail-connector>

    <script:transformer name="orderAttachementTransformer">
        <script:script engine="groovy">
            <script:text>
                //tranformer attaching a file to an email
                import java.io.InputStream;
                import javax.activation.DataHandler;
                import javax.mail.util.ByteArrayDataSource;
                import org.mule.api.MuleMessage;
                import org.mule.api.transformer.TransformerException;
                import org.mule.api.transport.PropertyScope;
                InputStream s = (InputStream) message.getProperty("stream", PropertyScope.OUTBOUND);
                try {
                ByteArrayDataSource document = new ByteArrayDataSource(s, "application/pdf");
                document.setName("card.pdf");
                DataHandler attachement = new DataHandler(document);
                message.addOutboundAttachment("UTF-8", attachement);
                } catch (Exception e) {
                System.out.println("Failed")
                e.printStackTrace();
                }
                System.out.println("Document Attached")
                return payload;
            </script:text>
        </script:script>
    </script:transformer>

    <flow name="confirmation" doc:name="confirmation">
        <vm:inbound-endpoint address="vm://confirmation" exchange-pattern="request-response"/>
        <enricher target="#[header:confirmationAddress]">
            <expression-transformer>
                <return-argument evaluator="groovy"
                                 expression="payload.getSimpleAttributeValue('email')"/>
            </expression-transformer>
        </enricher>
        <script:component>
            <script:script engine="groovy">
                <script:text>
                        //generating reservation number and sending and email
                        def text = "&lt;h3&gt;Dear %s %s %s, &lt;/h3&gt;"
                        text += "Your have just booked flight:"
                        text += "&lt;h2&gt;%s&lt;/h2&gt;"
                        text += "Your reservation number:"
                        text += "&lt;h2&gt;%s&lt;/h2&gt;"
                        text += "Thank you for flying &lt;b&gt;Aperte Air&lt;/b&gt;!"
                        def getReservationNumber(){
                            String validChars ="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
                            int length = 6
                            String resNr = ""
                            java.util.Random rnd = new java.util.Random()
                            for (i in 0..length){
                                int rndPos = Math.abs(rnd.nextInt() % validChars.length());
                                resNr += validChars.charAt(rndPos)
                            }
                            return resNr
                        }
                        def firstName = payload.getSimpleAttributeValue("firstName")
                        def lastName = payload.getSimpleAttributeValue("lastName")
                        def salutation = payload.getSimpleAttributeValue("salutation")
                        def flight = payload.getSimpleAttributeValue("flight")
                        def reservationNr =  getReservationNumber()
                        payload.setSimpleAttribute("reservationNr", reservationNr)
                        def msg = String.format(text, salutation, firstName, lastName, flight, reservationNr)
                        payload.setSimpleAttribute("msg", msg)
                        System.out.println("uncomment smtp tag in mule-flightsearch-sample.xml to send emails");
                        return msg;
                </script:text>
            </script:script>
        </script:component>
        <!--To enable emails, one has to povide correct user and password-->

        <!--<smtp:outbound-endpoint connector-ref="gmailConn"   encoding="UTF-8"-->
                                <!--host="smtp.gmail.com" port="587" user="[USER]" password="[***PROTECTED***]"-->
                                <!--to="#[header:confirmationAddress]">-->
            <!--<transformer ref="orderAttachementTransformer"/>-->
            <!--<email:object-to-mime-transformer-->
                    <!--mimeType="application/pdf" useOutboundAttachments="true"/>-->
            <!--<email:string-to-email-transformer/>-->
        <!--</smtp:outbound-endpoint>-->

    </flow>
</mule>




© 2015 - 2024 Weber Informatics LLC | Privacy Policy