From 7f95e4bf2081211e5d34b02ba7f6e63db9cfb6a9 Mon Sep 17 00:00:00 2001 From: Chandrika Date: Mon, 16 Mar 2015 19:16:42 -0400 Subject: [PATCH] MIFOSX-1808 Group hierarchy updation issue corrected.Center summary is now getting updated properly. --- .../portfolio/group/domain/Group.java | 31 +++++++++++++------ ...WritePlatformServiceJpaRepositoryImpl.java | 15 ++++----- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/domain/Group.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/domain/Group.java index 23810ac2d..dd1a170e0 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/domain/Group.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/domain/Group.java @@ -348,16 +348,24 @@ public final class Group extends AbstractPersistable { return this.clientMembers.contains(client); } - public void generateHierarchy() { - if (this.parent != null) { - this.hierarchy = this.parent.hierarchyOf(getId()); - } else { - this.hierarchy = "." + getId() + "."; - } - } - + public void generateHierarchy() { + if (this.parent != null) { + this.hierarchy = this.parent.hierarchyOf(getId()); + } else { + this.hierarchy = "." + getId() + "."; + for (Group group : this.groupMembers) { + group.setParent(this); + group.generateHierarchy(); + } + } + } + + public void resetHierarchy() { + this.hierarchy = "." + this.getId(); + } + private String hierarchyOf(final Long id) { - return this.hierarchy + id.toString() + "."; + return this.hierarchy + id.toString() + "."; } public boolean isOfficeIdentifiedBy(final Long officeId) { @@ -375,7 +383,7 @@ public final class Group extends AbstractPersistable { } return staffId; } - + private void addChild(final Group group) { this.groupMembers.add(group); } @@ -522,6 +530,8 @@ public final class Group extends AbstractPersistable { this.groupMembers.add(group); differences.add(group.getId().toString()); + group.setParent(this); + group.generateHierarchy(); } return differences; @@ -534,6 +544,7 @@ public final class Group extends AbstractPersistable { if (hasGroupAsMember(group)) { this.groupMembers.remove(group); differences.add(group.getId().toString()); + group.resetHierarchy(); } else { throw new GroupNotExistsInCenterException(group.getId(), getId()); } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupingTypesWritePlatformServiceJpaRepositoryImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupingTypesWritePlatformServiceJpaRepositoryImpl.java index 1667f9772..f0f5187c0 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupingTypesWritePlatformServiceJpaRepositoryImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupingTypesWritePlatformServiceJpaRepositoryImpl.java @@ -131,7 +131,7 @@ public class GroupingTypesWritePlatformServiceJpaRepositoryImpl implements Group parentGroup = this.groupRepository.findOneWithNotFoundDetection(centerId); officeId = parentGroup.officeId(); } - + final Office groupOffice = this.officeRepository.findOne(officeId); if (groupOffice == null) { throw new OfficeNotFoundException(officeId); } @@ -184,9 +184,10 @@ public class GroupingTypesWritePlatformServiceJpaRepositoryImpl implements Group } // pre-save to generate id for use in group hierarchy - this.groupRepository.save(newGroup); + this.groupRepository.save(newGroup); - newGroup.generateHierarchy(); + /* Generate hierarchy for a new center/group and all the child groups if they exist */ + newGroup.generateHierarchy(); this.groupRepository.saveAndFlush(newGroup); newGroup.captureStaffHistoryDuringCenterCreation(staff, activationDate); @@ -414,7 +415,7 @@ public class GroupingTypesWritePlatformServiceJpaRepositoryImpl implements Group groupForUpdate.unassignStaff(); } this.groupRepository.saveAndFlush(groupForUpdate); - + actualChanges.put(staffIdParamName, null); return new CommandProcessingResultBuilder() // @@ -756,17 +757,17 @@ public class GroupingTypesWritePlatformServiceJpaRepositoryImpl implements Group public CommandProcessingResult associateGroupsToCenter(final Long centerId, final JsonCommand command) { this.fromApiJsonDeserializer.validateForAssociateGroups(command.json()); - final Group centerForUpdate = this.groupRepository.findOneWithNotFoundDetection(centerId); final Set groupMembers = assembleSetOfChildGroups(centerForUpdate.officeId(), command); checkGroupMembersMeetingSyncWithCenterMeeting(centerId, groupMembers); - + final Map actualChanges = new HashMap<>(); + final List changes = centerForUpdate.associateGroups(groupMembers); if (!changes.isEmpty()) { actualChanges.put(GroupingTypesApiConstants.groupMembersParamName, changes); } - + this.groupRepository.saveAndFlush(centerForUpdate); return new CommandProcessingResultBuilder() //