View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krms.framework.engine.result;
17  
18  import java.util.Collections;
19  import java.util.EventObject;
20  import java.util.Map;
21  
22  import org.joda.time.DateTime;
23  import org.joda.time.format.DateTimeFormat;
24  import org.joda.time.format.DateTimeFormatter;
25  import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
26  import org.kuali.rice.krms.api.engine.ResultEvent;
27  
28  public class TimingResult extends EventObject implements ResultEvent {
29      
30  	private static final long serialVersionUID = 5335636381355236617L;
31  
32      private static final DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd HH.mm.ss.SSS");
33  	
34      private String type;
35  	private DateTime start;
36  	private DateTime end;
37  	private ExecutionEnvironment environment;
38  	private String description;
39  	private Map<String, ?> resultDetails;
40  
41  	public TimingResult(String description, String type, Object source, ExecutionEnvironment environment, DateTime start, DateTime end){
42  		super(source);
43  		this.type = type;
44  		this.environment = environment;
45  		this.start = start;
46  		this.end = end;
47  		this.description = description;
48  	}
49  	
50  	public TimingResult(String type, Object source, ExecutionEnvironment environment, DateTime start, DateTime end){
51  		super(source);
52  		this.type = type;
53  		this.environment = environment;
54  		this.start = start;
55  		this.end = end;
56  	}
57  	
58  	public Long getElapsedTimeInMilliseconds(){
59  		return Long.valueOf(end.getMillis() - start.getMillis());
60  	}
61  	
62  	public ExecutionEnvironment getEnvironment(){
63  		return environment;
64  	};
65  	
66  	public String toString(){
67  		StringBuffer sb = new StringBuffer();
68  		sb.append(fmt.print(end));
69  		sb.append(" EventType: "+ type);
70  		sb.append(" (Start = " + fmt.print(start));
71  		sb.append(", End = " + fmt.print(end));
72  		sb.append(",  Elapsed Time = "+ getElapsedTimeInMilliseconds().toString());
73  		sb.append(" milliseconds.)");
74  		return sb.toString();
75  	}
76  
77  	@Override
78  	public Boolean getResult() {
79  		return null;
80  	}
81  
82  	@Override
83  	public DateTime getTimestamp() {
84  		return end;
85  	}
86  
87  	@Override
88  	public String getType() {
89  		return type;
90  	}
91  	
92  	@Override
93  	public Map<String, ?> getResultDetails() {
94  	    if (resultDetails == null) {
95  	        return Collections.emptyMap();
96  	    } else {
97  	        return Collections.unmodifiableMap(resultDetails);
98  	    }
99  	}
100 	
101 	@Override
102 	public String getDescription() {
103 		return description;
104 	}
105 }