001    /**
002     * Copyright 2005-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.krms.framework.engine.result;
017    
018    import java.util.Collections;
019    import java.util.EventObject;
020    import java.util.Map;
021    
022    import org.joda.time.DateTime;
023    import org.joda.time.format.DateTimeFormat;
024    import org.joda.time.format.DateTimeFormatter;
025    import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
026    import org.kuali.rice.krms.api.engine.ResultEvent;
027    
028    /**
029     * An implementation of {@link ResultEvent} with start DateTime, end DateTime, and elapsed time in Milliseconds.
030     * The Date is formatted using org.joda.time.format.DateTimeFormat pattern of "yyyy-MM-dd HH.mm.ss.SSS"
031     *
032     * @author Kuali Rice Team (rice.collab@kuali.org)
033     */
034    public class TimingResult extends EventObject implements ResultEvent {
035        
036            private static final long serialVersionUID = 5335636381355236617L;
037    
038        private static final DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd HH.mm.ss.SSS");
039            
040        private String type;
041            private DateTime start;
042            private DateTime end;
043            private ExecutionEnvironment environment;
044            private String description;
045            private Map<String, ?> resultDetails;
046    
047        /**
048         * Constructor
049         * @param description of the TimingResult
050         * @param type of the TimingResult
051         * @param source of the TimingResult
052         * @param environment {@link ExecutionEnvironment} of the TimingResult
053         * @param start org.joda.time.DateTime of the TimingResult
054         * @param end org.joda.time.DateTime of the TimingResult
055         */
056            public TimingResult(String description, String type, Object source, ExecutionEnvironment environment, DateTime start, DateTime end){
057                    super(source);
058                    this.type = type;
059                    this.environment = environment;
060                    this.start = start;
061                    this.end = end;
062                    this.description = description;
063            }
064    
065        /**
066         * Constructor
067         * @param type of the TimingResult
068         * @param source of the TimingResult
069         * @param environment {@link ExecutionEnvironment} of the TimingResult
070         * @param start org.joda.time.DateTime of the TimingResult
071         * @param end org.joda.time.DateTime of the TimingResult
072         */
073            public TimingResult(String type, Object source, ExecutionEnvironment environment, DateTime start, DateTime end){
074                    super(source);
075                    this.type = type;
076                    this.environment = environment;
077                    this.start = start;
078                    this.end = end;
079            }
080    
081        /**
082         * Returns the Elapsed Time in Milliseconds
083         * @return Long end milliseconds minus start milliseconds
084         */
085            public Long getElapsedTimeInMilliseconds(){
086                    return Long.valueOf(end.getMillis() - start.getMillis());
087            }
088    
089        @Override
090            public ExecutionEnvironment getEnvironment(){
091                    return environment;
092            };
093    
094        @Override
095            public String toString(){
096                    StringBuffer sb = new StringBuffer();
097                    sb.append(fmt.print(end));
098                    sb.append(" EventType: "+ type);
099                    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    }