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.bam.model.ActivityState;
020    import org.apache.camel.bam.model.ProcessInstance;
021    import org.apache.camel.bam.rules.ActivityRules;
022    import org.apache.camel.bam.rules.TemporalRule;
023    import org.apache.camel.util.ObjectHelper;
024    
025    import java.util.Date;
026    
027    /**
028     * @version $Revision: $
029     */
030    public abstract class TimeExpression {
031        private ActivityRules activityRules;
032        private ActivityBuilder builder;
033        private ActivityLifecycle lifecycle;
034    
035        public TimeExpression(ActivityBuilder builder, ActivityLifecycle lifecycle) {
036            this.lifecycle = lifecycle;
037            this.builder = builder;
038            this.activityRules = builder.getActivityRules();
039        }
040    
041        public boolean isActivityLifecycle(ActivityRules activityRules, ActivityLifecycle lifecycle) {
042            return ObjectHelper.equals(activityRules, this.activityRules) && ObjectHelper.equals(lifecycle, this.lifecycle);
043        }
044    
045        /**
046         * Creates a new temporal rule on this expression and the other expression
047         */
048        public TemporalRule after(TimeExpression expression) {
049            TemporalRule rule = new TemporalRule(expression, this);
050            rule.getSecond().getActivityRules().addRule(rule);
051            return rule;
052        }
053    
054        public Date evaluate(ProcessInstance processInstance) {
055            ActivityState state = processInstance.getActivityState(activityRules);
056            if (state != null) {
057                return evaluate(processInstance, state);
058            }
059            return null;
060        }
061    
062        public abstract Date evaluate(ProcessInstance instance, ActivityState state);
063    
064        // Properties
065        //-------------------------------------------------------------------------
066    
067        public ActivityBuilder getBuilder() {
068            return builder;
069        }
070    
071        public ActivityRules getActivityRules() {
072            return activityRules;
073        }
074    
075        public ActivityLifecycle getLifecycle() {
076            return lifecycle;
077        }
078    
079        public ActivityState getActivityState(ProcessInstance instance) {
080            return instance.getActivityState(activityRules);
081        }
082    
083        public ActivityState getOrCreateActivityState(ProcessInstance instance) {
084            return instance.getOrCreateActivityState(activityRules);
085        }
086    }