View Javadoc
1   /*
2    * Copyright 2009 the original author or authors.
3    * 
4    * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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.openehealth.ipf.commons.ihe.xds.core.responses;
17  
18  import org.apache.commons.lang3.builder.ToStringBuilder;
19  import org.apache.commons.lang3.builder.ToStringStyle;
20  import org.openehealth.ipf.commons.ihe.xds.core.XdsRuntimeException;
21  import org.openehealth.ipf.commons.ihe.xds.core.validate.XDSMetaDataException;
22  
23  import javax.xml.bind.annotation.XmlAccessType;
24  import javax.xml.bind.annotation.XmlAccessorType;
25  import javax.xml.bind.annotation.XmlType;
26  import java.io.Serializable;
27  
28  /**
29   * Contains information about an error.
30   * All members of this class are allowed to be <code>null</code>.
31   * @author Jens Riemschneider
32   */
33  @XmlAccessorType(XmlAccessType.FIELD)
34  @XmlType(name = "ErrorInfo", propOrder = {"errorCode", "codeContext", "severity", "location", "customErrorCode"})
35  public class ErrorInfo implements Serializable {
36      private static final long serialVersionUID = 7615868122051414551L;
37      
38      private ErrorCode errorCode;
39      private String codeContext;
40      private Severity severity;
41      private String location;
42  
43      // is used when errorCode is equal to ErrorCode._USER_DEFINED
44      private String customErrorCode;
45  
46  
47      /**
48       * Constructs an error info.
49       */
50      public ErrorInfo() {}
51      
52      /**
53       * Constructs an error info.
54       * @param errorCode
55       *          the error that occurred.
56       * @param codeContext
57       *          the context in which the error occurred.
58       * @param severity
59       *          the severity of the error.
60       * @param location
61       *          the location in which the error occurred.
62       * @param customErrorCode
63       *          user-defined error code for the cases when the <code>errorCode</code> parameter
64       *          equals to {@link ErrorCode#_USER_DEFINED}, otherwise will be ignored.
65       */
66      public ErrorInfo(ErrorCode errorCode, String codeContext, Severity severity, String location, String customErrorCode) {
67          this.errorCode = errorCode;
68          this.codeContext = codeContext;
69          this.severity = severity;
70          this.location = location;
71          this.customErrorCode = customErrorCode;
72      }
73  
74      /**
75       * Constructs an error info from the given exception.
76       * @param throwable
77       *          the exception that occurred.
78       * @param defaultMetaDataError
79       *          the default error code for {@link XDSMetaDataException}.
80       * @param defaultError
81       *          the default error code for any other exception.
82       * @param defaultLocation
83       *          default error location.
84       */
85      public ErrorInfo(
86              Throwable throwable,
87              ErrorCode defaultMetaDataError,
88              ErrorCode defaultError,
89              String defaultLocation)
90      {
91          this(defaultError, throwable.getMessage(), Severity.ERROR, defaultLocation, null);
92          Throwable t = throwable;
93          while (t != null) {
94              if (t instanceof XDSMetaDataException) {
95                  XDSMetaDataException metaDataException = (XDSMetaDataException) t;
96                  this.errorCode = metaDataException.getValidationMessage().getErrorCode();
97                  if (this.errorCode == null) {
98                      this.errorCode = defaultMetaDataError;
99                  }
100                 this.codeContext = metaDataException.getMessage();
101                 return;
102             }
103             if (t instanceof XdsRuntimeException) {
104                 XdsRuntimeException exception = (XdsRuntimeException) t;
105                 this.errorCode = exception.getErrorCode();
106                 this.codeContext = exception.getCodeContext();
107                 this.severity = exception.getSeverity();
108                 this.location = exception.getLocation();
109                 return;
110             }
111             t = t.getCause();
112         }
113     }
114 
115     /**
116      * @return the error that occurred.
117      */
118     public ErrorCode getErrorCode() {
119         return errorCode;
120     }
121     
122     /**
123      * @param errorCode
124      *          the error that occurred.
125      */
126     public void setErrorCode(ErrorCode errorCode) {
127         this.errorCode = errorCode;
128     }
129     
130     /**
131      * @return the context in which the error occurred.
132      */
133     public String getCodeContext() {
134         return codeContext;
135     }
136     
137     /**
138      * @param codeContext
139      *          the context in which the error occurred.
140      */
141     public void setCodeContext(String codeContext) {
142         this.codeContext = codeContext;
143     }
144     
145     /**
146      * @return the severity of the error.
147      */
148     public Severity getSeverity() {
149         return severity;
150     }
151     
152     /**
153      * @param severity
154      *          the severity of the error.
155      */
156     public void setSeverity(Severity severity) {
157         this.severity = severity;
158     }
159     
160     /**
161      * @return the location in which the error occurred.
162      */
163     public String getLocation() {
164         return location;
165     }
166     
167     /**
168      * @param location
169      *          the location in which the error occurred.
170      */
171     public void setLocation(String location) {
172         this.location = location;
173     }
174 
175     /**
176      * @return
177      *      custom error code, if any set.
178      */
179     public String getCustomErrorCode() {
180         return customErrorCode;
181     }
182 
183     public void setCustomErrorCode(String customErrorCode) {
184         this.customErrorCode = customErrorCode;
185     }
186 
187     @Override
188     public int hashCode() {
189         final int prime = 31;
190         int result = 1;
191         result = prime * result + ((codeContext == null) ? 0 : codeContext.hashCode());
192         result = prime * result + ((errorCode == null) ? 0 : errorCode.hashCode());
193         result = prime * result + ((location == null) ? 0 : location.hashCode());
194         result = prime * result + ((severity == null) ? 0 : severity.hashCode());
195         result = prime * result + ((customErrorCode == null) ? 0 : customErrorCode.hashCode());
196         return result;
197     }
198 
199     @Override
200     public boolean equals(Object obj) {
201         if (this == obj)
202             return true;
203         if (obj == null)
204             return false;
205         if (getClass() != obj.getClass())
206             return false;
207         ErrorInfo other = (ErrorInfo) obj;
208         if (codeContext == null) {
209             if (other.codeContext != null)
210                 return false;
211         } else if (!codeContext.equals(other.codeContext))
212             return false;
213         if (errorCode == null) {
214             if (other.errorCode != null)
215                 return false;
216         } else if (!errorCode.equals(other.errorCode))
217             return false;
218         if (location == null) {
219             if (other.location != null)
220                 return false;
221         } else if (!location.equals(other.location))
222             return false;
223         if (severity == null) {
224             if (other.severity != null)
225                 return false;
226         } else if (!severity.equals(other.severity))
227             return false;
228         if (customErrorCode == null) {
229             if (other.customErrorCode != null)
230                 return false;
231         } else if (!customErrorCode.equals(other.customErrorCode))
232             return false;
233         return true;
234     }    
235 
236     @Override
237     public String toString() {
238         return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
239     }
240 }