View Javadoc
1   /*
2    * Copyright 2011 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.modules.cda;
17  
18  import java.util.Map;
19  
20  import org.openehealth.ipf.commons.core.modules.api.ValidationException;
21  import org.openehealth.ipf.commons.core.modules.api.Validator;
22  import org.openhealthtools.mdht.uml.cda.ClinicalDocument;
23  import org.openhealthtools.mdht.uml.cda.util.CDAUtil;
24  import org.openhealthtools.mdht.uml.cda.util.CDAUtil.ValidationHandler;
25  
26  /**
27   * Validates a ClinicalDocuments instance for conformity against CDA and/or CCD
28   * schema.
29   * 
30   * @author Stefan Ivanov
31   * 
32   */
33  public class CDAR2Validator implements Validator<ClinicalDocument, Map<Object, Object>> {
34  
35  
36      @Override
37      public void validate(ClinicalDocument doc, Map<Object, Object> context) {
38          boolean isValid = CDAUtil.validate(doc, retrieveValidationHandler(context));
39          if (! isValid) {
40              throw new ValidationException("Clinical Document not valid!");
41          }
42      }
43      
44      /**
45       * Retrieves validation handler from context. If none found returns a
46       * default validation handler.
47       * 
48       * @param context
49       * @return Validation handler
50       */
51      private ValidationHandler retrieveValidationHandler(Map<Object, Object> context) {
52          if (context != null) {
53              ValidationHandler handler = (ValidationHandler) context.get(ValidationHandler.class);
54              if (handler != null) {
55                  return handler;
56              }
57          }
58          return new DefaultValidationHandler();
59      }
60  
61  }