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.responses;
17  
18  import org.openehealth.ipf.commons.core.modules.api.Validator;
19  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLObjectContainer;
20  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLQueryResponse;
21  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLRegistryObject;
22  import org.openehealth.ipf.commons.ihe.xds.core.metadata.DocumentEntryType;
23  import org.openehealth.ipf.commons.ihe.xds.core.metadata.ObjectReference;
24  import org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationProfile;
25  import org.openehealth.ipf.commons.ihe.xds.core.validate.XDSMetaDataException;
26  import org.openehealth.ipf.commons.ihe.xds.core.validate.requests.ObjectContainerValidator;
27  
28  import java.util.List;
29  
30  import static org.apache.commons.lang3.Validate.notNull;
31  import static org.openehealth.ipf.commons.ihe.xds.XDS.Interactions.ITI_51;
32  import static org.openehealth.ipf.commons.ihe.xds.core.metadata.Vocabulary.DOC_ENTRY_PATIENT_ID_EXTERNAL_ID;
33  import static org.openehealth.ipf.commons.ihe.xds.core.metadata.Vocabulary.FOLDER_CLASS_NODE;
34  import static org.openehealth.ipf.commons.ihe.xds.core.metadata.Vocabulary.FOLDER_PATIENT_ID_EXTERNAL_ID;
35  import static org.openehealth.ipf.commons.ihe.xds.core.metadata.Vocabulary.SUBMISSION_SET_CLASS_NODE;
36  import static org.openehealth.ipf.commons.ihe.xds.core.metadata.Vocabulary.SUBMISSION_SET_PATIENT_ID_EXTERNAL_ID;
37  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.MISSING_OBJ_REF;
38  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.RESULT_NOT_SINGLE_PATIENT;
39  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidatorAssertions.metaDataAssert;
40  
41  /**
42   * Validate a {@link EbXMLQueryResponse}.
43   * @author Jens Riemschneider
44   */
45  public class QueryResponseValidator implements Validator<EbXMLQueryResponse, ValidationProfile> {
46      private final RegistryResponseValidator regResponseValidator = new RegistryResponseValidator();
47      private final ObjectContainerValidator objectContainerValidator = new ObjectContainerValidator();
48  
49      @Override
50      public void validate(EbXMLQueryResponse response, ValidationProfile profile) {
51          notNull(response, "response cannot be null");
52          
53          regResponseValidator.validate(response, profile);
54          objectContainerValidator.validate(response, profile);
55  
56          List<ObjectReference> references = response.getReferences();
57          for (ObjectReference objRef : references) {
58              metaDataAssert(objRef.getId() != null, MISSING_OBJ_REF);
59          }
60  
61          if (profile != ITI_51) {
62              validatePatientIdsAreIdentical(response);
63          }
64      }
65  
66      private void validatePatientIdsAreIdentical(EbXMLObjectContainer container) throws XDSMetaDataException {
67          String patientId = checkForMultiplePatientIds(null, SUBMISSION_SET_PATIENT_ID_EXTERNAL_ID,
68                  container.getRegistryPackages(SUBMISSION_SET_CLASS_NODE));
69  
70          patientId = checkForMultiplePatientIds(patientId, DOC_ENTRY_PATIENT_ID_EXTERNAL_ID,
71                  container.getExtrinsicObjects(DocumentEntryType.STABLE_OR_ON_DEMAND));
72  
73          checkForMultiplePatientIds(patientId, FOLDER_PATIENT_ID_EXTERNAL_ID,
74                  container.getRegistryPackages(FOLDER_CLASS_NODE));
75      }
76  
77      private String checkForMultiplePatientIds(String patientId, String id, List<? extends EbXMLRegistryObject> entries) {
78          for (EbXMLRegistryObject entry : entries) {
79              String patientIdEntry = entry.getExternalIdentifierValue(id);
80              patientId = patientId == null ? patientIdEntry : patientId;
81              metaDataAssert(patientId.equals(patientIdEntry), RESULT_NOT_SINGLE_PATIENT);
82          }
83          return patientId;
84      }
85  
86  }