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

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

<?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 >
                        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>
    <flow name="confirmation" doc:name="confirmation">
        <vm:inbound-endpoint address="vm://confirmation" exchange-pattern="request-response"/>
        <script:component>
            <script:script engine="groovy">
                <script:text>
                    //fake class to get class loader
                    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)
                    return payload
                </script:text>
            </script:script>
        </script:component>
        <smtp:outbound-endpoint connector-ref="gmailConn" host="smtp.gmail.com" port="587" user="aperteworkflow" password="awtest123"  doc:name="SMTP">
            <custom-transformer class="pl.net.bluesoft.rnd.awf.mule.transformer.ProcessInstanceToEmailTransformer"/>
            <email:string-to-email-transformer/>
        </smtp:outbound-endpoint>
    </flow>
    <flow name="validation" >
        <vm:inbound-endpoint address="vm://flightValidation"/>
        <scripting:component doc:name="Groovy">
            <scripting:script engine="Groovy">
                <scripting:text >
                    try {
                     return payload;
                    } catch (Exception e) {
                        e.printStackTrace();
                        throw e;
                    }
                </scripting:text>
            </scripting:script>
        </scripting:component>
    </flow>
</mule>




© 2015 - 2025 Weber Informatics LLC | Privacy Policy