001/**
002 * Copyright 2005-2015 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.api.engine;
017
018import java.util.Collections;
019import java.util.HashMap;
020import java.util.HashSet;
021import java.util.Map;
022import java.util.Map.Entry;
023import java.util.Set;
024
025import org.kuali.rice.core.api.exception.RiceRuntimeException;
026import org.springframework.util.CollectionUtils;
027
028/**
029 * An Exception for {@link TermResolver} exceptions.
030 *
031 * @author Kuali Rice Team (rice.collab@kuali.org)
032 */
033public class TermResolutionException extends RiceRuntimeException {
034
035        private static final long serialVersionUID = 1L;
036
037        public final String termResolverClassName;
038        public final String outputTerm;
039        public final Set<String> prereqs;
040        public final Set<String> parameterNames;
041        public final Map<String, String> parameters;
042
043    /**
044     * Builds the resolution info string from the given values.
045     * @param tr {@link TermResolver} whose values to append to the result String if not null
046     * @param parameters Map<String, String> whose keys and values will be appended to the result String
047     * @return String representing the given values
048     */
049        private static String buildResolutionInfoString(TermResolver<?> tr, Map<String, String> parameters) {
050                StringBuilder result = new StringBuilder();
051                
052                result.append("[");
053                result.append(TermResolver.class.getSimpleName() + "=");
054
055                if (tr == null) { 
056                        result.append("null");
057                } else {
058                        result.append(tr.toString());
059                }
060                
061                result.append(", parameters={");
062
063                boolean firstEntry = true;
064                if (!CollectionUtils.isEmpty(parameters)) {
065                        
066                        for (Entry<String,String> parameter : parameters.entrySet()){
067
068                                if (firstEntry) {
069                                        firstEntry = false;
070                                } else { 
071                                        result.append(",");
072                                }
073
074                                result.append(parameter.getKey());
075                                result.append("=");
076                                result.append(parameter.getValue());
077                        }
078                }
079                
080                result.append("}]");
081                return result.toString();
082        }
083
084    /**
085     * Create a TermResolutionException with the given values
086     * @param message the exception message
087     * @param tr {@link TermResolver} to use to set values to if not null
088     * @param parameters to set the parameters value to if not null
089     * @param cause the root Throwable cause.
090     */
091        public TermResolutionException(String message, TermResolver<?> tr, Map<String, String> parameters, Throwable cause) {
092                super(message + " " + buildResolutionInfoString(tr, parameters), cause);
093                if (tr == null) {
094                        termResolverClassName = "";
095                        outputTerm = null;
096                        prereqs = null;
097                        parameterNames = null;
098                } else {
099                        termResolverClassName = tr.getClass().getName();
100                        outputTerm = tr.getOutput();
101                        prereqs = tr.getPrerequisites();
102                        parameterNames = Collections.unmodifiableSet(new HashSet<String>(tr.getParameterNames()));
103                }
104                if (parameters != null){
105                        this.parameters = Collections.unmodifiableMap(new HashMap<String, String>(parameters));
106                } else {
107                        this.parameters = null;
108                }
109        }
110
111    /**
112     * Create a TermResolutionException with the given values
113     * @param message the exception message
114     * @param tr {@link TermResolver} to use to set values to if not null
115     * @param parameters to set the parameters value to if not null
116     */
117        public TermResolutionException(String message, TermResolver<?> tr, Map<String, String> parameters) {
118                super(message + " " + buildResolutionInfoString(tr, parameters));
119                if (tr == null) {
120                        termResolverClassName = "";
121                        outputTerm = null;
122                        prereqs = null;
123                        parameterNames = null;
124                } else {
125                        termResolverClassName = tr.getClass().getName();
126                        outputTerm = tr.getOutput();
127                        prereqs = tr.getPrerequisites();
128                        parameterNames = Collections.unmodifiableSet(new HashSet<String>(tr.getParameterNames()));
129                }
130                if (parameters != null){
131                        this.parameters = Collections.unmodifiableMap(new HashMap<String, String>(parameters));
132                } else {
133                        this.parameters = null;
134                }
135        }
136
137}