Session aSession = null;
Transaction tx = null;
try {
aSession = getSession();
tx = aSession.beginTransaction();
SbiFunctions hibFunct = (SbiFunctions) aSession.load(
SbiFunctions.class, aLowFunctionality.getId());
// delete all roles functionality
updateSbiCommonInfo4Update(hibFunct);
Set oldRoles = hibFunct.getSbiFuncRoles();
Iterator iterOldRoles = oldRoles.iterator();
while (iterOldRoles.hasNext()) {
SbiFuncRole role = (SbiFuncRole) iterOldRoles.next();
aSession.delete(role);
}
// save roles functionality
Set functRoleToSave = new HashSet();
functRoleToSave.addAll(saveRolesFunctionality(aSession, hibFunct,
aLowFunctionality, SpagoBIConstants.PERMISSION_ON_FOLDER_TO_DEVELOP));
functRoleToSave.addAll(saveRolesFunctionality(aSession, hibFunct,
aLowFunctionality, SpagoBIConstants.PERMISSION_ON_FOLDER_TO_TEST));
functRoleToSave.addAll(saveRolesFunctionality(aSession, hibFunct,
aLowFunctionality, SpagoBIConstants.PERMISSION_ON_FOLDER_TO_EXECUTE));
functRoleToSave.addAll(saveRolesFunctionality(aSession, hibFunct,
aLowFunctionality, SpagoBIConstants.PERMISSION_ON_FOLDER_TO_CREATE));
// set new roles into sbiFunctions
hibFunct.setSbiFuncRoles(functRoleToSave);
// set new data
hibFunct.setDescr(aLowFunctionality.getDescription());
Criterion domainCdCriterrion = Expression.eq("valueCd",
aLowFunctionality.getCodType());
Criteria criteria = aSession.createCriteria(SbiDomains.class);
criteria.add(domainCdCriterrion);
SbiDomains functTypeDomain = (SbiDomains) criteria.uniqueResult();
if (functTypeDomain == null){
logger.error("The Domain with value_cd="+aLowFunctionality.getCodType()+" does not exist.");
throw new EMFUserError(EMFErrorSeverity.ERROR, 1037);
}
hibFunct.setFunctType(functTypeDomain);
hibFunct.setFunctTypeCd(aLowFunctionality.getCodType());
hibFunct.setName(aLowFunctionality.getName());
Integer parentId = aLowFunctionality.getParentId();
Criteria parentCriteria = aSession.createCriteria(SbiFunctions.class);
Criterion parentCriterion = Expression.eq("functId", parentId);
parentCriteria.add(parentCriterion);
SbiFunctions hibParentFunct = (SbiFunctions) parentCriteria.uniqueResult();
if (hibParentFunct == null){
logger.error("The parent Functionality with id = " + parentId + " does not exist.");
throw new EMFUserError(EMFErrorSeverity.ERROR, 1037);
}
hibFunct.setParentFunct(hibParentFunct);
// manages code and path
String previousCode = hibFunct.getCode();
String previousPath = hibFunct.getPath();
String newCode = aLowFunctionality.getCode();
String newPath = aLowFunctionality.getPath();
if (!previousCode.equals(newCode) || !previousPath.equals(newPath)) {
// the code or the path was changed, so the path of the current folder and of its child folders
// must be changed
// the condition !previousPath.equals(newPath) was added for the following reason:
// till SpagoBI 1.9.3 a folder may have a path different from parentPath + "/" + code,
// with this condition those cases are considered and corrected.
// changes the code and path of the current folder
hibFunct.setCode(newCode);
hibFunct.setPath(newPath);
// loads sub folders and changes their path
Criteria subFoldersCriteria = aSession.createCriteria(SbiFunctions.class);
Criterion subFoldersCriterion = Expression.like("path", previousPath + "/", MatchMode.START);
subFoldersCriteria.add(subFoldersCriterion);
List hibList = subFoldersCriteria.list();
Iterator it = hibList.iterator();
while (it.hasNext()) {
SbiFunctions aSbiFunctions = (SbiFunctions) it.next();
String oldPath = aSbiFunctions.getPath();
String unchanged = oldPath.substring(previousPath.length());
aSbiFunctions.setPath(newPath + unchanged);
}
}
// commit all changes
tx.commit();