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 static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.INVALID_QUERY_PARAMETER_VALUE;
20  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.MISSING_REQUIRED_QUERY_PARAMETER;
21  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.PARAMETER_VALUE_NOT_STRING_LIST;
22  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidatorAssertions.metaDataAssert;
23  
24  import java.util.List;
25  import java.util.regex.Pattern;
26  
27  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLAdhocQueryRequest;
28  import org.openehealth.ipf.commons.ihe.xds.core.metadata.AvailabilityStatus;
29  import org.openehealth.ipf.commons.ihe.xds.core.transform.requests.QueryParameter;
30  import org.openehealth.ipf.commons.ihe.xds.core.transform.requests.query.QuerySlotHelper;
31  import org.openehealth.ipf.commons.ihe.xds.core.validate.XDSMetaDataException;
32  
33  /**
34   * Query parameter validation for parameters that are AvailabilityStatus-based.
35   * @author Jens Riemschneider
36   */
37  public class StatusValidation implements QueryParameterValidation {
38      private static final Pattern PATTERN =
39              Pattern.compile("\\s*\\(\\s*'.*'(\\s*,\\s*'.*')*\\s*\\)\\s*");
40  
41      private final QueryParameter param;
42  
43      /**
44       * Constructs a validation object.
45       * @param param
46       *          parameter to validate.
47       */
48      public StatusValidation(QueryParameter param) {
49          notNull(param, "param cannot be null");
50          this.param = param;
51      }
52  
53      @Override
54      public void validate(EbXMLAdhocQueryRequest request) throws XDSMetaDataException {
55          List<String> slotValues = request.getSlotValues(param.getSlotName());
56          metaDataAssert(!slotValues.isEmpty(), MISSING_REQUIRED_QUERY_PARAMETER, slotValues);
57          for (String slotValue : slotValues) {
58              metaDataAssert(slotValue != null, MISSING_REQUIRED_QUERY_PARAMETER, param);
59              metaDataAssert(PATTERN.matcher(slotValue).matches(),
60                      PARAMETER_VALUE_NOT_STRING_LIST, param);
61          }
62  
63          QuerySlotHelper slots = new QuerySlotHelper(request);
64  
65          List<AvailabilityStatus> list = slots.toStatus(param);
66  
67          if (list.isEmpty()) {
68              for (String value : slots.toStringList(param)) {
69                  metaDataAssert(AvailabilityStatus.valueOfOpcode(value) != null, INVALID_QUERY_PARAMETER_VALUE, value);
70              }
71          }
72          metaDataAssert(!list.isEmpty(), MISSING_REQUIRED_QUERY_PARAMETER, slotValues);
73  
74      }
75  }