- Query getGroupQuery = em.createQuery("SELECT g FROM GroupEntity g WHERE g.groupId=:groupId AND g.deleted=:deleted");
- getGroupQuery.setParameter("groupId", group.getId());
- getGroupQuery.setParameter("deleted", false);
- List<?> getGroupQueryList;
- try{
- getGroupQueryList = getGroupQuery.getResultList();
- } catch(Exception e){
- //TODO:EELF Cleanup - Remove logger
- //logger.error("Caught Exception on getGroupQuery.getResultList()",e);
- PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Caught Exception on getGroupQuery.getResultList()");
- throw new PersistenceException("Query failed trying to get group "+group.getId()+" for editing");
- }
- if(getGroupQueryList.size() < 1){
- //TODO:EELF Cleanup - Remove logger
- //logger.error("The group cannot be found to update with id "+group.getId());
- PolicyLogger.error("The group cannot be found to update with id "+group.getId());
- throw new PersistenceException("The group cannot be found to update with id "+group.getId());
- } else if(getGroupQueryList.size() > 1){
- //TODO:EELF Cleanup - Remove logger
- //logger.error("Somehow, more than one group with the same id "+group.getId()+" and deleted status were found in the database");
- PolicyLogger.error("Somehow, more than one group with the same id "+group.getId()+" and deleted status were found in the database");
- throw new PersistenceException("Somehow, more than one group with the same id "+group.getId()+" and deleted status were found in the database");
- }
- //em.getTransaction().begin();
- GroupEntity groupToUpdate = (GroupEntity)getGroupQueryList.get(0);
- if(!stringEquals(groupToUpdate.getModifiedBy(), username)){
- groupToUpdate.setModifiedBy(username);
- }
- if(group.getDescription() != null && !stringEquals(group.getDescription(),groupToUpdate.getDescription())){
- groupToUpdate.setDescription(group.getDescription());
- }
- //let's find out what policies have been deleted
- StdPDPGroup oldGroup = null;
- try {
- oldGroup = (StdPDPGroup) papEngine.getGroup(group.getId());
- } catch (PAPException e1) {
- //TODO:EELF Cleanup - Remove logger
- //logger.error("We cannot get the group from the papEngine to delete policies",e1);
- PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e1, "PolicyDBDao", "We cannot get the group from the papEngine to delete policies");
- }
- if(oldGroup == null){
- //TODO:EELF Cleanup - Remove logger
- //logger.error("We cannot get the group from the papEngine to delete policies");
- PolicyLogger.error("We cannot get the group from the papEngine to delete policies");
- } else {
-
- Set<String> newPolicySet = new HashSet<String>(group.getPolicies().size());
- //a multiple of n runtime is faster than n^2, so I am using a hashset to do the comparison
- for(PDPPolicy pol: group.getPolicies()){
- newPolicySet.add(pol.getId());
- }
- for(PDPPolicy pol : oldGroup.getPolicies()){
- //should be fast since getPolicies uses a HashSet in StdPDPGroup
- if(!newPolicySet.contains(pol.getId())){
- String[] scopeAndName = getNameScopeAndVersionFromPdpPolicy(pol.getId());
- PolicyEntity policyToDelete;
- try{
- policyToDelete = getPolicy(scopeAndName[0],scopeAndName[1]);
- }catch(Exception e){
- //TODO:EELF Cleanup - Remove logger
- //logger.error("Could not get policy to remove: "+pol.getId(),e);
- PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "PolicyDBDao", "Could not get policy to remove: "+pol.getId());
- throw new PersistenceException("Could not get policy to remove: "+pol.getId());
- }
- groupToUpdate.getPolicies().remove(policyToDelete);
-
- }
- }
- }
- if(group.getName() != null && !stringEquals(group.getName(),groupToUpdate.getgroupName())){
- //we need to check if the new id exists in the database
- String newGroupId = createNewPDPGroupId(group.getName());
- Query checkGroupQuery = em.createQuery("SELECT g FROM GroupEntity g WHERE g.groupId=:groupId AND g.deleted=:deleted");
- checkGroupQuery.setParameter("groupId", newGroupId);
- checkGroupQuery.setParameter("deleted", false);
- List<?> checkGroupQueryList;