// See if a file of that name is already attached
String convertedFileName = _parent.convertFileNameForAttaching(file.getName());
if (_content != null) {
if (_content.getFiles().containsKey(convertedFileName)) {
// B0000471E
throw new WGIllegalArgumentException("A file with the same name '" + convertedFileName + "' is already attached on this document, please remove it first.");
}
}
else {
FileContainer cont = (FileContainer) _entity;
if (cont.getFiles().containsKey(convertedFileName)) {
// B0000471E
throw new WGIllegalArgumentException("A file with the same name '" + convertedFileName + "' is already present in this file container, please remove it first.");
}
}
// Load the data
InputStream in = new BufferedInputStream(new FileInputStream(file));
Blob blob = Hibernate.createBlob(in);
// Create entity and attach to parent entity
// TB
if (_content != null) {
ContentFile fileAtt = new ContentFile();
fileAtt.setParentcontent(_content);
fileAtt.setName(convertedFileName);
fileAtt.setData(blob);
ContentFile oldFile = (ContentFile) _content.getFiles().put(convertedFileName, fileAtt);
if (oldFile != null && oldFile != fileAtt) {
_parent.getSession().evict(oldFile);
}
}
else {
FileContainer cont = (FileContainer) _entity;
ContainerFile fileAtt = new ContainerFile();
fileAtt.setParentcontainer(cont);
fileAtt.setName(convertedFileName);
fileAtt.setData(blob);
ContainerFile oldFile = (ContainerFile) cont.getFiles().put(convertedFileName, fileAtt);
if (oldFile != null && oldFile != fileAtt) {
_parent.getSession().evict(oldFile);
}
}
}
catch (FileNotFoundException e) {
throw new WGIllegalArgumentException("Error attaching file - not found.", e);
}
catch (IOException e) {
throw new WGBackendException("Error attaching file.", e);
}
}