001package ca.cdr.api.util; 002 003/*- 004 * #%L 005 * Smile CDR - CDR 006 * %% 007 * Copyright (C) 2016 - 2024 Smile CDR, Inc. 008 * %% 009 * All rights reserved. 010 * #L% 011 */ 012 013import ca.cdr.api.model.enm.TransactionLogBodyTypeEnum; 014import ca.cdr.api.model.enm.TransactionLogOutcomeEnum; 015import ca.cdr.api.model.enm.TransactionLogStepTypeEnum; 016import ca.cdr.api.model.json.TransactionLogStepJson; 017import ca.cdr.api.pub.hl7v2.model.MappingMessage; 018import jakarta.annotation.Nullable; 019 020import java.util.Date; 021import java.util.List; 022 023import static org.apache.commons.lang3.StringUtils.isNotBlank; 024 025public class MappingMessageTransactionLogUtil { 026 private MappingMessageTransactionLogUtil() {} 027 028 /** 029 * Given a set of mapping issues (if any), generate a {@link TransactionLogStepJson} 030 * that can be sent to the transaction log service. Will only return 031 * an object if any issues are actually present, otherwise returns null. 032 * 033 * @return Returns null if no issues detected 034 */ 035 @Nullable 036 public static TransactionLogStepJson createTransactionLogStep(List<MappingMessage> theIssues) { 037 if (theIssues.isEmpty()) { 038 return null; 039 } 040 041 // Construct a markdown message containing any processing messages 042 StringBuilder messageMarkdown = new StringBuilder(); 043 for (MappingMessage nextIssue : theIssues) { 044 if (!messageMarkdown.isEmpty()) { 045 messageMarkdown.append("\n"); 046 } 047 messageMarkdown.append("* "); 048 messageMarkdown.append("[**").append(nextIssue.getLevel().name()).append("**] "); 049 if (isNotBlank(nextIssue.getPath())) { 050 messageMarkdown.append(" `").append(nextIssue.getPath()).append("` - "); 051 } 052 messageMarkdown.append(nextIssue.getMessage()); 053 } 054 055 boolean hasErrors = 056 theIssues.stream().anyMatch(t -> t.getLevel().ordinal() >= MappingMessage.MessageLevel.ERROR.ordinal()); 057 058 // Add a processing step with the markdown message as the body 059 TransactionLogStepJson mapStep = new TransactionLogStepJson(); 060 mapStep.setOutcome(hasErrors ? TransactionLogOutcomeEnum.FAIL : TransactionLogOutcomeEnum.SUCCESS); 061 mapStep.setType(TransactionLogStepTypeEnum.PROCESSING); 062 mapStep.setBody(messageMarkdown.toString()); 063 mapStep.setBodyType(TransactionLogBodyTypeEnum.MESSAGE_MARKDOWN); 064 mapStep.setInitialTimestamp(new Date()); 065 return mapStep; 066 } 067}