001/**
002 * Copyright 2005-2016 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 */
016package org.kuali.rice.krms.framework.engine.result;
017
018import java.util.Collections;
019import java.util.EventObject;
020import java.util.Map;
021
022import org.apache.commons.lang.StringUtils;
023import org.joda.time.DateTime;
024import org.joda.time.format.DateTimeFormat;
025import org.joda.time.format.DateTimeFormatter;
026import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
027import org.kuali.rice.krms.api.engine.ResultEvent;
028
029/**
030 * An implementation of {@link ResultEvent}
031 * @author Kuali Rice Team (rice.collab@kuali.org)
032 */
033public class BasicResult extends EventObject implements ResultEvent {
034        private static final long serialVersionUID = -4124200802034785921L;
035
036    private static final DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd HH.mm.ss.SSS");
037
038        protected String type;
039        protected DateTime timestamp;
040        protected ExecutionEnvironment environment;
041        protected Boolean result = null;
042        protected String description;
043        protected Map<String, ?> resultDetails;
044
045    // TODO Consider static factory methods in stead of constructors - Item 1 Effective Java 2nd Edition
046    // TODO Consider a builder when faced with many constructor parameters - Item 2 Effective Java 2nd Edition
047    /**
048     * Constructor 
049     * @param resultDetails of the ResultEvent
050     * @param eventType String of the ResultEvent
051     * @param source Object of the ResultEvent
052     * @param environment {@link ExecutionEnvironment}
053     * @param result boolean of the ResultEvent
054     */
055    public BasicResult(Map<String, ?> resultDetails, String eventType, Object source, ExecutionEnvironment environment, boolean result) {
056        this(resultDetails, null, eventType, source, environment, result);
057    }
058
059    /**
060     * Constructor
061     * @param resultDetails of the ResultEvent
062     * @param description String of the ResultEvent
063     * @param eventType String of the ResultEvent
064     * @param source Object of the ResultEvent
065     * @param environment {@link ExecutionEnvironment}
066     * @param result boolean of the ResultEvent
067     */
068    public BasicResult(Map<String, ?> resultDetails, String description, String eventType, Object source, ExecutionEnvironment environment, boolean result) {
069        this(eventType, source, environment);
070        this.resultDetails = resultDetails;
071        this.result = new Boolean(result);
072        this.description = (description == null) ? StringUtils.EMPTY : description;
073    }
074
075    /**
076     * Constructor
077     * @param description String of the ResultEvent
078     * @param eventType String of the ResultEvent
079     * @param source Object of the ResultEvent
080     * @param environment {@link ExecutionEnvironment}
081     * @param result boolean of the ResultEvent
082     */
083    public BasicResult(String description, String eventType, Object source, ExecutionEnvironment environment, boolean result) {
084                this(eventType, source, environment);
085                this.result = new Boolean(result);
086                this.description = description;
087        }
088
089    /**
090     * Constructor
091     * @param eventType String of the ResultEvent
092     * @param source Object of the ResultEvent
093     * @param environment {@link ExecutionEnvironment}
094     * @param result boolean of the ResultEvent
095     */
096        public BasicResult(String eventType, Object source, ExecutionEnvironment environment, boolean result) {
097                this(eventType, source, environment);
098                this.result = new Boolean(result);
099        }
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}