Coverage Report - org.apache.camel.bam.processor.JpaBamProcessorSupport
 
Classes in this File Line Coverage Branch Coverage Complexity
JpaBamProcessorSupport
57% 
75% 
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.processor;
 18  
 
 19  
 import org.apache.camel.Exchange;
 20  
 import org.apache.camel.Expression;
 21  
 import org.apache.camel.Processor;
 22  
 import org.apache.camel.bam.model.ProcessDefinition;
 23  
 import org.apache.camel.bam.rules.ActivityRules;
 24  
 import org.apache.camel.util.IntrospectionSupport;
 25  
 import org.springframework.orm.jpa.JpaTemplate;
 26  
 import org.springframework.transaction.support.TransactionTemplate;
 27  
 
 28  
 import java.util.List;
 29  
 
 30  
 /**
 31  
  * A base class for JPA based BAM which can use any entity to store the process instance information which
 32  
  * allows derived classes to specialise the process instance entity.
 33  
  *
 34  
  * @version $Revision: $
 35  
  */
 36  
 public class JpaBamProcessorSupport<T> extends BamProcessorSupport<T> {
 37  
     private ActivityRules activityRules;
 38  
     private JpaTemplate template;
 39  
     private String findByKeyQuery;
 40  3
     private String keyPropertyName = "correlationKey";
 41  
 
 42  
     public JpaBamProcessorSupport(TransactionTemplate transactionTemplate, JpaTemplate template, Expression<Exchange> correlationKeyExpression, ActivityRules activityRules, Class<T> entitytype) {
 43  3
         super(transactionTemplate, correlationKeyExpression, entitytype);
 44  3
         this.activityRules = activityRules;
 45  3
         this.template = template;
 46  3
     }
 47  
 
 48  
     public JpaBamProcessorSupport(TransactionTemplate transactionTemplate, JpaTemplate template, Expression<Exchange> correlationKeyExpression, ActivityRules activityRules) {
 49  0
         super(transactionTemplate, correlationKeyExpression);
 50  0
         this.activityRules = activityRules;
 51  0
         this.template = template;
 52  0
     }
 53  
 
 54  
     public String getFindByKeyQuery() {
 55  1
         if (findByKeyQuery == null) {
 56  1
             findByKeyQuery = createFindByKeyQuery();
 57  
         }
 58  1
         return findByKeyQuery;
 59  
     }
 60  
 
 61  
     public void setFindByKeyQuery(String findByKeyQuery) {
 62  0
         this.findByKeyQuery = findByKeyQuery;
 63  0
     }
 64  
 
 65  
     public ActivityRules getActivityRules() {
 66  2
         return activityRules;
 67  
     }
 68  
 
 69  
     public void setActivityRules(ActivityRules activityRules) {
 70  0
         this.activityRules = activityRules;
 71  0
     }
 72  
 
 73  
     public String getKeyPropertyName() {
 74  2
         return keyPropertyName;
 75  
     }
 76  
 
 77  
     public void setKeyPropertyName(String keyPropertyName) {
 78  0
         this.keyPropertyName = keyPropertyName;
 79  0
     }
 80  
 
 81  
     public JpaTemplate getTemplate() {
 82  0
         return template;
 83  
     }
 84  
 
 85  
     public void setTemplate(JpaTemplate template) {
 86  0
         this.template = template;
 87  0
     }
 88  
 
 89  
     // Implementatiom methods
 90  
     //-----------------------------------------------------------------------
 91  
     protected T loadEntity(Exchange exchange, Object key) {
 92  1
         List<T> list = template.find(getFindByKeyQuery(), key);
 93  1
         T entity = null;
 94  1
         if (!list.isEmpty()) {
 95  0
             entity = list.get(0);
 96  
         }
 97  1
         if (entity == null) {
 98  1
             entity = createEntity(exchange, key);
 99  1
             setKeyProperty(entity, key);
 100  1
             ProcessDefinition definition = ProcessDefinition.getRefreshedProcessDefinition(template, getActivityRules().getProcessRules().getProcessDefinition());
 101  1
             setProcessDefinitionProperty(entity, definition);
 102  1
             template.persist(entity);
 103  
         }
 104  1
         return entity;
 105  
     }
 106  
 
 107  
     /**
 108  
      * Sets the key property on the new entity
 109  
      */
 110  
     protected void setKeyProperty(T entity, Object key) {
 111  1
         IntrospectionSupport.setProperty(entity, getKeyPropertyName(), key);
 112  1
     }
 113  
 
 114  
     protected void setProcessDefinitionProperty(T entity, ProcessDefinition processDefinition) {
 115  1
         IntrospectionSupport.setProperty(entity, "processDefinition", processDefinition);
 116  1
     }
 117  
 
 118  
     /**
 119  
      * Create a new instance of the entity for the given key
 120  
      */
 121  
     protected T createEntity(Exchange exchange, Object key) {
 122  1
         return (T) exchange.getContext().getInjector().newInstance(getEntityType());
 123  
     }
 124  
 
 125  
     protected void processEntity(Exchange exchange, T entity) throws Exception {
 126  0
         if (entity instanceof Processor) {
 127  0
             Processor processor = (Processor) entity;
 128  0
             processor.process(exchange);
 129  0
         }
 130  
         else {
 131  
             // TODO add other extension points - eg. passing in Activity
 132  0
             throw new IllegalArgumentException("No processor defined for this route");
 133  
         }
 134  0
     }
 135  
 
 136  
     protected String createFindByKeyQuery() {
 137  1
         return "select x from " + getEntityType().getName() + " x where x." + getKeyPropertyName() + " = ?1";
 138  
     }
 139  
 }