Transaction tx = null;
try {
aSession = getSession();
tx = aSession.beginTransaction();
// load object
SbiObjects hibBIObject = (SbiObjects) aSession.load(SbiObjects.class, obj.getId());
// erase object from functionalities
Set hibObjFuncs = hibBIObject.getSbiObjFuncs();
Iterator itObjFunc = hibObjFuncs.iterator();
while (itObjFunc.hasNext()) {
SbiObjFunc aSbiObjFunc = (SbiObjFunc) itObjFunc.next();
if (idFunct == null || aSbiObjFunc.getId().getSbiFunctions().getFunctId().intValue() == idFunct.intValue()) {
logger.debug("Deleting object [" + obj.getName() + "] from folder [" + aSbiObjFunc.getId().getSbiFunctions().getPath() + "]");
aSession.delete(aSbiObjFunc);
}
}
aSession.flush();
// reload object
aSession.refresh(hibBIObject);
// if the object is no more referenced in any folder, erases it from sbi_obejcts table
hibObjFuncs = hibBIObject.getSbiObjFuncs();
if (hibObjFuncs == null || hibObjFuncs.size() == 0) {
logger.debug("The object [" + obj.getName() + "] is no more referenced by any functionality. It will be completely deleted from db.");
// delete templates
String hql = "from SbiObjTemplates sot where sot.sbiObject.biobjId="+obj.getId();
Query query = aSession.createQuery(hql);
List templs = query.list();
Iterator iterTempls = templs.iterator();
while(iterTempls.hasNext()) {
SbiObjTemplates hibObjTemp = (SbiObjTemplates)iterTempls.next();
SbiBinContents hibBinCont = hibObjTemp.getSbiBinContents();
aSession.delete(hibObjTemp);
aSession.delete(hibBinCont);
}
//delete subobjects eventually associated
ISubObjectDAO subobjDAO = DAOFactory.getSubObjectDAO();
List subobjects = subobjDAO.getSubObjects(obj.getId());
for (int i=0; i < subobjects.size(); i++){
SubObject s = (SubObject) subobjects.get(i);
//subobjDAO.deleteSubObject(s.getId());
subobjDAO.deleteSubObjectSameConnection(s.getId(), aSession);
}
//delete viewpoints eventually associated
List viewpoints = new ArrayList();
IViewpointDAO biVPDAO = DAOFactory.getViewpointDAO();
viewpoints = biVPDAO.loadAllViewpointsByObjID(obj.getId());
for (int i=0; i<viewpoints.size(); i++){
Viewpoint vp =(Viewpoint)viewpoints.get(i);
biVPDAO.eraseViewpoint(vp.getVpId());
}
//delete snapshots eventually associated
ISnapshotDAO snapshotsDAO = DAOFactory.getSnapshotDAO();
List snapshots = snapshotsDAO.getSnapshots(obj.getId());
for (int i=0; i < snapshots.size(); i++){
Snapshot aSnapshots = (Snapshot) snapshots.get(i);
snapshotsDAO.deleteSnapshot(aSnapshots.getId());
}
//delete notes eventually associated
IObjNoteDAO objNoteDAO = DAOFactory.getObjNoteDAO();
objNoteDAO.eraseNotes(obj.getId());
//delete metadata eventually associated
List metadata = DAOFactory.getObjMetadataDAO().loadAllObjMetadata();
IObjMetacontentDAO objMetaContentDAO = DAOFactory.getObjMetacontentDAO();
if (metadata != null && !metadata.isEmpty()) {
Iterator it = metadata.iterator();
while (it.hasNext()) {
ObjMetadata objMetadata = (ObjMetadata) it.next();
ObjMetacontent objMetacontent = (ObjMetacontent) DAOFactory.getObjMetacontentDAO().loadObjMetacontent(objMetadata.getObjMetaId(), obj.getId(), null);
if(objMetacontent!=null){
objMetaContentDAO.eraseObjMetadata(objMetacontent);
}
}
}
// delete parameters associated
Set objPars = hibBIObject.getSbiObjPars();
Iterator itObjPar = objPars.iterator();
BIObjectParameterDAOHibImpl objParDAO = new BIObjectParameterDAOHibImpl();
while (itObjPar.hasNext()) {
SbiObjPar aSbiObjPar = (SbiObjPar) itObjPar.next();
BIObjectParameter aBIObjectParameter = new BIObjectParameter();