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