use of edu.cornell.kfs.sys.batch.CuBatchInputFileType in project cu-kfs by CU-CommunityApps.
the class CuBatchInputFileServiceImpl method save.
@Override
public String save(Person user, BatchInputFileType batchInputFileType, String fileUserIdentifier, InputStream fileContents, Object parsedObject) throws FileStorageException {
if (user == null || batchInputFileType == null || fileContents == null) {
LOG.error(INVALID_ARGUEMENT);
throw new IllegalArgumentException(INVALID_ARGUEMENT);
}
if (!isFileUserIdentifierProperlyFormatted(fileUserIdentifier)) {
LOG.error(NOT_PROPERLY_FORMATTED + fileUserIdentifier);
throw new IllegalArgumentException(NOT_PROPERLY_FORMATTED + fileUserIdentifier);
}
// defer to batch input type to add any security or other needed information to the file name
String saveFileName = batchInputFileType.getDirectoryPath() + "/" + batchInputFileType.getFileName(user.getPrincipalName(), parsedObject, fileUserIdentifier);
if (!StringUtils.isBlank(batchInputFileType.getFileExtension())) {
saveFileName += "." + batchInputFileType.getFileExtension();
}
// construct the file object and check for existence
File fileToSave = new File(saveFileName);
if (fileToSave.exists()) {
LOG.error("cannot store file, name already exists " + saveFileName);
throw new FileStorageException("Cannot store file because the name " + saveFileName + " already exists on the file system.");
}
try {
FileOutputStream fos = new FileOutputStream(fileToSave);
while (fileContents.available() > 0) {
fos.write(fileContents.read());
}
fos.flush();
fos.close();
// isDoneFileRequired function.
if (!(batchInputFileType instanceof CuBatchInputFileType) || ((CuBatchInputFileType) batchInputFileType).isDoneFileRequired()) {
createDoneFile(fileToSave, batchInputFileType);
}
// CU Mod
batchInputFileType.process(saveFileName, parsedObject);
} catch (IOException e) {
LOG.error("unable to save contents to file " + saveFileName, e);
throw new RuntimeException("errors encountered while writing file " + saveFileName, e);
}
return saveFileName;
}
Aggregations