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.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
33
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
44
45
46
47
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 }