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.audit;
17  
18  import org.openehealth.ipf.commons.audit.AuditContext;
19  import org.openehealth.ipf.commons.audit.codes.EventOutcomeIndicator;
20  import org.openehealth.ipf.commons.ihe.core.atna.AuditStrategySupport;
21  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLRegistryError;
22  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLRegistryResponse;
23  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLRegistryResponse30;
24  import org.openehealth.ipf.commons.ihe.xds.core.responses.Severity;
25  import org.openehealth.ipf.commons.ihe.xds.core.responses.Status;
26  import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rs.RegistryResponseType;
27  
28  
29  /**
30   * Basis for Strategy pattern implementation for ATNA Auditing in XDS transactions.
31   * @author Dmytro Rud
32   */
33  public abstract class XdsAuditStrategy<T extends XdsAuditDataset> extends AuditStrategySupport<T> {
34  
35      /**
36       * Constructs an XDS audit strategy.
37       *   
38       * @param serverSide
39       *      whether this is a server-side or a client-side strategy.
40       */
41      public XdsAuditStrategy(boolean serverSide) {
42          super(serverSide);
43      }
44  
45      /**
46       * A helper method that analyzes the given registry response and 
47       * determines the corresponding RFC 3881 event outcome code.
48       * @param response registry to analyze.
49       * @return outcome code.
50       */
51      private static EventOutcomeIndicator getEventOutcomeCodeFromRegistryResponse(EbXMLRegistryResponse response) {
52          try {
53              if (response.getStatus() == Status.SUCCESS) {
54                  return EventOutcomeIndicator.Success;
55              }
56              if (response.getErrors().isEmpty()) {
57                  return EventOutcomeIndicator.SeriousFailure;
58              }
59              // determine the highest error severity
60              for (EbXMLRegistryError error : response.getErrors()) {
61                  if (error.getSeverity() == Severity.ERROR) {
62                      return EventOutcomeIndicator.SeriousFailure;
63                  }
64              }
65              return EventOutcomeIndicator.MinorFailure;
66          } catch (Exception e) {
67              return EventOutcomeIndicator.SeriousFailure;
68          }
69      }
70  
71      private static String getEventOutcomeDescriptionFromRegistryResponse(EbXMLRegistryResponse response) {
72          if (response.getErrors().isEmpty()) {
73              return null;
74          }
75          for (EbXMLRegistryError error : response.getErrors()) {
76              if (error.getSeverity() == Severity.ERROR) {
77                  return error.getCodeContext();
78              }
79          }
80          return response.getErrors().get(0).getCodeContext();
81      }
82  
83  
84          @Override
85      public EventOutcomeIndicator getEventOutcomeIndicator(Object pojo) {
86          RegistryResponseType response = (RegistryResponseType) pojo;
87          EbXMLRegistryResponse ebXML = new EbXMLRegistryResponse30(response);
88          return getEventOutcomeCodeFromRegistryResponse(ebXML);
89      }
90  
91      @Override
92      public String getEventOutcomeDescription(Object pojo) {
93          RegistryResponseType response = (RegistryResponseType) pojo;
94          EbXMLRegistryResponse ebXML = new EbXMLRegistryResponse30(response);
95          return getEventOutcomeDescriptionFromRegistryResponse(ebXML);
96      }
97  
98      @Override
99      public boolean enrichAuditDatasetFromResponse(T auditDataset, Object response, AuditContext auditContext) {
100         EventOutcomeIndicator outcomeCodes = getEventOutcomeIndicator(response);
101         auditDataset.setEventOutcomeIndicator(outcomeCodes);
102         auditDataset.setEventOutcomeDescription(getEventOutcomeDescription(response));
103         return outcomeCodes == EventOutcomeIndicator.Success;
104     }
105 
106 }