View Javadoc

1   /**
2    * Copyright 2005-2011 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  public class TermResolutionException extends RiceRuntimeException {
29  
30  	private static final long serialVersionUID = 1L;
31  
32  	public final String termResolverClassName;
33  	public final String outputTerm;
34  	public final Set<String> prereqs;
35  	public final Set<String> parameterNames;
36  	public final Map<String, String> parameters;
37  	
38  	private static String buildResolutionInfoString(TermResolver<?> tr, Map<String, String> parameters) {
39  		StringBuilder result = new StringBuilder();
40  		
41  		result.append("[");
42  		result.append(TermResolver.class.getSimpleName() + "=");
43  
44  		if (tr == null) { 
45  			result.append("null");
46  		} else {
47  			result.append(tr.toString());
48  		}
49  		
50  		result.append(", parameters={");
51  
52  		boolean firstEntry = true;
53  		if (!CollectionUtils.isEmpty(parameters)) {
54  			
55  			for (Entry<String,String> parameter : parameters.entrySet()){
56  
57  				if (firstEntry) {
58  					firstEntry = false;
59  				} else { 
60  					result.append(",");
61  				}
62  
63  				result.append(parameter.getKey());
64  				result.append("=");
65  				result.append(parameter.getValue());
66  			}
67  		}
68  		
69  		result.append("}]");
70  		return result.toString();
71  	}
72  
73  	public TermResolutionException(String message, TermResolver<?> tr, Map<String, String> parameters, Throwable cause) {
74  		super(message + " " + buildResolutionInfoString(tr, parameters), cause);
75  		if (tr == null) {
76  			termResolverClassName = "";
77  			outputTerm = null;
78  			prereqs = null;
79  			parameterNames = null;
80  		} else {
81  			termResolverClassName = tr.getClass().getName();
82  			outputTerm = tr.getOutput();
83  			prereqs = tr.getPrerequisites();
84  			parameterNames = Collections.unmodifiableSet(new HashSet<String>(tr.getParameterNames()));
85  		}
86  		if (parameters != null){
87  			this.parameters = Collections.unmodifiableMap(new HashMap<String, String>(parameters));
88  		} else {
89  			this.parameters = null;
90  		}
91  	}
92  
93  	public TermResolutionException(String message, TermResolver<?> tr, Map<String, String> parameters) {
94  		super(message + " " + buildResolutionInfoString(tr, parameters));
95  		if (tr == null) {
96  			termResolverClassName = "";
97  			outputTerm = null;
98  			prereqs = null;
99  			parameterNames = null;
100 		} else {
101 			termResolverClassName = tr.getClass().getName();
102 			outputTerm = tr.getOutput();
103 			prereqs = tr.getPrerequisites();
104 			parameterNames = Collections.unmodifiableSet(new HashSet<String>(tr.getParameterNames()));
105 		}
106 		if (parameters != null){
107 			this.parameters = Collections.unmodifiableMap(new HashMap<String, String>(parameters));
108 		} else {
109 			this.parameters = null;
110 		}
111 	}
112 
113 }