[507] | 1 | /*
|
---|
| 2 | * To change this template, choose Tools | Templates
|
---|
| 3 | * and open the template in the editor.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | package gov.hhs.fha.nhinc.auditrepository;
|
---|
| 7 |
|
---|
| 8 | import java.util.ArrayList;
|
---|
| 9 | import java.util.Calendar;
|
---|
| 10 | import java.util.Date;
|
---|
| 11 | import java.util.GregorianCalendar;
|
---|
| 12 | import java.util.List;
|
---|
| 13 | import java.util.Locale;
|
---|
| 14 |
|
---|
| 15 | import java.io.ByteArrayOutputStream;
|
---|
| 16 | import java.io.InputStream;
|
---|
| 17 | import java.sql.Blob;
|
---|
| 18 | import java.text.SimpleDateFormat;
|
---|
| 19 |
|
---|
| 20 | import javax.xml.bind.JAXBContext;
|
---|
| 21 | import javax.xml.bind.JAXBElement;
|
---|
| 22 | import javax.xml.bind.Marshaller;
|
---|
| 23 | import javax.xml.bind.Unmarshaller;
|
---|
| 24 | import javax.xml.datatype.XMLGregorianCalendar;
|
---|
| 25 |
|
---|
| 26 | import org.apache.commons.logging.Log;
|
---|
| 27 | import org.apache.commons.logging.LogFactory;
|
---|
| 28 |
|
---|
| 29 | import org.hibernate.Hibernate;
|
---|
| 30 |
|
---|
| 31 | import gov.hhs.fha.nhinc.hibernate.AuditRepositoryDAO;
|
---|
| 32 | import gov.hhs.fha.nhinc.hibernate.AuditRepositoryRecord;
|
---|
| 33 | import gov.hhs.fha.nhinc.common.nhinccommon.AcknowledgementType;
|
---|
| 34 | import gov.hhs.fha.nhinc.common.nhinccommonadapter.FindCommunitiesAndAuditEventsResponseType;
|
---|
| 35 | import gov.hhs.fha.nhinc.common.auditlog.LogEventRequestType;
|
---|
| 36 |
|
---|
| 37 | import com.services.nhinc.schema.auditmessage.*;
|
---|
| 38 | import com.services.nhinc.schema.auditmessage.AuditMessageType.ActiveParticipant;
|
---|
| 39 |
|
---|
| 40 | /**
|
---|
| 41 | *
|
---|
| 42 | * @author mflynn02
|
---|
| 43 | */
|
---|
| 44 | public class AuditRepositoryImpl {
|
---|
| 45 | private static Log log = LogFactory.getLog(AuditRepositoryImpl.class);
|
---|
| 46 | private static AuditRepositoryDAO auditLogDao = AuditRepositoryDAO.getAuditRepositoryDAOInstance();
|
---|
| 47 | private static String logStatus = "";
|
---|
| 48 |
|
---|
| 49 | /**
|
---|
| 50 | * constructor
|
---|
| 51 | */
|
---|
| 52 | public AuditRepositoryImpl() {
|
---|
| 53 | log.debug("AuditRepositoryImpl Initialized");
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | /**
|
---|
| 57 | *
|
---|
| 58 | * @return String
|
---|
| 59 | */
|
---|
| 60 | /* TODO private static String getAuditProperties(){
|
---|
| 61 | String choice = "";
|
---|
| 62 | try {
|
---|
| 63 | choice = PropertyAccessor.getProperty("auditlogchoice", "loggingstatus");
|
---|
| 64 | } catch (PropertyAccessException ex) {
|
---|
| 65 | log.error(ex.getMessage());
|
---|
| 66 | }
|
---|
| 67 | return choice;
|
---|
| 68 | }
|
---|
| 69 | */
|
---|
| 70 | /**
|
---|
| 71 | * This method is the actual implementation method for AuditLogMgr Service to Log the AuditEvents and responses the status of logging
|
---|
| 72 | * @param mess
|
---|
| 73 | * @return gov.hhs.fha.nhinc.common.nhinccommon.AcknowledgementType
|
---|
| 74 | */
|
---|
| 75 | public static AcknowledgementType logAudit(LogEventRequestType mess) {
|
---|
| 76 | log.debug("AuditLogManagerImpl.logAudit() -- Begin");
|
---|
| 77 | AcknowledgementType response = null;
|
---|
| 78 |
|
---|
| 79 | ActiveParticipant activeParticipant = null;
|
---|
| 80 | ParticipantObjectIdentificationType participantObjectIdentificationType = null;
|
---|
| 81 | AuditRepositoryRecord auditRec = new AuditRepositoryRecord();
|
---|
| 82 |
|
---|
| 83 | Date eventTimeStamp = null;
|
---|
| 84 | String eventCommunityId = null;
|
---|
| 85 | String eventUserId = null;
|
---|
| 86 | String eventParticipationIDTypeCode = null;
|
---|
| 87 | String eventPatientID = null;
|
---|
| 88 | int eventParticipationTypeCode = 0;
|
---|
| 89 | int eventParticipationTypeCodeRole = 0;
|
---|
| 90 |
|
---|
| 91 | List<ActiveParticipant> activeParticipantList = mess.getAuditMessage().getActiveParticipant();
|
---|
| 92 | EventIdentificationType eventIdentification = mess.getAuditMessage().getEventIdentification();
|
---|
| 93 | List<ParticipantObjectIdentificationType> participantObjectIdentificationList = mess.getAuditMessage().getParticipantObjectIdentification();
|
---|
| 94 |
|
---|
| 95 | if (activeParticipantList != null && activeParticipantList.size() > 0) {
|
---|
| 96 | activeParticipant = (ActiveParticipant) activeParticipantList.get(0);
|
---|
| 97 | if (activeParticipant != null) {
|
---|
| 98 | eventUserId = activeParticipant.getUserID();
|
---|
| 99 | if (eventUserId != null && !eventUserId.equals("")) {
|
---|
| 100 | auditRec.setUserId(eventUserId);
|
---|
| 101 | } else {
|
---|
| 102 | auditRec.setUserId("");
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | List<AuditSourceIdentificationType> auditSourceIdentificationList = null;
|
---|
| 108 | auditSourceIdentificationList = mess.getAuditMessage().getAuditSourceIdentification();
|
---|
| 109 | if (auditSourceIdentificationList != null && auditSourceIdentificationList.size() > 0) {
|
---|
| 110 | AuditSourceIdentificationType auditSourceIdentification = auditSourceIdentificationList.get(0);
|
---|
| 111 | eventCommunityId = auditSourceIdentification.getAuditSourceID();
|
---|
| 112 | log.debug("auditSourceID : " + eventCommunityId);
|
---|
| 113 | if (eventCommunityId != null && !eventCommunityId.equals("")) {
|
---|
| 114 | auditRec.setCommunityId(eventCommunityId);
|
---|
| 115 | } else {
|
---|
| 116 | auditRec.setCommunityId("");
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | if (participantObjectIdentificationList != null && participantObjectIdentificationList.size() > 0) {
|
---|
| 121 | participantObjectIdentificationType = (ParticipantObjectIdentificationType) participantObjectIdentificationList.get(0);
|
---|
| 122 | if (participantObjectIdentificationType != null) {
|
---|
| 123 | eventPatientID = participantObjectIdentificationType.getParticipantObjectID();
|
---|
| 124 | auditRec.setReceiverPatientId(eventPatientID);
|
---|
| 125 | eventParticipationTypeCode = participantObjectIdentificationType.getParticipantObjectTypeCode();
|
---|
| 126 | auditRec.setParticipationTypeCode(eventParticipationTypeCode);
|
---|
| 127 | eventParticipationTypeCodeRole = participantObjectIdentificationType.getParticipantObjectTypeCodeRole();
|
---|
| 128 | auditRec.setParticipationTypeCodeRole(eventParticipationTypeCodeRole);
|
---|
| 129 | eventParticipationIDTypeCode = participantObjectIdentificationType.getParticipantObjectIDTypeCode().getCode();
|
---|
| 130 | auditRec.setParticipationIDTypeCode(eventParticipationIDTypeCode);
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | auditRec.setMessageType(mess.getInterface() + " " + mess.getDirection());
|
---|
| 135 | // auditRec.setReceiverPatientId(gov.hhs.fha.nhinc.transform.audit.AuditDataTransformHelper.createCompositePatientId(auditData.getRecieverHomeCommunityId(), auditData.getReceiverPatientId()));
|
---|
| 136 | // auditRec.setSenderPatientId(auditData.getSenderPatientId());
|
---|
| 137 | auditRec.setMessage(getBlobFromAuditMessage(mess.getAuditMessage()));
|
---|
| 138 |
|
---|
| 139 | XMLGregorianCalendar XMLCalDate = eventIdentification.getEventDateTime();
|
---|
| 140 | if (XMLCalDate != null) {
|
---|
| 141 | eventTimeStamp = convertXMLGregorianCalendarToDate(XMLCalDate);
|
---|
| 142 | auditRec.setTimeStamp(eventTimeStamp);
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | List<AuditRepositoryRecord> auditRecList = new ArrayList();
|
---|
| 146 | auditRecList.add(auditRec);
|
---|
| 147 | boolean result = auditLogDao.insertAuditRepository(auditRecList);
|
---|
| 148 |
|
---|
| 149 | response = new AcknowledgementType();
|
---|
| 150 | if (result == true) {
|
---|
| 151 | response.setMessage("Created Log Message in Database...");
|
---|
| 152 | } else {
|
---|
| 153 | response.setMessage("Unable to create Log Message in Database...");
|
---|
| 154 | }
|
---|
| 155 | log.debug("AuditLogManagerImpl.logAudit() -- End");
|
---|
| 156 | return response;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | public static Blob getBlobFromAuditMessage(com.services.nhinc.schema.auditmessage.AuditMessageType mess) {
|
---|
| 160 | Blob eventMessage = null; //Not Implemented
|
---|
| 161 | try {
|
---|
| 162 | JAXBContext jaxbContext = JAXBContext.newInstance("com.services.nhinc.schema.auditmessage");
|
---|
| 163 | Marshaller marshaller = jaxbContext.createMarshaller();
|
---|
| 164 | ByteArrayOutputStream baOutStrm = new ByteArrayOutputStream();
|
---|
| 165 | baOutStrm.reset();
|
---|
| 166 | com.services.nhinc.schema.auditmessage.ObjectFactory factory = new com.services.nhinc.schema.auditmessage.ObjectFactory();
|
---|
| 167 | JAXBElement oJaxbElement = factory.createAuditMessage(mess);
|
---|
| 168 | baOutStrm.close();
|
---|
| 169 | marshaller.marshal(oJaxbElement, baOutStrm);
|
---|
| 170 | byte[] buffer = baOutStrm.toByteArray();
|
---|
| 171 | eventMessage = Hibernate.createBlob(buffer);
|
---|
| 172 | } catch (Exception e) {
|
---|
| 173 | log.error("Exception during Blob conversion :" + e.getMessage());
|
---|
| 174 | e.printStackTrace();
|
---|
| 175 | }
|
---|
| 176 | return eventMessage;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | /**
|
---|
| 180 | * This is the actual implementation for AuditLogMgr Service for AuditQuery returns the AuditEventsReponse
|
---|
| 181 | * @param query
|
---|
| 182 | * @return FindAuditEventsResponseType
|
---|
| 183 | */
|
---|
| 184 | public static FindCommunitiesAndAuditEventsResponseType findAudit(FindAuditEventsType query) {
|
---|
| 185 | log.debug("AuditLogManagerImpl.findAudit() -- Begin");
|
---|
| 186 |
|
---|
| 187 | if (logStatus.equals("")) {
|
---|
| 188 | // TODO logStatus = getAuditProperties();
|
---|
| 189 | logStatus = "on";
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | if (logStatus.equalsIgnoreCase("off")) {
|
---|
| 193 | log.info("Enable Audit Logging Before Making Query by changing the " +
|
---|
| 194 | "value in 'auditlogchoice' properties file");
|
---|
| 195 | return null;
|
---|
| 196 | }
|
---|
| 197 | FindCommunitiesAndAuditEventsResponseType auditEvents = new FindCommunitiesAndAuditEventsResponseType();
|
---|
| 198 | String patientId = query.getPatientId();
|
---|
| 199 | String userId = query.getUserId();
|
---|
| 200 | Date beginDate = null;
|
---|
| 201 | Date endDate = null;
|
---|
| 202 | XMLGregorianCalendar xmlBeginDate = query.getBeginDateTime();
|
---|
| 203 | XMLGregorianCalendar xmlEndDate = query.getEndDateTime();
|
---|
| 204 |
|
---|
| 205 | if (xmlBeginDate != null) {
|
---|
| 206 | beginDate = convertXMLGregorianCalendarToDate(xmlBeginDate);
|
---|
| 207 | }
|
---|
| 208 | if (xmlEndDate != null) {
|
---|
| 209 | endDate = convertXMLGregorianCalendarToDate(xmlEndDate);
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | List<AuditRepositoryRecord> responseList = auditLogDao.queryAuditRepositoryOnCriteria(userId, patientId, beginDate, endDate);
|
---|
| 213 | log.debug ("after query call to logDAO.");
|
---|
| 214 | /* if (responseList != null && responseList.size() > 0) {*/
|
---|
| 215 | log.debug("responseList is not NULL ");
|
---|
| 216 | auditEvents = buildAuditReponseType(responseList);
|
---|
| 217 | /* }*/
|
---|
| 218 |
|
---|
| 219 | log.debug("AuditLogManagerImpl.findAudit() -- End");
|
---|
| 220 | return auditEvents;
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | /**
|
---|
| 224 | * This method builds the Actual Response from each of the EventLogList coming from Database
|
---|
| 225 | * @param eventsList
|
---|
| 226 | * @return CommunitiesAndFindAdutiEventResponse
|
---|
| 227 | */
|
---|
| 228 | public static FindCommunitiesAndAuditEventsResponseType buildAuditReponseType(List<AuditRepositoryRecord> auditRecList) {
|
---|
| 229 | log.debug("AuditLogManagerImpl.buildAuditResponseType -- Begin");
|
---|
| 230 | FindCommunitiesAndAuditEventsResponseType auditResType = new FindCommunitiesAndAuditEventsResponseType();
|
---|
| 231 | FindAuditEventsResponseType response = new FindAuditEventsResponseType();
|
---|
| 232 | AuditMessageType auditMessageType = null;
|
---|
| 233 | Blob blobMessage = null;
|
---|
| 234 |
|
---|
| 235 | int size = auditRecList.size();
|
---|
| 236 | AuditRepositoryRecord eachRecord = null;
|
---|
| 237 | for (int i = 0; i < size; i++) {
|
---|
| 238 | eachRecord = auditRecList.get(i);
|
---|
| 239 | auditMessageType = new AuditMessageType();
|
---|
| 240 | blobMessage = eachRecord.getMessage();
|
---|
| 241 | if (blobMessage != null) {
|
---|
| 242 | auditMessageType = unMarshallBlobToAuditMess(blobMessage);
|
---|
| 243 | ActiveParticipant act = (ActiveParticipant) auditMessageType.getActiveParticipant().get(0);
|
---|
| 244 | response.getFindAuditEventsReturn().add(auditMessageType);
|
---|
| 245 |
|
---|
| 246 | if (auditMessageType.getAuditSourceIdentification().size() > 0 &&
|
---|
| 247 | auditMessageType.getAuditSourceIdentification().get(0) != null &&
|
---|
| 248 | auditMessageType.getAuditSourceIdentification().get(0).getAuditSourceID() != null &&
|
---|
| 249 | auditMessageType.getAuditSourceIdentification().get(0).getAuditSourceID().length() > 0) {
|
---|
| 250 | String tempCommunity = auditMessageType.getAuditSourceIdentification().get(0).getAuditSourceID();
|
---|
| 251 | if (!auditResType.getCommunities().contains(tempCommunity) ) {
|
---|
| 252 | auditResType.getCommunities().add(tempCommunity);
|
---|
| 253 | log.debug("Adding community " + tempCommunity);
|
---|
| 254 | }
|
---|
| 255 | }
|
---|
| 256 | }
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | auditResType.setFindAuditEventResponse(response);
|
---|
| 260 | log.debug("AuditLogManagerImpl.buildAuditResponseType -- End");
|
---|
| 261 | return auditResType;
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | /**
|
---|
| 265 | * This method unmarshalls XML Blob to AuditMessage
|
---|
| 266 | * @param auditBlob
|
---|
| 267 | * @return AuditMessageType
|
---|
| 268 | */
|
---|
| 269 | public static AuditMessageType unMarshallBlobToAuditMess(Blob auditBlob) {
|
---|
| 270 | log.debug("AuditLogManagerImpl.unMarshallBlobToAuditMess -- Begin");
|
---|
| 271 | AuditMessageType auditMessageType = null;
|
---|
| 272 | try {
|
---|
| 273 | if (auditBlob != null && ((int) auditBlob.length()) > 0) {
|
---|
| 274 | InputStream in = auditBlob.getBinaryStream();
|
---|
| 275 | JAXBContext jaxbContext = JAXBContext.newInstance("com.services.nhinc.schema.auditmessage");
|
---|
| 276 | Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
|
---|
| 277 | // com.services.nhinc.schema.auditmessage.ObjectFactory factory = new com.services.nhinc.schema.auditmessage.ObjectFactory();
|
---|
| 278 | JAXBElement jaxEle = (JAXBElement) unmarshaller.unmarshal(in);
|
---|
| 279 | auditMessageType = (AuditMessageType) jaxEle.getValue();
|
---|
| 280 | }
|
---|
| 281 | } catch (Exception e) {
|
---|
| 282 | log.error("Blob to Audit Message Conversion Error : " + e.getMessage());
|
---|
| 283 | e.printStackTrace();
|
---|
| 284 | }
|
---|
| 285 | log.debug("AuditLogManagerImpl.unMarshallBlobToAuditMess -- End");
|
---|
| 286 | return auditMessageType;
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | /**
|
---|
| 290 | * This method converts an XMLGregorianCalendar date to java.util.Date
|
---|
| 291 | * @param xmlCalDate
|
---|
| 292 | * @return java.util.Date
|
---|
| 293 | */
|
---|
| 294 | private static Date convertXMLGregorianCalendarToDate(XMLGregorianCalendar xmlCalDate) {
|
---|
| 295 | Calendar cal = Calendar.getInstance(Locale.getDefault());
|
---|
| 296 | log.info("cal.getTime() -> "+cal.getTime());
|
---|
| 297 | cal.setTime(xmlCalDate.toGregorianCalendar().getTime());
|
---|
| 298 | Date eventDate = cal.getTime();
|
---|
| 299 | log.info("eventDate -> "+eventDate);
|
---|
| 300 | return eventDate;
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 | /**
|
---|
| 304 | * This method converts an XMLGregorianCalendar date to java.util.Date
|
---|
| 305 | * @param xmlCalDate
|
---|
| 306 | * @return String
|
---|
| 307 | */
|
---|
| 308 | private static String convertXMLGregorianCalendarToString(XMLGregorianCalendar xmlCalDate) {
|
---|
| 309 | GregorianCalendar calDate = xmlCalDate.toGregorianCalendar();
|
---|
| 310 | Date eventDate = calDate.getTime();
|
---|
| 311 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
---|
| 312 | String strDate = sdf.format(eventDate);
|
---|
| 313 | return strDate;
|
---|
| 314 | }
|
---|
| 315 |
|
---|
| 316 | }
|
---|