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.requests;
17  
18  import static org.apache.commons.lang3.Validate.notNull;
19  import org.openehealth.ipf.commons.core.modules.api.Validator;
20  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLExtrinsicObject;
21  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLProvideAndRegisterDocumentSetRequest;
22  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLSubmitObjectsRequest;
23  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.MISSING_DOCUMENT_FOR_DOC_ENTRY;
24  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.MISSING_DOC_ENTRY_FOR_DOCUMENT;
25  
26  import org.openehealth.ipf.commons.ihe.xds.core.metadata.DocumentEntryType;
27  import org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationProfile;
28  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidatorAssertions.metaDataAssert;
29  
30  import javax.activation.DataHandler;
31  import java.util.HashSet;
32  import java.util.Map;
33  import java.util.Set;
34  
35  /**
36   * Validates a {@link EbXMLSubmitObjectsRequest} request.
37   * @author Jens Riemschneider
38   */
39  public class ProvideAndRegisterDocumentSetRequestValidator implements Validator<EbXMLProvideAndRegisterDocumentSetRequest, ValidationProfile>{
40      private final SubmitObjectsRequestValidator submitObjectsRequestValidator = 
41          new SubmitObjectsRequestValidator();
42      
43      @Override
44      public void validate(EbXMLProvideAndRegisterDocumentSetRequest request, ValidationProfile profile) {
45          notNull(request, "request cannot be null");
46  
47          submitObjectsRequestValidator.validate(request, profile);
48          
49          validateDocuments(request);
50      }
51  
52      private void validateDocuments(EbXMLProvideAndRegisterDocumentSetRequest request) {
53          Map<String, DataHandler> documents = request.getDocuments();
54  
55          Set<String> docEntryIds = new HashSet<>();
56          for (EbXMLExtrinsicObject docEntry : request.getExtrinsicObjects(DocumentEntryType.STABLE.getUuid())) {
57              String docId = docEntry.getId();
58              if (docId != null) {
59                  docEntryIds.add(docId);
60                  metaDataAssert(documents.get(docId) != null, MISSING_DOCUMENT_FOR_DOC_ENTRY, docId);                
61              }
62          }
63                  
64          for (String docId : documents.keySet()) {
65              metaDataAssert(docEntryIds.contains(docId), MISSING_DOC_ENTRY_FOR_DOCUMENT, docId);
66          }
67      }
68  }