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 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
32
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
43
44
45
46
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
56
57
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 }