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.apache.commons.lang.StringUtils;
23  import org.joda.time.DateTime;
24  import org.joda.time.format.DateTimeFormat;
25  import org.joda.time.format.DateTimeFormatter;
26  import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
27  import org.kuali.rice.krms.api.engine.ResultEvent;
28  
29  /**
30   * An implementation of {@link ResultEvent}
31   * @author Kuali Rice Team (rice.collab@kuali.org)
32   */
33  public class BasicResult extends EventObject implements ResultEvent {
34  	private static final long serialVersionUID = -4124200802034785921L;
35  
36      private static final DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd HH.mm.ss.SSS");
37  
38  	protected String type;
39  	protected DateTime timestamp;
40  	protected ExecutionEnvironment environment;
41  	protected Boolean result = null;
42  	protected String description;
43  	protected Map<String, ?> resultDetails;
44  
45      // TODO Consider static factory methods in stead of constructors - Item 1 Effective Java 2nd Edition
46      // TODO Consider a builder when faced with many constructor parameters - Item 2 Effective Java 2nd Edition
47      /**
48       * Constructor 
49       * @param resultDetails of the ResultEvent
50       * @param eventType String of the ResultEvent
51       * @param source Object of the ResultEvent
52       * @param environment {@link ExecutionEnvironment}
53       * @param result boolean of the ResultEvent
54       */
55      public BasicResult(Map<String, ?> resultDetails, String eventType, Object source, ExecutionEnvironment environment, boolean result) {
56          this(resultDetails, null, eventType, source, environment, result);
57      }
58  
59      /**
60       * Constructor
61       * @param resultDetails of the ResultEvent
62       * @param description String of the ResultEvent
63       * @param eventType String of the ResultEvent
64       * @param source Object of the ResultEvent
65       * @param environment {@link ExecutionEnvironment}
66       * @param result boolean of the ResultEvent
67       */
68      public BasicResult(Map<String, ?> resultDetails, String description, String eventType, Object source, ExecutionEnvironment environment, boolean result) {
69          this(eventType, source, environment);
70          this.resultDetails = resultDetails;
71          this.result = new Boolean(result);
72          this.description = (description == null) ? StringUtils.EMPTY : description;
73      }
74  
75      /**
76       * Constructor
77       * @param description String of the ResultEvent
78       * @param eventType String of the ResultEvent
79       * @param source Object of the ResultEvent
80       * @param environment {@link ExecutionEnvironment}
81       * @param result boolean of the ResultEvent
82       */
83      public BasicResult(String description, String eventType, Object source, ExecutionEnvironment environment, boolean result) {
84  		this(eventType, source, environment);
85  		this.result = new Boolean(result);
86  		this.description = description;
87  	}
88  
89      /**
90       * Constructor
91       * @param eventType String of the ResultEvent
92       * @param source Object of the ResultEvent
93       * @param environment {@link ExecutionEnvironment}
94       * @param result boolean of the ResultEvent
95       */
96  	public BasicResult(String eventType, Object source, ExecutionEnvironment environment, boolean result) {
97  		this(eventType, source, environment);
98  		this.result = new Boolean(result);
99  	}
100 
101     /**
102      * Constructor
103      * @param eventType String of the ResultEvent
104      * @param source Object of the ResultEvent
105      * @param environment {@link ExecutionEnvironment}
106      */
107 	public BasicResult(String eventType, Object source, ExecutionEnvironment environment) {
108 		super(source);
109 		this.type = eventType;
110 		this.timestamp = new DateTime(); 
111 		this.environment = environment;
112 	}
113 
114 	@Override
115 	public String getType() {
116 		return type;
117 	}
118 
119 	@Override
120 	public DateTime getTimestamp() {
121 		return timestamp;
122 	}
123 	
124 	@Override
125 	public ExecutionEnvironment getEnvironment(){
126 		return environment;
127 	}
128 	
129 	@Override
130 	public Boolean getResult(){
131 		return result;
132 	}
133 
134 	@Override
135 	public String getDescription() {
136 	    return description;
137 	}
138 
139     /**
140      * Returns the result details of the ResultEvent as an unmodifiable Map<?,?>
141      * @return result details of the ResultEvent as an unmodifiable Map<?,?>
142      */
143 	@Override
144 	public Map<String, ?> getResultDetails() {
145 	    if (resultDetails == null) {
146 	        return Collections.emptyMap();
147 	    } else {
148 	        return Collections.unmodifiableMap(resultDetails);
149 	    }
150 	}
151 
152     @Override
153 	public String toString(){
154 		StringBuffer sb = new StringBuffer();
155         sb.append(fmt.print(this.getTimestamp()));
156 		sb.append(" EventType: "+ getType());
157 		sb.append(" ( "+ this.getSource().toString());
158 		if (this.getResult() != null){
159 			sb.append(" evaluated to: "+ this.getResult().toString());
160 		}
161 		sb.append(" )");
162 		return sb.toString();
163 	}
164 }