String parentObjectId = vars.getStringParameter("parentObjectId");
if (parentObjectId == null || parentObjectId.equals("")) {
boolean adminMode = OBContext.getOBContext().setInAdministratorMode(true);
try {
Table table = OBDal.getInstance().get(Table.class, vars.getStringParameter("inpTableId"));
List<Column> cols = table.getADColumnList();
String keyCol = "";
for (Column col : cols) {
if (col.isKeyColumn()) {
keyCol = col.getDBColumnName();
break;
}
}
parentObjectId = vars.getStringParameter("inp" + Sqlc.TransformaNombreColumna(keyCol));
} finally {
OBContext.getOBContext().setInAdministratorMode(adminMode);
}
}
if (vars.commandIn("DEFAULT")) {
printPageFrame(response, vars, imageID, tableId, columnName, parentObjectId);
} else if (vars.commandIn("SAVE")) {
final FileItem fi = vars.getMultiFile("inpFile");
byte[] bytea = fi.get();
// Using DAL to write the image data to the database
Image image;
if (imageID == null || imageID.equals("")) {
image = OBProvider.getInstance().get(Image.class);
image.setBindaryData(bytea);
image.setActive(true);
image.setName("Image");
OBDal.getInstance().save(image);
OBDal.getInstance().flush();
} else {
image = OBDal.getInstance().get(Image.class, imageID);
image.setActive(true);
image.setBindaryData(bytea);
OBDal.getInstance().flush();
}
response.setContentType("text/html; charset=UTF-8");
PrintWriter writer = response.getWriter();
writeRedirect(writer, image.getId(), columnName);
} else if (vars.commandIn("DELETE")) {
if (imageID != null && !imageID.equals("")) {
boolean adminMode = OBContext.getOBContext().setInAdministratorMode(true);
try {
Image image = OBDal.getInstance().get(Image.class, imageID);
Table table = OBDal.getInstance().get(Table.class, tableId);
String propertyName = ModelProvider.getInstance().getEntityByTableName(
table.getDBTableName()).getPropertyByColumnName(columnName).getName();
DataPackage dpackage = table.getDataPackage();
try {
Class tableClass = Class.forName(dpackage.getJavaPackage() + "."
+ table.getJavaClassName());
BaseOBObject parentObject = (BaseOBObject) OBDal.getInstance().get(tableClass,
parentObjectId);
parentObject.set(propertyName, null);
OBDal.getInstance().flush();
OBDal.getInstance().remove(image);