Skip to content

Commit

Permalink
KFSPTS-30687: Include data file name in report file name for Accounti…
Browse files Browse the repository at this point in the history
…ng Document Batch Job. (#1569)
  • Loading branch information
nkimble committed Mar 6, 2024
1 parent b10c723 commit f695034
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@
import edu.cornell.kfs.sys.service.ReportWriterService;

public class CreateAccountingDocumentReportServiceImpl implements CreateAccountingDocumentReportService {
private static final Logger LOG = LogManager.getLogger(CreateAccountingDocumentReportServiceImpl.class);
private static final Logger LOG = LogManager.getLogger();

protected ConfigurationService configurationService;
protected ReportWriterService reportWriterService;
protected EmailService emailService;
protected ConcurBatchUtilityService concurBatchUtilityService;

@Override
public void generateReport(CreateAccountingDocumentReportItem reportItem) {
reportWriterService.initialize();
reportWriterService.initialize(reportItem.getXmlFileName());
if (reportItem.isNonBusinessRuleFailure()
&& (ObjectUtils.isNotNull(reportItem.getValidationErrorMessage()) && StringUtils.isNotBlank(reportItem.getValidationErrorMessage()))) {
LOG.info("generateReport: generateFileFailureDueToHeaderValidationErrorSummary request was issued.");
Expand All @@ -46,7 +46,7 @@ public void generateReport(CreateAccountingDocumentReportItem reportItem) {
}
reportWriterService.destroy();
}

private void generateFileProcessingSummary(CreateAccountingDocumentReportItem reportItem) {
generateSummary(reportItem);
reportWriterService.writeNewLines(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,6 @@ public interface ReportWriterService extends org.kuali.kfs.sys.service.ReportWri
public void setTitle(String title);

public void destroy();

public void initialize(String fullyQualifiedDataFileName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.kuali.kfs.kim.api.role.RoleService;
import org.kuali.kfs.krad.util.ObjectUtils;
import org.kuali.kfs.sys.KFSConstants;

import edu.cornell.kfs.sys.service.ReportWriterService;

Expand All @@ -40,11 +43,17 @@ public class ReportWriterTextServiceImpl extends org.kuali.kfs.sys.service.impl.
implements ReportWriterService {
private static final Logger LOG = LogManager.getLogger();

private static final String REPORT_FILE_NAME_INFIX = "_report_";
private static final int NUM_CHARS_ALLOWED_FOR_FULL_FILE_PATH = 512;

protected String fullFilePath;
protected String fromAddress;
protected String messageBody;
protected RoleService roleService;
protected Set<String> ccAddresses;

protected String springFileNamePrefixBackupCopy = KFSConstants.EMPTY_STRING;
protected boolean resetFileNamePrefixToSpringValue = false;

public ReportWriterTextServiceImpl() {
super();
Expand All @@ -55,7 +64,7 @@ public ReportWriterTextServiceImpl() {
public void initialize() {
try {
fullFilePath = generateFullFilePath();
LOG.debug("initialize, fullFilePath: " + fullFilePath);
LOG.info("initialize, report fullFilePath: {}", fullFilePath);
printStream = new PrintStream(fullFilePath);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
Expand All @@ -66,6 +75,81 @@ public void initialize() {
this.writeHeader(title);
}

@Override
public void initialize(String fullyQualifiedDataFileName) {
String dataFileNamePrefix = obtainDataFileName(fullyQualifiedDataFileName);
if (StringUtils.isNotBlank(fullyQualifiedDataFileName)
&& StringUtils.isNotBlank(dataFileNamePrefix)
&& fullFilePathLimitWouldNotBeExceeded(dataFileNamePrefix)) {
//use data file name as part of this report file name
configureFileNamePrefixForThisReport(dataFileNamePrefix);
LOG.info("initialize, data file name configured into report prefix name: {}", fileNamePrefix);
} //else - use spring configured default report file name
initialize();
}

/**
* This method generates a file name prefix from the fully qualified file name attribute
* (i.e. directoryPath/fileName.fileExtension)
* This functionality was created for the CreateAccountingDocumentReportItem xmlFileName attribute but could
* be utilized by any other batch job that desired the report output file contain the input data file name
* as the report file prefix. The input parameter is assumed to contain the fully qualified directory path
* as well as a file name and extension. This method strips off both the directory path and file extension
* returning just the file name portion of the string.
* When both a directory path and file extension are not detected, a zero length string is returned.
*
* Example:
* Input parameter : /infra/work/staging/fp/accountingXmlDocument/fp_ib_netsuite_20240229_050035.xml
* Return value : fp_ib_netsuite_20240229_050035
*
* @param fullyQualifiedDataFileName
*/
private String obtainDataFileName(String fullyQualifiedDataFileName) {
String onlyDataFileName = KFSConstants.EMPTY_STRING;

if (StringUtils.contains(fullyQualifiedDataFileName, KFSConstants.DELIMITER)
&& (StringUtils.contains(fullyQualifiedDataFileName, File.separator))) {
onlyDataFileName = StringUtils.substringBeforeLast(fullyQualifiedDataFileName, KFSConstants.DELIMITER);

if (StringUtils.isNotBlank(onlyDataFileName)) {
onlyDataFileName = StringUtils.substringAfterLast(onlyDataFileName, File.separator);
}
}
return onlyDataFileName;
}

private boolean fullFilePathLimitWouldNotBeExceeded(String dataFileNamePrefix) {
int numCharsCalculated = StringUtils.length(filePath) + File.separator.length()
+ StringUtils.length(dataFileNamePrefix) + StringUtils.length(REPORT_FILE_NAME_INFIX)
+ StringUtils.length(dateTimeService.toDateTimeStringForFilename(dateTimeService.getCurrentDate()))
+ StringUtils.length(fileNameSuffix);

if (numCharsCalculated < NUM_CHARS_ALLOWED_FOR_FULL_FILE_PATH) {
LOG.debug("fullFilePathLimitWouldNotBeExceeded: report file name prefix {} ok for path limit.", dataFileNamePrefix);
return true;
} else {
LOG.warn("fullFilePathLimitWouldNotBeExceeded: Using data file name as prefix for report file prefix "
+ "would make full report file path, name, extension too long. Using default report file name "
+ "prefix {} instead.", fileNamePrefix);
return false;
}
}

private void configureFileNamePrefixForThisReport(String dataFileNamePrefix) {
resetFileNamePrefixToSpringValue = true;
springFileNamePrefixBackupCopy = fileNamePrefix;
setFileNamePrefix(dataFileNamePrefix + REPORT_FILE_NAME_INFIX);
}

private void restoreFileNamePrefixToSpringConfiguredPrefix() {
if (resetFileNamePrefixToSpringValue) {
resetFileNamePrefixToSpringValue = false;
setFileNamePrefix(springFileNamePrefixBackupCopy);
springFileNamePrefixBackupCopy = KFSConstants.EMPTY_STRING;
LOG.info("restoreFileNamePrefixToSpringConfiguredPrefix, data file name prefix reset to Spring default value: {}", fileNamePrefix);
}
}

@Override
public File getReportFile() {
File report = new File(this.fullFilePath);
Expand Down Expand Up @@ -171,6 +255,7 @@ public void setTitle(String title) {

@Override
public void destroy() {
restoreFileNamePrefixToSpringConfiguredPrefix();
super.destroy();
}
}

0 comments on commit f695034

Please sign in to comment.