Coverage Report - org.apache.camel.bam.ProcessBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
ProcessBuilder
84% 
100% 
0
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.camel.bam;
 18  
 
 19  
 import org.apache.camel.Endpoint;
 20  
 import org.apache.camel.Processor;
 21  
 import org.apache.camel.Route;
 22  
 import org.apache.camel.bam.model.ActivityDefinition;
 23  
 import org.apache.camel.bam.model.ProcessDefinition;
 24  
 import org.apache.camel.bam.model.ProcessInstance;
 25  
 import org.apache.camel.bam.processor.ActivityMonitorEngine;
 26  
 import org.apache.camel.bam.processor.JpaBamProcessor;
 27  
 import org.apache.camel.bam.rules.ProcessRules;
 28  
 import org.apache.camel.builder.RouteBuilder;
 29  
 import static org.apache.camel.util.ObjectHelper.notNull;
 30  
 import org.springframework.orm.jpa.JpaTemplate;
 31  
 import org.springframework.transaction.TransactionStatus;
 32  
 import org.springframework.transaction.support.TransactionCallbackWithoutResult;
 33  
 import org.springframework.transaction.support.TransactionTemplate;
 34  
 
 35  
 import java.util.ArrayList;
 36  
 import java.util.List;
 37  
 
 38  
 /**
 39  
  * A builder of a process definition
 40  
  *
 41  
  * @version $Revision: $
 42  
  */
 43  3
 public abstract class ProcessBuilder extends RouteBuilder {
 44  
     private static int processCounter;
 45  
     private JpaTemplate jpaTemplate;
 46  
     private final TransactionTemplate transactionTemplate;
 47  
     private final String processName;
 48  1
     private List<ActivityBuilder> activityBuilders = new ArrayList<ActivityBuilder>();
 49  1
     private Class entityType = ProcessInstance.class;
 50  1
     private ProcessRules processRules = new ProcessRules();
 51  
     private ProcessDefinition processDefinition;
 52  
 
 53  
     protected ProcessBuilder(JpaTemplate jpaTemplate, TransactionTemplate transactionTemplate) {
 54  1
         this(jpaTemplate, transactionTemplate, createProcessName());
 55  1
     }
 56  
 
 57  
     protected static synchronized String createProcessName() {
 58  1
         return "Process-" + (++processCounter);
 59  
     }
 60  
 
 61  1
     protected ProcessBuilder(JpaTemplate jpaTemplate, TransactionTemplate transactionTemplate, String processName) {
 62  1
         this.jpaTemplate = jpaTemplate;
 63  1
         this.transactionTemplate = transactionTemplate;
 64  1
         this.processName = processName;
 65  1
     }
 66  
 
 67  
     public ActivityBuilder activity(String endpointUri) {
 68  3
         return activity(endpoint(endpointUri));
 69  
     }
 70  
 
 71  
     public ActivityBuilder activity(Endpoint endpoint) {
 72  3
         ActivityBuilder answer = new ActivityBuilder(this, endpoint);
 73  3
         activityBuilders.add(answer);
 74  3
         return answer;
 75  
     }
 76  
 
 77  
     /**
 78  
      * Sets the process entity type used to perform state management
 79  
      */
 80  
     public ProcessBuilder entityType(Class entityType) {
 81  0
         this.entityType = entityType;
 82  0
         return this;
 83  
     }
 84  
 
 85  
     public Processor createActivityProcessor(ActivityBuilder activityBuilder) {
 86  3
         notNull(jpaTemplate, "jpaTemplate");
 87  3
         transactionTemplate.execute(new TransactionCallbackWithoutResult() {
 88  3
             protected void doInTransactionWithoutResult(TransactionStatus status) {
 89  3
                 processRules.setProcessDefinition(getProcessDefinition());
 90  3
             }
 91  
         });
 92  3
         return new JpaBamProcessor(getTransactionTemplate(), getJpaTemplate(), activityBuilder.getCorrelationExpression(), activityBuilder.getActivityRules(), getEntityType());
 93  
     }
 94  
 
 95  
     // Properties
 96  
     //-----------------------------------------------------------------------
 97  
     public List<ActivityBuilder> getActivityBuilders() {
 98  0
         return activityBuilders;
 99  
     }
 100  
 
 101  
     public Class getEntityType() {
 102  3
         return entityType;
 103  
     }
 104  
 
 105  
     public JpaTemplate getJpaTemplate() {
 106  4
         return jpaTemplate;
 107  
     }
 108  
 
 109  
     public void setJpaTemplate(JpaTemplate jpaTemplate) {
 110  0
         this.jpaTemplate = jpaTemplate;
 111  0
     }
 112  
 
 113  
     public TransactionTemplate getTransactionTemplate() {
 114  4
         return transactionTemplate;
 115  
     }
 116  
 
 117  
     public ProcessRules getProcessRules() {
 118  4
         return processRules;
 119  
     }
 120  
 
 121  
     public String getProcessName() {
 122  0
         return processName;
 123  
     }
 124  
 
 125  
     public ProcessDefinition getProcessDefinition() {
 126  5
         if (processDefinition == null) {
 127  1
             processDefinition = findOrCreateProcessDefinition();
 128  
         }
 129  5
         return processDefinition;
 130  
     }
 131  
 
 132  
     public void setProcessDefinition(ProcessDefinition processDefinition) {
 133  0
         this.processDefinition = processDefinition;
 134  0
     }
 135  
 
 136  
     // Implementation methods
 137  
     //-------------------------------------------------------------------------
 138  
     protected void populateRoutes(List<Route> routes) throws Exception {
 139  1
         boolean first = true;
 140  1
         for (ActivityBuilder builder : activityBuilders) {
 141  3
             Route route = builder.createRoute();
 142  3
             if (first) {
 143  1
                 route.getServices().add(new ActivityMonitorEngine(getJpaTemplate(), getTransactionTemplate(), getProcessRules()));
 144  1
                 first = false;
 145  
             }
 146  3
             routes.add(route);
 147  3
         }
 148  1
     }
 149  
 
 150  
     // Implementation methods
 151  
     //-------------------------------------------------------------------------
 152  
     public ActivityDefinition findOrCreateActivityDefinition(String activityName) {
 153  2
         ProcessDefinition definition = getProcessDefinition();
 154  2
         List<ActivityDefinition> list = jpaTemplate.find("select x from " + ActivityDefinition.class.getName() + " x where x.processDefinition = ?1 and x.name = ?2", definition, activityName);
 155  2
         if (!list.isEmpty()) {
 156  0
             return list.get(0);
 157  
         }
 158  
         else {
 159  2
             ActivityDefinition answer = new ActivityDefinition();
 160  2
             answer.setName(activityName);
 161  2
             answer.setProcessDefinition(ProcessDefinition.getRefreshedProcessDefinition(jpaTemplate, definition));
 162  2
             jpaTemplate.persist(answer);
 163  2
             return answer;
 164  
         }
 165  
     }
 166  
 
 167  
     protected ProcessDefinition findOrCreateProcessDefinition() {
 168  1
         List<ProcessDefinition> list = jpaTemplate.find("select x from " + ProcessDefinition.class.getName() + " x where x.name = ?1", processName);
 169  1
         if (!list.isEmpty()) {
 170  0
             return list.get(0);
 171  
         }
 172  
         else {
 173  1
             ProcessDefinition answer = new ProcessDefinition();
 174  1
             answer.setName(processName);
 175  1
             jpaTemplate.persist(answer);
 176  1
             return answer;
 177  
         }
 178  
     }
 179  
 }