<file_sep>package nl.han.dare2date.matchservice;

import facebook4j.User;
import nl.han.dare2date.matchservice.model.MatchResponse;
import nl.han.dare2date.matchservice.model.MatchResult;
import org.apache.camel.Exchange;
import org.apache.camel.processor.aggregate.AggregationStrategy;
import twitter4j.Status;

import java.math.BigInteger;
import java.util.ArrayList;

public class SocialMediaMatchAggregrate implements AggregationStrategy {
    private int match = 0;

    /**
     * This method is called every time a to(...) returns a response. This response (whether it's from
     * twitter or facebook) is sent in the body of the newExchange.
     *
     * @param oldExchange
     * @param newExchange
     * @return
     */
    public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
        if (oldExchange == null) {
            return newExchange;
        }

        /**
         * Build a Response using the results of twitter & facebook. This is no useful
         * matchalgorithm but you can do better don't you?
         */
        increateMatchValueForFacebookUser(oldExchange, newExchange);
        increaseMatchForTwitterStatus(oldExchange, newExchange);

        return oldExchange;
    }

    private void increaseMatchForTwitterStatus(Exchange oldExchange, Exchange newExchange) {
        ArrayList<Status> statuses = newExchange.getIn().getBody(ArrayList.class);
        if (statuses != null) {
            for (Status status : statuses) {
                // A match is bigger when a user has a lots of tweets favoured
                match += status.getFavoriteCount();
            }
            MatchResult matchResult = new MatchResult();
            matchResult.setNumber(BigInteger.valueOf(match));
            MatchResponse matchResponse = new MatchResponse();
            matchResponse.setMatchResult(matchResult);
            oldExchange.getIn().setBody(matchResponse);
        }
    }

    private void increateMatchValueForFacebookUser(Exchange oldExchange, Exchange newExchange) {
        User user = newExchange.getIn().getBody(User.class);
        if (user != null) {
            match += user.getName().length();
            MatchResult matchResult = new MatchResult();
            // A match is bigger when a user has a long name
            matchResult.setNumber(BigInteger.valueOf(match));
            MatchResponse matchResponse = new MatchResponse();
            matchResponse.setMatchResult(matchResult);
            oldExchange.getIn().setBody(matchResponse);
        }
    }
}
<file_sep>Visit the WSDL at http://localhost:8080/ws/calculatorservice/calculator.wsdl
<file_sep>package nl.ase.calculator.gateway;

import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import org.springframework.stereotype.Service;

@Service
public class MoviePrinter implements IMoviePrinter {
    public void printMovieDetails(String searchTerm) {try {
        HttpResponse<JsonNode> jsonResponse = Unirest.post("http://www.moviemeter.nl/api/film/")
                .header("accept", "application/json")
                .queryString("api_key", "<KEY>")
                .queryString("q", searchTerm)
                .asJson();
        System.out.println(jsonResponse.getBody().toString());
    } catch (UnirestException e) {
        e.printStackTrace();
    }
    }
}<file_sep>spring.datasource.url=jdbc:mysql://localhost:3306/dare2date
spring.datasource.username=root
spring.datasource.password=<PASSWORD>
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect<file_sep>How to run this route
---------------------
1. Run Maven: ```mvn clean package```
2. Create a run-configuration for Tomcat 7 or 8 (you can also use Jetty, see the alternative)
3. Deploy the war (Dare2DateCamel.war) to the Tomcat instance
4. Run Tomcat. If only the Tomcat homepage shows up you have to fall back to the alternative 
5. Visit http://localhost:8080/Dare2DateCamel/applyregistration.wsdl or drop a SOAP message in the inbox folder
6. This project does not contain a JAXB-plugin so you have to generate (right click XSD->WebServices->Generate Java Code...) Java classes yourself based on your own XSD.

Alternative
-----------
For steps 2-4 you can also Run Maven with Jetty ```mvn jetty:run```. 

Implementation
--------------
When you run the ApplyRegistrationRoute, messages can be obtained from two sources:
* inbox-folder (located in the bin-directory of your Tomcat/Jetty instance (don't run this example with mvn tomcat:run, instead use a separate Tomcat server. In src/main/webapp/WEB-INF/applyregistrationservice an example messages is included which
 you can copy to your inbox folder in case your webservice is not available.
* a webservice with its wsdl served at http://localhost:8080/Dare2DateCamel/applyregistration.wsdl
 (otherwise: http://localhost:8080/applyregistration.wsdl)
