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.processor;
018    
019    import org.apache.camel.Exchange;
020    import org.apache.camel.Expression;
021    import org.apache.camel.Processor;
022    import org.apache.camel.bam.model.ActivityState;
023    import org.apache.camel.bam.model.ProcessInstance;
024    import org.apache.camel.bam.rules.ActivityRules;
025    import org.apache.commons.logging.Log;
026    import org.apache.commons.logging.LogFactory;
027    import org.springframework.orm.jpa.JpaTemplate;
028    import org.springframework.transaction.support.TransactionTemplate;
029    
030    /**
031     * A concrete {@link Processor} for working on <a
032     * href="http://activemq.apache.org/camel/bam.html">BAM</a> which uses JPA as
033     * the persistence and uses the {@link ProcessInstance} entity to store the
034     * process information.
035     * 
036     * @version $Revision: $
037     */
038    public class JpaBamProcessor extends JpaBamProcessorSupport<ProcessInstance> {
039        private static final transient Log LOG = LogFactory.getLog(JpaBamProcessor.class);
040    
041        public JpaBamProcessor(TransactionTemplate transactionTemplate, JpaTemplate template, Expression<Exchange> correlationKeyExpression, ActivityRules activityRules) {
042            super(transactionTemplate, template, correlationKeyExpression, activityRules);
043        }
044    
045        public JpaBamProcessor(TransactionTemplate transactionTemplate, JpaTemplate template, Expression<Exchange> correlationKeyExpression, 
046                               ActivityRules activityRules, Class<ProcessInstance> entitytype) {
047            super(transactionTemplate, template, correlationKeyExpression, activityRules, entitytype);
048        }
049    
050        protected void processEntity(Exchange exchange, ProcessInstance process) throws Exception {
051            if (LOG.isDebugEnabled()) {
052                LOG.debug("Processing process instance: " + process);
053            }
054    
055            // lets force the lazy creation of this activity
056            ActivityRules rules = getActivityRules();
057            ActivityState state = process.getOrCreateActivityState(rules);
058    
059            state.processExchange(rules, new ProcessContext(exchange, rules, state));
060    
061            rules.getProcessRules().processExchange(exchange, process);
062        }
063    }