001    package org.kuali.rice.krms.framework.engine.result;
002    
003    import java.util.Collections;
004    import java.util.EventObject;
005    import java.util.Map;
006    
007    import org.apache.commons.lang.StringUtils;
008    import org.joda.time.DateTime;
009    import org.joda.time.format.DateTimeFormat;
010    import org.joda.time.format.DateTimeFormatter;
011    import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
012    import org.kuali.rice.krms.api.engine.ResultEvent;
013    
014    public class BasicResult extends EventObject implements ResultEvent {
015            private static final long serialVersionUID = -4124200802034785921L;
016    
017        private static final DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd HH.mm.ss.SSS");
018    
019            protected String type;
020            protected DateTime timestamp;
021            protected ExecutionEnvironment environment;
022            protected Boolean result = null;
023            protected String description;
024            protected Map<String, ?> resultDetails;
025    
026        public BasicResult(Map<String, ?> resultDetails, String eventType, Object source, ExecutionEnvironment environment, boolean result) {
027            this(resultDetails, null, eventType, source, environment, result);
028        }
029            
030        public BasicResult(Map<String, ?> resultDetails, String description, String eventType, Object source, ExecutionEnvironment environment, boolean result) {
031            this(eventType, source, environment);
032            this.resultDetails = resultDetails;
033            this.result = new Boolean(result);
034            this.description = (description == null) ? StringUtils.EMPTY : description;
035        }
036    
037        public BasicResult(String description, String eventType, Object source, ExecutionEnvironment environment, boolean result) {
038                    this(eventType, source, environment);
039                    this.result = new Boolean(result);
040                    this.description = description;
041            }
042    
043            public BasicResult(String eventType, Object source, ExecutionEnvironment environment, boolean result) {
044                    this(eventType, source, environment);
045                    this.result = new Boolean(result);
046            }
047    
048            public BasicResult(String eventType, Object source, ExecutionEnvironment environment) {
049                    super(source);
050                    this.type = eventType;
051                    this.timestamp = new DateTime(); 
052                    this.environment = environment;
053            }
054    
055            @Override
056            public String getType() {
057                    return type;
058            }
059    
060            @Override
061            public DateTime getTimestamp() {
062                    return timestamp;
063            }
064            
065            @Override
066            public ExecutionEnvironment getEnvironment(){
067                    return environment;
068            }
069            
070            @Override
071            public Boolean getResult(){
072                    return result;
073            }
074    
075            @Override
076            public String getDescription() {
077                return description;
078            }
079            
080            @Override
081            public Map<String, ?> getResultDetails() {
082                if (resultDetails == null) {
083                    return Collections.emptyMap();
084                } else {
085                    return Collections.unmodifiableMap(resultDetails);
086                }
087            }
088    
089            public String toString(){
090                    StringBuffer sb = new StringBuffer();
091            sb.append(fmt.print(this.getTimestamp()));
092                    sb.append(" EventType: "+ getType());
093                    sb.append(" ( "+ this.getSource().toString());
094                    if (this.getResult() != null){
095                            sb.append(" evaluated to: "+ this.getResult().toString());
096                    }
097                    sb.append(" )");
098                    return sb.toString();
099            }
100    }