Coverage Report - org.apache.camel.bam.TemporalRule
 
Classes in this File Line Coverage Branch Coverage Complexity
TemporalRule
0% 
0% 
1.909
 
 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.bam.model.ActivityState;
 22  
 import org.apache.camel.bam.model.ProcessInstance;
 23  
 import org.apache.camel.builder.FromBuilder;
 24  
 import org.apache.camel.builder.ProcessorFactory;
 25  
 import org.apache.camel.impl.DefaultExchange;
 26  
 import org.apache.camel.util.Time;
 27  
 import org.apache.commons.logging.Log;
 28  
 import org.apache.commons.logging.LogFactory;
 29  
 
 30  
 import java.util.Date;
 31  0
 
 32  0
 /**
 33  0
  * A temporal rule
 34  0
  *
 35  
  * @version $Revision: $
 36  
  */
 37  
 public class TemporalRule {
 38  0
     private static final transient Log log = LogFactory.getLog(TemporalRule.class);
 39  
     private TimeExpression first;
 40  
     private TimeExpression second;
 41  
     private long expectedMillis;
 42  
     private long overdueMillis;
 43  
     private Processor overdueAction;
 44  
     private ProcessorFactory overdueProcessorFactory;
 45  
 
 46  0
     public TemporalRule(TimeExpression left, TimeExpression right) {
 47  0
         this.first = left;
 48  0
         this.second = right;
 49  0
     }
 50  
 
 51  
     public TemporalRule expectWithin(Time builder) {
 52  0
         return expectWithin(builder.toMillis());
 53  
     }
 54  
 
 55  
     public TemporalRule expectWithin(long millis) {
 56  0
         expectedMillis = millis;
 57  0
         return this;
 58  
     }
 59  
 
 60  
     public FromBuilder errorIfOver(Time builder) {
 61  0
         return errorIfOver(builder.toMillis());
 62  0
     }
 63  
 
 64  
     public FromBuilder errorIfOver(long millis) {
 65  0
         overdueMillis = millis;
 66  
 
 67  0
         FromBuilder builder = new FromBuilder(second.getBuilder().getProcessBuilder(), null);
 68  0
         overdueProcessorFactory = builder;
 69  0
         return builder;
 70  
     }
 71  
 
 72  
     public TimeExpression getFirst() {
 73  0
         return first;
 74  
     }
 75  
 
 76  
     public TimeExpression getSecond() {
 77  0
         return second;
 78  
     }
 79  
 
 80  
     public void evaluate(ProcessContext context, ActivityState activityState) {
 81  0
         ProcessInstance instance = context.getProcessInstance();
 82  
 
 83  0
         Date firstTime = first.evaluateState(instance);
 84  0
         if (firstTime == null) {
 85  
             // ignore as first event has not accurred yet
 86  0
             return;
 87  
         }
 88  
 
 89  
         // TODO now we might need to set the second activity state
 90  
         // to 'grey' to indicate it now could happen?
 91  
         // if the second activity state is not created yet we might wanna create it
 92  
 
 93  0
         ActivityState secondState = second.getActivityState(instance);
 94  0
         if (expectedMillis > 0L) {
 95  0
             Date expected = secondState.getTimeExpected();
 96  0
             if (expected == null) {
 97  0
                 expected = add(firstTime, expectedMillis);
 98  0
                 secondState.setTimeExpected(expected);
 99  
             }
 100  
         }
 101  0
         if (overdueMillis > 0L) {
 102  0
             Date overdue = secondState.getTimeOverdue();
 103  0
             if (overdue == null) {
 104  0
                 overdue = add(firstTime, overdueMillis);
 105  0
                 secondState.setTimeOverdue(overdue);
 106  
             }
 107  
         }
 108  
 
 109  0
         Date secondTime = second.evaluateState(instance);
 110  0
         if (secondTime == null) {
 111  
             // TODO add test that things have expired
 112  
         }
 113  
         else {
 114  
 
 115  
 /*
 116  
             if (secondTime.delta(firstTime.plus(gap)) > 0) {
 117  
                 // TODO
 118  
             }
 119  
 */
 120  
         }
 121  0
     }
 122  
 
 123  
     public void processExpired(ActivityState activityState) throws Exception {
 124  0
         if (overdueAction == null && overdueProcessorFactory != null) {
 125  0
             overdueAction = overdueProcessorFactory.createProcessor();
 126  
         }
 127  0
         if (overdueAction != null) {
 128  0
             Date now = new Date();
 129  0
             ProcessInstance instance = activityState.getProcess();
 130  0
             ActivityState secondState = second.getActivityState(instance);
 131  0
             Date overdue = secondState.getTimeOverdue();
 132  0
             if (now.compareTo(overdue) >= 0) {
 133  0
                 Exchange exchange = createExchange();
 134  0
                 exchange.getIn().setBody(activityState);
 135  0
                 overdueAction.process(exchange);
 136  0
             }
 137  
             else {
 138  0
                 log.warn("Process has not actually expired; the time is: " + now + " but the overdue time is: " + overdue);
 139  
             }
 140  
         }
 141  0
     }
 142  
 
 143  
     protected Exchange createExchange() {
 144  0
         return new DefaultExchange(second.getBuilder().getProcessBuilder().getContext());
 145  
     }
 146  
 
 147  
     /**
 148  
      * Returns the date in the future adding the given number of millis
 149  
      *
 150  
      * @param date
 151  
      * @param millis
 152  
      * @return the date in the future
 153  
      */
 154  
     protected Date add(Date date, long millis) {
 155  0
         return new Date(date.getTime() + millis);
 156  
     }
 157  
 
 158  
     /*
 159  
     public void onActivityLifecycle(ActivityState state, ActivityRules activityRules, ActivityLifecycle lifecycle) {
 160  
         if (first.isActivityLifecycle(activityRules, lifecycle)) {
 161  
             // lets create the expected and error timers
 162  
 
 163  
             // TODO we could use a single timer event; then keep incrementing its type
 164  
             // counter to escalate & use different times each time to reduce some DB work
 165  
             createTimer(state, expectedMillis);
 166  
             createTimer(state, overdueMillis);
 167  
         }
 168  
     }
 169  
     */
 170  
 }