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.validate;
17  
18  import org.openehealth.ipf.commons.core.modules.api.ValidationException;
19  import org.openehealth.ipf.commons.ihe.xds.core.transform.requests.QueryParameter;
20  
21  
22  /**
23   * Thrown if XDS meta data did not match the expectations.
24   * @author Jens Riemschneider
25   */
26  public class XDSMetaDataException extends ValidationException {
27      private static final long serialVersionUID = -394009702858390335L;
28      
29      private final ValidationMessage validationMessage;
30  
31      /**
32       * Constructs the exception.
33       * @param validationMessage
34       *          the validation message.
35       * @param details
36       *          objects required by the message text formatting.
37       */
38      public XDSMetaDataException(ValidationMessage validationMessage, Object... details) {
39          super(String.format(validationMessage.getText(), unwrapQueryParameterNames(details)));
40          this.validationMessage = validationMessage;
41      }
42  
43      /**
44       * @return the validation message.
45       */
46      public ValidationMessage getValidationMessage() {
47          return validationMessage;
48      }
49  
50      /**
51       * Recursively replaces all
52       * {@link org.openehealth.ipf.commons.ihe.xds.core.transform.requests.QueryParameter}
53       * elements found in the given array with their corresponding ebXML slot names.
54       * @param array
55       *      array potentially containing
56       *      {@link org.openehealth.ipf.commons.ihe.xds.core.transform.requests.QueryParameter}
57       *      elements
58       * @return the same array instance, possibly modified in-place.
59       */
60      private static Object[] unwrapQueryParameterNames(Object[] array) {
61          for (int i = 0; i < array.length; ++i) {
62              if (array[i] instanceof QueryParameter) {
63                  QueryParameter param = (QueryParameter) array[i];
64                  array[i] = param.getSlotName();
65              }
66              else if (array[i] instanceof QueryParameter[]) {
67                  QueryParameter[] params = (QueryParameter[]) array[i];
68                  if (params.length > 0) {
69                      StringBuilder sb = new StringBuilder();
70                      for (QueryParameter param : params) {
71                          sb.append(", ").append(param.getSlotName());
72                      }
73                      array[i] = sb.replace(0, 2, "[").append(']').toString();
74                  }
75              }
76          }
77          return array;
78      }
79  
80  }