001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.camel.bam; 018 019 import org.apache.camel.Exchange; 020 import org.apache.camel.Processor; 021 import org.apache.camel.Expression; 022 import org.apache.camel.bam.ActivityRules; 023 import org.apache.camel.util.IntrospectionSupport; 024 import org.springframework.orm.jpa.JpaTemplate; 025 import org.springframework.transaction.support.TransactionTemplate; 026 027 import java.util.List; 028 029 /** 030 * @version $Revision: $ 031 */ 032 public class JpaBamProcessorSupport<T> extends BamProcessorSupport<T> { 033 private ActivityRules activityRules; 034 private JpaTemplate template; 035 private String findByKeyQuery; 036 private String keyPropertyName = "correlationKey"; 037 038 public JpaBamProcessorSupport(TransactionTemplate transactionTemplate, JpaTemplate template, Expression<Exchange> correlationKeyExpression, ActivityRules activityRules, Class<T> entitytype) { 039 super(transactionTemplate, correlationKeyExpression, entitytype); 040 this.activityRules = activityRules; 041 this.template = template; 042 } 043 044 public JpaBamProcessorSupport(TransactionTemplate transactionTemplate, JpaTemplate template, Expression<Exchange> correlationKeyExpression, ActivityRules activityRules) { 045 super(transactionTemplate, correlationKeyExpression); 046 this.activityRules = activityRules; 047 this.template = template; 048 } 049 050 public String getFindByKeyQuery() { 051 if (findByKeyQuery == null) { 052 findByKeyQuery = createFindByKeyQuery(); 053 } 054 return findByKeyQuery; 055 } 056 057 058 public void setFindByKeyQuery(String findByKeyQuery) { 059 this.findByKeyQuery = findByKeyQuery; 060 } 061 062 public ActivityRules getActivity() { 063 return activityRules; 064 } 065 066 public void setActivity(ActivityRules activityRules) { 067 this.activityRules = activityRules; 068 } 069 070 public String getKeyPropertyName() { 071 return keyPropertyName; 072 } 073 074 public void setKeyPropertyName(String keyPropertyName) { 075 this.keyPropertyName = keyPropertyName; 076 } 077 078 public JpaTemplate getTemplate() { 079 return template; 080 } 081 082 public void setTemplate(JpaTemplate template) { 083 this.template = template; 084 } 085 086 // Implementatiom methods 087 //----------------------------------------------------------------------- 088 protected T loadEntity(Exchange exchange, Object key) { 089 List<T> list = template.find(getFindByKeyQuery(), key); 090 T entity = null; 091 if (!list.isEmpty()) { 092 entity = list.get(0); 093 } 094 if (entity == null) { 095 entity = createEntity(exchange, key); 096 setKeyProperty(entity, key); 097 template.persist(entity); 098 } 099 return entity; 100 } 101 102 /** 103 * Sets the key property on the new entity 104 */ 105 protected void setKeyProperty(T entity, Object key) { 106 IntrospectionSupport.setProperty(entity, getKeyPropertyName(), key); 107 } 108 109 /** 110 * Create a new instance of the entity for the given key 111 */ 112 protected T createEntity(Exchange exchange, Object key) { 113 return (T) exchange.getContext().getInjector().newInstance(getEntityType()); 114 } 115 116 protected void processEntity(Exchange exchange, T entity) throws Exception { 117 if (entity instanceof Processor) { 118 Processor processor = (Processor) entity; 119 processor.process(exchange); 120 } 121 else { 122 // TODO add other extension points - eg. passing in Activity 123 throw new IllegalArgumentException("No processor defined for this route"); 124 } 125 } 126 127 protected String createFindByKeyQuery() { 128 return "select x from " + getEntityType().getName() + " x where x." + getKeyPropertyName() + " = ?1"; 129 } 130 }