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 org.openehealth.ipf.commons.ihe.xds.core.XdsRuntimeException;
19  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLAdhocQueryRequest;
20  import org.openehealth.ipf.commons.ihe.xds.core.responses.ErrorCode;
21  import org.openehealth.ipf.commons.ihe.xds.core.responses.Severity;
22  import org.openehealth.ipf.commons.ihe.xds.core.transform.requests.QueryParameter;
23  import org.openehealth.ipf.commons.ihe.xds.core.validate.XDSMetaDataException;
24  
25  import java.util.Arrays;
26  import java.util.List;
27  import java.util.Objects;
28  import java.util.stream.Collectors;
29  
30  import static org.apache.commons.lang3.Validate.notNull;
31  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.MISSING_REQUIRED_QUERY_PARAMETER;
32  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.QUERY_PARAMETERS_CANNOT_BE_SET_TOGETHER;
33  
34  /**
35   * Query parameter validation to ensure that only one of the given parameters is specified.
36   * Also has the "either ... or ... " check to avoid the case that all parameters haven't a
37   * value set.
38   * @author Jens Riemschneider
39   */
40  public class ChoiceValidation implements QueryParameterValidation {
41      private final QueryParameter[] params;
42  
43      /**
44       * Constructs a validation object.
45       * @param params
46       *          parameters to validate.
47       */
48      public ChoiceValidation(QueryParameter... params) {
49          notNull(params, "params cannot be null");        
50          this.params = params;
51      }
52  
53      @Override
54      public void validate(EbXMLAdhocQueryRequest request) throws XDSMetaDataException {
55          List<String> paramSlotNames = Arrays.stream(params)
56                  .map(QueryParameter::getSlotName)
57                  .collect(Collectors.toList());
58  
59          long count = paramSlotNames.stream()
60                  .map(request::getSingleSlotValue)
61                  .filter(Objects::nonNull)
62                  .count();
63  
64          if (count == 0L) {
65              throw new XDSMetaDataException(MISSING_REQUIRED_QUERY_PARAMETER, "one of " + paramSlotNames);
66          }
67          if (count > 1L) {
68              throw new XdsRuntimeException(
69                      ErrorCode.STORED_QUERY_PARAM_NUMBER,
70                      String.format(QUERY_PARAMETERS_CANNOT_BE_SET_TOGETHER.getText(), paramSlotNames),
71                      Severity.ERROR,
72                      null);
73          }
74      }
75  }