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.query;
17  
18  import static org.apache.commons.lang3.Validate.notNull;
19  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLAdhocQueryRequest;
20  import org.openehealth.ipf.commons.ihe.xds.core.metadata.Code;
21  import org.openehealth.ipf.commons.ihe.xds.core.transform.requests.QueryParameter;
22  import org.openehealth.ipf.commons.ihe.xds.core.transform.requests.query.QuerySlotHelper;
23  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.*;
24  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidatorAssertions.metaDataAssert;
25  import org.openehealth.ipf.commons.ihe.xds.core.validate.XDSMetaDataException;
26  
27  import java.util.List;
28  import java.util.regex.Pattern;
29  
30  /**
31   * Query parameter validation for parameters that are Code-based. 
32   * @author Jens Riemschneider
33   */
34  public class CodeValidation implements QueryParameterValidation {
35      private static final Pattern PATTERN =
36              Pattern.compile("\\s*\\(\\s*'.*'(\\s*,\\s*'.*')*\\s*\\)\\s*");
37  
38      private final QueryParameter param;
39      private final boolean optional;
40  
41      /**
42       * Constructs a validation object.
43       * @param param
44       *          parameter of the code to validate.
45       * @param optional
46       *          whether the code presence is optional.
47       */
48      public CodeValidation(QueryParameter param, boolean optional) {
49          notNull(param, "param cannot be null");
50          this.param = param;
51          this.optional = optional;
52      }
53  
54      /**
55       * Constructs a validation object.
56       * @param param
57       *          parameter of the optional code to validate.
58       */
59      public CodeValidation(QueryParameter param) {
60          this(param, true);
61      }
62  
63      @Override
64      public void validate(EbXMLAdhocQueryRequest request) throws XDSMetaDataException {
65          List<String> slotValues = request.getSlotValues(param.getSlotName());
66          for (String slotValue : slotValues) {
67              metaDataAssert(slotValue != null, MISSING_REQUIRED_QUERY_PARAMETER, param);
68              metaDataAssert(PATTERN.matcher(slotValue).matches(),
69                      PARAMETER_VALUE_NOT_STRING_LIST, param);
70          }
71  
72          QuerySlotHelper slots = new QuerySlotHelper(request);
73          List<Code> codes = slots.toCodeList(param);
74  
75          if (codes != null) {
76              for (Code code : codes) {
77                  metaDataAssert(code != null, INVALID_QUERY_PARAMETER_VALUE, param);
78                  metaDataAssert(code.getCode() != null, INVALID_QUERY_PARAMETER_VALUE, param);
79                  metaDataAssert(code.getSchemeName() != null, INVALID_QUERY_PARAMETER_VALUE, param);
80              }
81          } else {
82              metaDataAssert(optional, MISSING_REQUIRED_QUERY_PARAMETER, param);
83          }
84      }
85  }