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