I am looking for any refactoring tips to improve code readability. Does it feel like procedural programming? If so, how can I improve it?
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
FileStatements
which represents files, calledStatement
, stored in the file system in the inbound directory. - When
StatementJob
is executed it movesStatement
s from the inbound to work in progress directory. - Then
Statement
s from the work in progress directory are uploded to some storage. - When the upload is succesfull, the
Statement
s are moved to the archive statement folder. Statements which failed to upload are moved to the error statement folder. Log
is 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.
FileStatements
for example and got no results. We can't evaluate your code without knowing what your code does. \$\endgroup\$FileStatements
and other classes, but I was hoping that it is clear what it does by looking at the methods it has and what they return. I could upload the entire project to github with all the classes but I think no one would be interested in such extensive code review. I might be wrong, let me know. \$\endgroup\$