package org.recap;

import org.apache.activemq.camel.component.ActiveMQComponent;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.recap.repository.XmlRecordRepository;
import org.recap.route.JMSReportRouteBuilder;
import org.recap.route.XmlRouteBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * Created by angelind on 21/7/16.
 */
@Component
public class ReCAPCamelContext {

    Logger logger = LoggerFactory.getLogger(ReCAPCamelContext.class);

    CamelContext context;
    XmlRecordRepository xmlRecordRepository;

    private String xmlTagName;
    private String inputDirectoryPath;
    private Integer poolSize;
    private Integer maxPoolSize;


    @Autowired
    public ReCAPCamelContext(CamelContext context, XmlRecordRepository xmlRecordRepository,
                             @Value("${etl.split.xml.tag.name}") String xmlTagName,
                             @Value("${etl.load.directory}") String inputDirectoryPath,
                             @Value("${etl.pool.size}") Integer poolSize, @Value("${etl.max.pool.size}") Integer maxPoolSize) {
        this.context = context;
        this.xmlRecordRepository = xmlRecordRepository;
        this.xmlTagName = xmlTagName;
        this.inputDirectoryPath = inputDirectoryPath;
        this.poolSize = poolSize;
        this.maxPoolSize = maxPoolSize;
        init();
    }

    private void init() {
        try {
            addDynamicRoute();
        } catch (Exception e) {
            logger.error("Exception : " + e.getMessage());
        }
    }

    public void addRoutes(RouteBuilder routeBuilder) throws Exception {
        context.addRoutes(routeBuilder);
    }

    public void addDynamicRoute() throws Exception {
        context.addComponent("activemq", ActiveMQComponent.activeMQComponent("vm://localhost?broker.persistent=false"));
        addRoutes(getXmlRouteBuilder());
        addRoutes(new JMSReportRouteBuilder());
    }

    public RouteBuilder getXmlRouteBuilder() {
        XmlRouteBuilder xmlRouteBuilder = new XmlRouteBuilder();
        xmlRouteBuilder.setXmlTagName(xmlTagName);
        xmlRouteBuilder.setInputDirectoryPath(inputDirectoryPath);
        xmlRouteBuilder.setPoolSize(poolSize);
        xmlRouteBuilder.setMaxPoolSize(maxPoolSize);
        xmlRouteBuilder.setXmlRecordRepository(xmlRecordRepository);
        return xmlRouteBuilder;
    }
}

@@ -888,16 +888,119 @@
 lSize;%0A%0A
+    private JMSReportRouteBuilder jmsReportRouteBuilder;%0A    private XmlRouteBuilder xmlRouteBuilder;%0A%0A
 %0A    @Au
@@ -1731,28 +1731,58 @@
      add
-Dynamic
+Components();%0A            addDefault
 Route
+s
 ();%0A    
@@ -2029,19 +2029,20 @@
 addD
-ynamic
+efault
 Route
+s
 () t
@@ -2051,32 +2051,380 @@
 ows Exception %7B%0A
+        addRoutes(getXmlRouteBuilder());%0A        addRoutes(getJMSReportBuilder());%0A    %7D%0A%0A    private JMSReportRouteBuilder getJMSReportBuilder() %7B%0A        if (null == jmsReportRouteBuilder) %7B%0A            jmsReportRouteBuilder = new JMSReportRouteBuilder();%0A        %7D%0A        return jmsReportRouteBuilder;%0A    %7D%0A%0A    private void addComponents() %7B%0A
         context.
@@ -2536,76 +2536,41 @@
     
+%7D%0A%0A
     
-addRoutes(getXmlRouteBuilder());%0A        addRoutes(new JMSReport
+public RouteBuilder getXml
 Rout
@@ -2583,46 +2583,32 @@
 er()
-);
+ %7B
 %0A    
-%7D%0A%0A
     
-public RouteBuilder getX
+if (null == x
 mlRo
@@ -2609,33 +2609,32 @@
  xmlRouteBuilder
-(
 ) %7B%0A        XmlR
@@ -2629,31 +2629,19 @@
         
+  
-XmlRouteBuilder
+ 
  xmlRout
@@ -2666,32 +2666,36 @@
 RouteBuilder();%0A
+    
         xmlRoute
@@ -2729,32 +2729,36 @@
 gName);%0A        
+   
+ 
 xmlRouteBuilder.
@@ -2800,32 +2800,36 @@
 yPath);%0A        
+    
 xmlRouteBuilder.
@@ -2851,32 +2851,36 @@
 lSize);%0A        
+   
+ 
 xmlRouteBuilder.
@@ -2900,32 +2900,36 @@
 e(maxPoolSize);%0A
+    
         xmlRoute
@@ -2981,16 +2981,26 @@
 itory);%0A
+        %7D%0A
         

refactored ReCAPCamelContext