1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
36
37
38
39
40 public class ChoiceValidation implements QueryParameterValidation {
41 private final QueryParameter[] params;
42
43
44
45
46
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 }