View Javadoc
1   /**
2    * Copyright 2005-2015 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.api.engine;
17  
18  import java.util.Collections;
19  import java.util.HashMap;
20  import java.util.HashSet;
21  import java.util.Map;
22  import java.util.Map.Entry;
23  import java.util.Set;
24  
25  import org.kuali.rice.core.api.exception.RiceRuntimeException;
26  import org.springframework.util.CollectionUtils;
27  
28  /**
29   * An Exception for {@link TermResolver} exceptions.
30   *
31   * @author Kuali Rice Team (rice.collab@kuali.org)
32   */
33  public class TermResolutionException extends RiceRuntimeException {
34  
35  	private static final long serialVersionUID = 1L;
36  
37  	public final String termResolverClassName;
38  	public final String outputTerm;
39  	public final Set<String> prereqs;
40  	public final Set<String> parameterNames;
41  	public final Map<String, String> parameters;
42  
43      /**
44       * Builds the resolution info string from the given values.
45       * @param tr {@link TermResolver} whose values to append to the result String if not null
46       * @param parameters Map<String, String> whose keys and values will be appended to the result String
47       * @return String representing the given values
48       */
49  	private static String buildResolutionInfoString(TermResolver<?> tr, Map<String, String> parameters) {
50  		StringBuilder result = new StringBuilder();
51  		
52  		result.append("[");
53  		result.append(TermResolver.class.getSimpleName() + "=");
54  
55  		if (tr == null) { 
56  			result.append("null");
57  		} else {
58  			result.append(tr.toString());
59  		}
60  		
61  		result.append(", parameters={");
62  
63  		boolean firstEntry = true;
64  		if (!CollectionUtils.isEmpty(parameters)) {
65  			
66  			for (Entry<String,String> parameter : parameters.entrySet()){
67  
68  				if (firstEntry) {
69  					firstEntry = false;
70  				} else { 
71  					result.append(",");
72  				}
73  
74  				result.append(parameter.getKey());
75  				result.append("=");
76  				result.append(parameter.getValue());
77  			}
78  		}
79  		
80  		result.append("}]");
81  		return result.toString();
82  	}
83  
84      /**
85       * Create a TermResolutionException with the given values
86       * @param message the exception message
87       * @param tr {@link TermResolver} to use to set values to if not null
88       * @param parameters to set the parameters value to if not null
89       * @param cause the root Throwable cause.
90       */
91  	public TermResolutionException(String message, TermResolver<?> tr, Map<String, String> parameters, Throwable cause) {
92  		super(message + " " + buildResolutionInfoString(tr, parameters), cause);
93  		if (tr == null) {
94  			termResolverClassName = "";
95  			outputTerm = null;
96  			prereqs = null;
97  			parameterNames = null;
98  		} else {
99  			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 }