View Javadoc

1   /**
2    * Copyright 2005-2013 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  /**
29   * An implementation of {@link ResultEvent} with start DateTime, end DateTime, and elapsed time in Milliseconds.
30   * The Date is formatted using org.joda.time.format.DateTimeFormat pattern of "yyyy-MM-dd HH.mm.ss.SSS"
31   *
32   * @author Kuali Rice Team (rice.collab@kuali.org)
33   */
34  public class TimingResult extends EventObject implements ResultEvent {
35      
36  	private static final long serialVersionUID = 5335636381355236617L;
37  
38      private static final DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd HH.mm.ss.SSS");
39  	
40      private String type;
41  	private DateTime start;
42  	private DateTime end;
43  	private ExecutionEnvironment environment;
44  	private String description;
45  	private Map<String, ?> resultDetails;
46  
47      /**
48       * Constructor
49       * @param description of the TimingResult
50       * @param type of the TimingResult
51       * @param source of the TimingResult
52       * @param environment {@link ExecutionEnvironment} of the TimingResult
53       * @param start org.joda.time.DateTime of the TimingResult
54       * @param end org.joda.time.DateTime of the TimingResult
55       */
56  	public TimingResult(String description, String type, Object source, ExecutionEnvironment environment, DateTime start, DateTime end){
57  		super(source);
58  		this.type = type;
59  		this.environment = environment;
60  		this.start = start;
61  		this.end = end;
62  		this.description = description;
63  	}
64  
65      /**
66       * Constructor
67       * @param type of the TimingResult
68       * @param source of the TimingResult
69       * @param environment {@link ExecutionEnvironment} of the TimingResult
70       * @param start org.joda.time.DateTime of the TimingResult
71       * @param end org.joda.time.DateTime of the TimingResult
72       */
73  	public TimingResult(String type, Object source, ExecutionEnvironment environment, DateTime start, DateTime end){
74  		super(source);
75  		this.type = type;
76  		this.environment = environment;
77  		this.start = start;
78  		this.end = end;
79  	}
80  
81      /**
82       * Returns the Elapsed Time in Milliseconds
83       * @return Long end milliseconds minus start milliseconds
84       */
85  	public Long getElapsedTimeInMilliseconds(){
86  		return Long.valueOf(end.getMillis() - start.getMillis());
87  	}
88  
89      @Override
90  	public ExecutionEnvironment getEnvironment(){
91  		return environment;
92  	};
93  
94      @Override
95  	public String toString(){
96  		StringBuffer sb = new StringBuffer();
97  		sb.append(fmt.print(end));
98  		sb.append(" EventType: "+ type);
99  		sb.append(" (Start = " + fmt.print(start));
100 		sb.append(", End = " + fmt.print(end));
101 		sb.append(",  Elapsed Time = "+ getElapsedTimeInMilliseconds().toString());
102 		sb.append(" milliseconds.)");
103 		return sb.toString();
104 	}
105 
106 	@Override
107 	public Boolean getResult() {
108 		return null;
109 	}
110 
111 	@Override
112 	public DateTime getTimestamp() {
113 		return end;
114 	}
115 
116 	@Override
117 	public String getType() {
118 		return type;
119 	}
120 
121     /**
122      * Returns the result details of the ResultEvent as an unmodifiable Map<?,?>
123      * @return result details of the ResultEvent as an unmodifiable Map<?,?>
124      */
125 	@Override
126 	public Map<String, ?> getResultDetails() {
127 	    if (resultDetails == null) {
128 	        return Collections.emptyMap();
129 	    } else {
130 	        return Collections.unmodifiableMap(resultDetails);
131 	    }
132 	}
133 	
134 	@Override
135 	public String getDescription() {
136 		return description;
137 	}
138 }