Transaction tx = null;
try {
aSession = getSession();
tx = aSession.beginTransaction();
SbiOrgUnitGrant hibGrant = (SbiOrgUnitGrant) aSession.load(SbiOrgUnitGrant.class, grant.getId());
hibGrant.setLabel(grant.getLabel());
hibGrant.setName(grant.getName());
hibGrant.setDescription(grant.getDescription());
hibGrant.setStartDate(grant.getStartDate());
hibGrant.setEndDate(grant.getEndDate());
hibGrant.setIsAvailable(grant.getIsAvailable());
// if hierarchy and/or kpi model instance have been changed, erase previous defined node grants
Integer previousHierachyId = hibGrant.getSbiOrgUnitHierarchies().getId();
Integer newHierachyId = grant.getHierarchy().getId();
Integer previousKpiModelInstId = hibGrant.getSbiKpiModelInst().getKpiModelInst();
Integer newKpiModelInstId = grant.getModelInstance().getId();
if (previousHierachyId.intValue() != newHierachyId.intValue()
|| previousKpiModelInstId.intValue() != newKpiModelInstId.intValue()) {
String hql = "delete from SbiOrgUnitGrantNodes s where s.id.grantId = ?";
Query query = aSession.createQuery(hql);
query.setInteger(0, hibGrant.getId());
query.executeUpdate();
}
// update hierarchy
if (previousHierachyId.intValue() != newHierachyId.intValue()) {
Query query = aSession.createQuery(" from SbiOrgUnitHierarchies s where s.id = ? ");
query.setInteger(0, newHierachyId);
SbiOrgUnitHierarchies h = (SbiOrgUnitHierarchies) query.uniqueResult();
hibGrant.setSbiOrgUnitHierarchies(h);
}
// update kpi model instance
if (previousKpiModelInstId.intValue() != newKpiModelInstId.intValue()) {
Query query = aSession.createQuery(" from SbiKpiModelInst s where s.kpiModelInst = ? ");
query.setInteger(0, newKpiModelInstId);
SbiKpiModelInst s = (SbiKpiModelInst) query.uniqueResult();
hibGrant.setSbiKpiModelInst(s);
}
updateSbiCommonInfo4Update(hibGrant);
aSession.save(hibGrant);
tx.commit();