public class StatementJob implements Job {
private static final Logger LOGGER = LoggerFactory.getLogger(StatementJob.class);
private static final String JOB_IS_DISABLED = "Uploads statement job is disabled and will not run";
private static final String UNABLE_TO_CONTINUE = "Unable to continue";
private final Configuration jobConfiguration;
private final Log log;
public StatementJob(JobConfiguration jobConfiguration) {
this.jobConfiguration = jobConfiguration;
this.log = new JobLog();
}
@Override
public Log execute() {
LOGGER.info("Stating uploading statements");
final JobProperties properties;
try {
properties = jobConfiguration.loadProperties();
} catch (BadConfigurationException e) {
return log.addError(e.getMessage());
}
if (!properties.isRunJob()) {
return log.addSuccess(JOB_IS_DISABLED);
}
final Employee statementUploader = Employee.load(properties.getStatementUploader());
final FileStatements fileStatements = new Statements(properties.getDirectories());
fileStatements
.getInboundStatements()
.forEach(inboundStatement -> {
try {
fileStatements.moveToWipLocation(inboundStatement);
fileStatements
.getWipStatements()
.forEach(wipFileStatement -> {
try {
new UploadableDocument(statementUploader, wipFileStatement).upload();
fileStatements.moveToArchiveLocation(wipFileStatement);
} catch (FailedToUploadDocumentException e) {
log.addError(e.getMessage());
try {
fileStatements.moveToErrorLocation(wipFileStatement);
} catch (UnableToMoveFileToDirectoryException ex) {
LOGGER.error(UNABLE_TO_CONTINUE, e);
throw new FatalJobException(UNABLE_TO_CONTINUE, e);
}
} catch (UnableToReadFileException | UnableToMoveFileToDirectoryException e) {
LOGGER.error(UNABLE_TO_CONTINUE, e);
throw new FatalJobException("Unable to continue", e);
}
});
} catch (UnableToMoveFileToDirectoryException e) {
LOGGER.error(UNABLE_TO_CONTINUE, e);
throw new FatalJobException("Unable to continue", e);
}
});
return log;
}
}
```
What does the code do?
- There is
FileStatementswhich represents files, calledStatement, stored in the file system in the inbound directory. - When
StatementJobis executed it movesStatements from the inbound to work in progress directory. - Then
Statements from the work in progress directory are uploded to some storage. - When the upload is succesfull, the
Statements are moved to the archive statement folder. Statements which failed to upload are moved to the error statement folder. Logis used to collect information about the job that will be emailed to whomever needs to know the status. This is very primitive at this point.