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
032     * <a href="http://activemq.apache.org/camel/bam.html">BAM</a> which uses JPA as the persistence and uses the
033     * {@link ProcessInstance} entity to store the process information.
034     *
035     * @version $Revision: $
036     */
037    public class JpaBamProcessor extends JpaBamProcessorSupport<ProcessInstance> {
038        private static final transient Log log = LogFactory.getLog(JpaBamProcessor.class);
039    
040        public JpaBamProcessor(TransactionTemplate transactionTemplate, JpaTemplate template, Expression<Exchange> correlationKeyExpression, ActivityRules activityRules) {
041            super(transactionTemplate, template, correlationKeyExpression, activityRules);
042        }
043    
044        public JpaBamProcessor(TransactionTemplate transactionTemplate, JpaTemplate template, Expression<Exchange> correlationKeyExpression, ActivityRules activityRules, Class<ProcessInstance> entitytype) {
045            super(transactionTemplate, template, correlationKeyExpression, activityRules, entitytype);
046        }
047    
048        protected void processEntity(Exchange exchange, ProcessInstance process) throws Exception {
049            if (log.isDebugEnabled()) {
050                log.debug("Processing process instance: " + process);
051            }
052    
053            // lets force the lazy creation of this activity
054            ActivityRules rules = getActivityRules();
055            ActivityState state = process.getOrCreateActivityState(rules);
056    
057            state.processExchange(rules, new ProcessContext(exchange, rules, state));
058    
059            rules.getProcessRules().processExchange(exchange, process);
060        }
061    }