diff --git a/mifosng-db/patches/22-group_name_modified_to_not_null.sql b/mifosng-db/patches/22-group_name_modified_to_not_null.sql new file mode 100644 index 000000000..dcf9185b2 --- /dev/null +++ b/mifosng-db/patches/22-group_name_modified_to_not_null.sql @@ -0,0 +1,5 @@ +ALTER TABLE m_group MODIFY name VARCHAR(100) NOT NULL; +ALTER TABLE m_group DROP INDEX name; +ALTER TABLE m_group DROP INDEX external_id; +ALTER TABLE m_group ADD UNIQUE INDEX name (name, level_Id); +ALTER TABLE m_group ADD UNIQUE INDEX external_id (external_id, level_Id); \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/api/GroupsApiResource.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/api/GroupsApiResource.java index a99f56e38..c3b4b43fc 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/api/GroupsApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/api/GroupsApiResource.java @@ -45,9 +45,13 @@ import org.springframework.stereotype.Component; @Scope("singleton") public class GroupsApiResource { - private static final Set GROUP_DATA_PARAMETERS = new HashSet(Arrays.asList("id", "officeId", "name", "externalId", - "clientMembers", "allowedClients", "allowedOffices", "allowedParentGroups", "groupLevel", "allowedStaffs", "childGroups")); - + /* + * GROUP_DATA_PARAMETERS is used by ToApiJsonSerializer, make sure E's properties and E_PARAMETERS are in same + */ + private static final Set GROUP_DATA_PARAMETERS = new HashSet(Arrays.asList("id", "name", "externalId", "officeId", + "officeName", "staffId", "staffName", "parentId", "parentName", "hierarchy", "groupSummaryData", "groupLevelData", + "clientMembers", "allowedClients", "allowedOffices", "allowedParentGroups", "allowedStaffs", "childGroups")); + private final PlatformSecurityContext context; private final GroupReadPlatformService groupReadPlatformService; private final ToApiJsonSerializer toApiJsonSerializer; diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/data/GroupData.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/data/GroupData.java index a0c1e3adc..4aaabe103 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/data/GroupData.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/data/GroupData.java @@ -23,12 +23,11 @@ public class GroupData { private final String officeName; private final Long staffId; private final String staffName; - private final Long groupLevel; private final Long parentId; private final String parentName; private final String hierarchy; @SuppressWarnings("unused") - private final GroupSummaryData groupSummaryData; + private final GroupSummary groupSummaryData; @SuppressWarnings("unused") private final GroupLevelData groupLevelData; private final Collection clientMembers; @@ -37,21 +36,19 @@ public class GroupData { @SuppressWarnings("unused") private final Collection allowedOffices; @SuppressWarnings("unused") - private final Collection allowedParentGroups; + private final Collection allowedParentGroups; @SuppressWarnings("unused") private final Collection allowedStaffs; @SuppressWarnings("unused") - private final Collection childGroups; + private final Collection childGroups; public GroupData(final Long id, final Long officeId, final String officeName, final String name, final String externalId, - final Long groupLevel, final Long parentId, final String parentName, final Long staffId, final String staffName, - final String hierarchy) { + final Long parentId, final String parentName, final Long staffId, final String staffName, final String hierarchy) { this.id = id; this.officeId = officeId; this.officeName = officeName; this.name = name; this.externalId = externalId; - this.groupLevel = groupLevel; this.parentId = parentId; this.parentName = parentName; this.staffId = staffId; @@ -68,15 +65,14 @@ public class GroupData { } public GroupData(final GroupData group, final Collection clientMembers, final Collection allowedClients, - final Collection allowedOffices, final Collection allowedParentGroups, - final GroupLevelData groupLevelData, final Collection allowedStaffs, final Collection childGroups, - final GroupSummaryData groupSummaryData) { + final Collection allowedOffices, final Collection allowedParentGroups, + final GroupLevelData groupLevelData, final Collection allowedStaffs, final Collection childGroups, + final GroupSummary groupSummaryData) { this.id = group.getId(); this.officeId = group.getOfficeId(); this.officeName = group.getOfficeName(); this.name = group.getName(); this.externalId = group.getExternalId(); - this.groupLevel = group.getGroupLevel(); this.parentId = group.getParentId(); this.parentName = group.getParentName(); this.staffId = group.getStaffId(); @@ -94,7 +90,7 @@ public class GroupData { } public GroupData(final Long officeId, final Collection allowedClients, final Collection allowedOffices, - final Collection allowedParentGroups, final GroupLevelData groupLevelData, + final Collection allowedParentGroups, final GroupLevelData groupLevelData, final Collection allowedStaffs) { this.id = null; this.officeId = officeId; @@ -102,7 +98,6 @@ public class GroupData { this.name = null; this.externalId = null; this.clientMembers = null; - this.groupLevel = null; this.parentId = null; this.parentName = null; this.staffId = null; @@ -137,10 +132,6 @@ public class GroupData { return this.officeName; } - public Long getGroupLevel() { - return this.groupLevel; - } - public Long getParentId() { return this.parentId; } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/data/GroupLookupData.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/data/GroupLookup.java similarity index 85% rename from mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/data/GroupLookupData.java rename to mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/data/GroupLookup.java index 557a7415c..eb2793e99 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/data/GroupLookupData.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/data/GroupLookup.java @@ -8,12 +8,12 @@ package org.mifosplatform.portfolio.group.data; /** * Immutable data object representing groups. */ -public class GroupLookupData { +public class GroupLookup { private final Long id; private final String name; - public GroupLookupData(final Long id, final String name) { + public GroupLookup(final Long id, final String name) { this.id = id; this.name = name; } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/data/GroupSummaryData.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/data/GroupSummary.java similarity index 87% rename from mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/data/GroupSummaryData.java rename to mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/data/GroupSummary.java index 4d700a9cc..fc4993160 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/data/GroupSummaryData.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/data/GroupSummary.java @@ -9,14 +9,14 @@ import java.util.Collection; import org.mifosplatform.organisation.monetary.data.MoneyData; -public class GroupSummaryData { +public class GroupSummary { private final Long totalActiveClients; private final Long totalChildGroups; private final Collection totalLoanPortfolio; private final Collection totalSavings; - public GroupSummaryData(final Long totalActiveClients, final Long totalChildGroups, final Collection totalLoanPortfolio, + public GroupSummary(final Long totalActiveClients, final Long totalChildGroups, final Collection totalLoanPortfolio, final Collection totalSavings) { this.totalActiveClients = totalActiveClients; this.totalChildGroups = totalChildGroups; diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/serialization/GroupCommandFromApiJsonDeserializer.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/serialization/GroupCommandFromApiJsonDeserializer.java index eaeee4d3b..b2ec935c8 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/serialization/GroupCommandFromApiJsonDeserializer.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/serialization/GroupCommandFromApiJsonDeserializer.java @@ -80,6 +80,13 @@ public final class GroupCommandFromApiJsonDeserializer extends AbstractFromApiJs final List dataValidationErrors = new ArrayList(); final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("group"); + + /* + * Name is a mandatory field and not exceeding length 100 character + */ + final String nameParameterName = "name"; + final String name = this.fromApiJsonHelper.extractStringNamed(nameParameterName, element); + baseDataValidator.reset().parameter(nameParameterName).value(name).notNull().notExceedingLengthOf(100); final String externalIdParameterName = "externalId"; if (this.fromApiJsonHelper.parameterExists(externalIdParameterName, element)) { @@ -87,22 +94,24 @@ public final class GroupCommandFromApiJsonDeserializer extends AbstractFromApiJs baseDataValidator.reset().parameter(externalIdParameterName).value(externalId).ignoreIfNull().notExceedingLengthOf(100); } - final String officeIdParameterName = "officeId"; - if (this.fromApiJsonHelper.parameterExists(officeIdParameterName, element)) { - final Long officeId = this.fromApiJsonHelper.extractLongNamed(officeIdParameterName, element); - baseDataValidator.reset().parameter(officeIdParameterName).value(officeId).notNull().integerGreaterThanZero(); - } + /* + * Either OfficeId or ParentId is mandatory to have value + */ final String parentIdParameterName = "parentId"; if (this.fromApiJsonHelper.parameterExists(parentIdParameterName, element)) { final Long parentId = this.fromApiJsonHelper.extractLongNamed(parentIdParameterName, element); baseDataValidator.reset().parameter(parentIdParameterName).value(parentId).notNull().integerGreaterThanZero(); + }else{ + final String officeIdParameterName = "officeId"; + final Long officeId = this.fromApiJsonHelper.extractLongNamed(officeIdParameterName, element); + baseDataValidator.reset().parameter(officeIdParameterName).value(officeId).notNull().integerGreaterThanZero(); } final String staffIdParameterName = "staffId"; if (this.fromApiJsonHelper.parameterExists(staffIdParameterName, element)) { final Long staffId = this.fromApiJsonHelper.extractLongNamed(staffIdParameterName, element); - baseDataValidator.reset().parameter(staffIdParameterName).value(staffId).notNull().integerGreaterThanZero(); + baseDataValidator.reset().parameter(staffIdParameterName).value(staffId).ignoreIfNull().integerGreaterThanZero(); } if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", @@ -137,13 +146,13 @@ public final class GroupCommandFromApiJsonDeserializer extends AbstractFromApiJs final String parentIdParameterName = "parentId"; if (this.fromApiJsonHelper.parameterExists(parentIdParameterName, element)) { final Long parentId = this.fromApiJsonHelper.extractLongNamed(parentIdParameterName, element); - baseDataValidator.reset().parameter(parentIdParameterName).value(parentId).notNull().integerGreaterThanZero(); + baseDataValidator.reset().parameter(parentIdParameterName).value(parentId).ignoreIfNull().integerGreaterThanZero(); } final String staffIdParameterName = "staffId"; if (this.fromApiJsonHelper.parameterExists(staffIdParameterName, element)) { final Long staffId = this.fromApiJsonHelper.extractLongNamed(staffIdParameterName, element); - baseDataValidator.reset().parameter(staffIdParameterName).value(staffId).notNull().integerGreaterThanZero(); + baseDataValidator.reset().parameter(staffIdParameterName).value(staffId).ignoreIfNull().integerGreaterThanZero(); } if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", @@ -155,9 +164,6 @@ public final class GroupCommandFromApiJsonDeserializer extends AbstractFromApiJs final Type typeOfMap = new TypeToken>() {}.getType(); - /* - * Office updated is not supported - */ final Set supportedParametersUnassignStaff = new HashSet(Arrays.asList("staffId")); this.fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, supportedParametersUnassignStaff); @@ -168,10 +174,8 @@ public final class GroupCommandFromApiJsonDeserializer extends AbstractFromApiJs final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("group"); final String staffIdParameterName = "staffId"; - if (this.fromApiJsonHelper.parameterExists(staffIdParameterName, element)) { - final Long staffId = this.fromApiJsonHelper.extractLongNamed(staffIdParameterName, element); - baseDataValidator.reset().parameter(staffIdParameterName).value(staffId).notNull().integerGreaterThanZero(); - } + final Long staffId = this.fromApiJsonHelper.extractLongNamed(staffIdParameterName, element); + baseDataValidator.reset().parameter(staffIdParameterName).value(staffId).notNull().integerGreaterThanZero(); if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.", dataValidationErrors); } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupLevelReadPlatformServiceImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupLevelReadPlatformServiceImpl.java index 8cb88b206..f1ef9d3da 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupLevelReadPlatformServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupLevelReadPlatformServiceImpl.java @@ -9,6 +9,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.Collection; +import org.mifosplatform.infrastructure.core.domain.JdbcSupport; import org.mifosplatform.infrastructure.core.service.TenantAwareRoutingDataSource; import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext; import org.mifosplatform.portfolio.group.data.GroupLevelData; @@ -54,9 +55,9 @@ public class GroupLevelReadPlatformServiceImpl implements GroupLevelReadPlatform final Long levelId = rs.getLong("id"); final String levelName = rs.getString("levelName"); - final Long parentLevelId = rs.getLong("parentLevelId"); + final Long parentLevelId = JdbcSupport.getLong(rs, "parentLevelId"); final String parentLevelName = rs.getString("parentName"); - final Long childLevelId = rs.getLong("childLevelId"); + final Long childLevelId = JdbcSupport.getLong(rs, "childLevelId"); final String childLevelName = rs.getString("childLevelName"); final boolean superParent = rs.getBoolean("superParent"); final boolean recursable = rs.getBoolean("recursable"); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupReadPlatformService.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupReadPlatformService.java index 2019a8ef9..4c9b057b8 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupReadPlatformService.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupReadPlatformService.java @@ -14,7 +14,7 @@ import org.mifosplatform.portfolio.group.data.GroupAccountSummaryCollectionData; import org.mifosplatform.portfolio.group.data.GroupAccountSummaryData; import org.mifosplatform.portfolio.group.data.GroupData; import org.mifosplatform.portfolio.group.data.GroupLevelData; -import org.mifosplatform.portfolio.group.data.GroupLookupData; +import org.mifosplatform.portfolio.group.data.GroupLookup; public interface GroupReadPlatformService { @@ -30,7 +30,7 @@ public interface GroupReadPlatformService { Collection retrieveGroupLoanAccountsByLoanOfficerId(Long groupId, Long loanOfficerId); - Collection retrieveAllGroupsbyOfficeIdAndLevelId(Long officeId, Long levelId); + Collection retrieveAllGroupsbyOfficeIdAndLevelId(Long officeId, Long levelId); GroupLevelData retrieveGroupLevelDetails(Long levelId); @@ -38,7 +38,7 @@ public interface GroupReadPlatformService { GroupData retrieveGroupDetails(Long groupId, boolean template); - Collection retrieveChildGroupsbyGroupId(Long groupId); + Collection retrieveChildGroupsbyGroupId(Long groupId); Long retrieveTotalNoOfChildGroups(Long groupId); @@ -49,4 +49,6 @@ public interface GroupReadPlatformService { GroupData retrieveNewChildGroupDetails(Long officeId, Long levelId, Long parentGroupId); StaffData retrieveStaffsbyId(Long staffId); + + Long getLevelIdByGroupId(final Long groupId); } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupReadPlatformServiceImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupReadPlatformServiceImpl.java index 4dee80941..a35d8d434 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupReadPlatformServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupReadPlatformServiceImpl.java @@ -28,12 +28,13 @@ import org.mifosplatform.portfolio.group.data.GroupAccountSummaryCollectionData; import org.mifosplatform.portfolio.group.data.GroupAccountSummaryData; import org.mifosplatform.portfolio.group.data.GroupData; import org.mifosplatform.portfolio.group.data.GroupLevelData; -import org.mifosplatform.portfolio.group.data.GroupLookupData; -import org.mifosplatform.portfolio.group.data.GroupSummaryData; +import org.mifosplatform.portfolio.group.data.GroupLookup; +import org.mifosplatform.portfolio.group.data.GroupSummary; import org.mifosplatform.portfolio.group.domain.Group; import org.mifosplatform.portfolio.group.domain.GroupRepository; import org.mifosplatform.portfolio.group.exception.GroupLevelNotFoundException; import org.mifosplatform.portfolio.group.exception.GroupNotFoundException; +import org.mifosplatform.useradministration.domain.AppUser; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.jdbc.core.JdbcTemplate; @@ -64,16 +65,20 @@ public class GroupReadPlatformServiceImpl implements GroupReadPlatformService { public Collection retrieveAllGroups(final String extraCriteria) { this.context.authenticatedUser(); + + AppUser currentUser = context.authenticatedUser(); + String hierarchy = currentUser.getOffice().getHierarchy(); + String hierarchySearchString = hierarchy + "%"; final GroupDataMapper rm = new GroupDataMapper(); - String sql = "select " + rm.groupSchema() + " where g.is_deleted=0"; - + String sql = "select " + rm.groupSchema() + " where o.hierarchy like ? and g.is_deleted=0 "; + if (StringUtils.isNotBlank(extraCriteria)) { sql += " and (" + extraCriteria + ")"; } - return this.jdbcTemplate.query(sql, rm, new Object[] {}); + return this.jdbcTemplate.query(sql, rm, new Object[] {hierarchySearchString}); } @Override @@ -93,7 +98,7 @@ public class GroupReadPlatformServiceImpl implements GroupReadPlatformService { } @Override - public Collection retrieveAllGroupsbyOfficeIdAndLevelId(final Long officeId, final Long levelId) { + public Collection retrieveAllGroupsbyOfficeIdAndLevelId(final Long officeId, final Long levelId) { this.context.authenticatedUser(); final GroupLookupMapper rm = new GroupLookupMapper(); @@ -101,7 +106,7 @@ public class GroupReadPlatformServiceImpl implements GroupReadPlatformService { return this.jdbcTemplate.query(sql, rm, new Object[] { officeId, levelId }); } - private GroupLookupData retrieveGroupbyId(final Long parentGroupId) { + private GroupLookup retrieveGroupbyId(final Long parentGroupId) { try { this.context.authenticatedUser(); final GroupLookupMapper rm = new GroupLookupMapper(); @@ -114,7 +119,7 @@ public class GroupReadPlatformServiceImpl implements GroupReadPlatformService { } @Override - public Collection retrieveChildGroupsbyGroupId(final Long groupId) { + public Collection retrieveChildGroupsbyGroupId(final Long groupId) { this.context.authenticatedUser(); final GroupLookupMapper rm = new GroupLookupMapper(); @@ -155,10 +160,10 @@ public class GroupReadPlatformServiceImpl implements GroupReadPlatformService { final List allowedOffices = new ArrayList(this.officeReadPlatformService.retrieveAllOfficesForDropdown()); - List allowedParentGroups = null; + List allowedParentGroups = null; if (groupLevelData.getParentLevelId() != null) { - allowedParentGroups = new ArrayList(retrieveAllGroupsbyOfficeIdAndLevelId(officeId, + allowedParentGroups = new ArrayList(retrieveAllGroupsbyOfficeIdAndLevelId(officeId, groupLevelData.getParentLevelId())); } @@ -190,7 +195,7 @@ public class GroupReadPlatformServiceImpl implements GroupReadPlatformService { final List allowedOffices = new ArrayList(); allowedOffices.add(this.officeReadPlatformService.retrieveOffice(officeId)); - final List allowedParentGroups = new ArrayList(); + final List allowedParentGroups = new ArrayList(); allowedParentGroups.add(retrieveGroupbyId(parentGroupId)); final List allowedStaffs = new ArrayList(); @@ -206,17 +211,17 @@ public class GroupReadPlatformServiceImpl implements GroupReadPlatformService { final Collection clientMembers = retrieveClientMembers(groupId); Collection availableClients = null; Collection allowedOffices = null; - Collection allowedParentGroups = null; - final GroupLevelData groupLevelData = retrieveGroupLevelDetails(group.getGroupLevel()); + Collection allowedParentGroups = null; + final GroupLevelData groupLevelData = retrieveGroupLevelDetails(getLevelIdByGroupId(groupId)); ; Collection allowedStaffs = null; - final Collection childGroups = retrieveChildGroupsbyGroupId(groupId); + final Collection childGroups = retrieveChildGroupsbyGroupId(groupId); final Long totalActiveClients = retrieveTotalClients(group.getHierarchy()); final Long totalChildGroups = retrieveTotalNoOfChildGroups(groupId); final Collection totalLoanPortfolio = retrieveGroupLoanPortfolio(group.getHierarchy()); - final GroupSummaryData groupSummaryData = new GroupSummaryData(totalActiveClients, totalChildGroups, totalLoanPortfolio, null); + final GroupSummary groupSummaryData = new GroupSummary(totalActiveClients, totalChildGroups, totalLoanPortfolio, null); group = new GroupData(group, clientMembers, availableClients, allowedOffices, allowedParentGroups, groupLevelData, allowedStaffs, childGroups, groupSummaryData); @@ -265,6 +270,15 @@ public class GroupReadPlatformServiceImpl implements GroupReadPlatformService { } } + @Override + public Long getLevelIdByGroupId(final Long groupId){ + + this.context.authenticatedUser(); + final String sqlTotalClients = "SELECT g.level_Id as levelId FROM m_group g WHERE g.id = ?"; + return this.jdbcTemplate.queryForLong(sqlTotalClients, new Object[] { groupId }); + + } + @Override public Long retrieveTotalClients(final String hierarchy) { @@ -323,10 +337,10 @@ public class GroupReadPlatformServiceImpl implements GroupReadPlatformService { private static final class GroupDataMapper implements RowMapper { public String groupSchema() { - return "g.office_id as officeId, g.level_id as groupLevel , g.parent_id as parentId , o.name as officeName," + return "g.office_id as officeId, g.parent_id as parentId , o.name as officeName," + " g.id as id, g.external_id as externalId, g.name as name , s.display_name as staffName , pg.name as" - + " parentName , g.staff_id as staffId , g.hierarchy as hierarchy from m_group g join m_office o on o.id = g.office_id left join " - + "m_staff s on s.id = g.staff_id left join m_group pg on pg.id = g.parent_id"; + + " parentName , g.staff_id as staffId , g.hierarchy as hierarchy from m_group g join m_office o on o.id = g.office_id left join" + + " m_staff s on s.id = g.staff_id left join m_group pg on pg.id = g.parent_id"; } @Override @@ -337,32 +351,31 @@ public class GroupReadPlatformServiceImpl implements GroupReadPlatformService { final String externalId = rs.getString("externalId"); final Long officeId = rs.getLong("officeId"); final String officeName = rs.getString("officeName"); - final Long groupLevel = rs.getLong("groupLevel"); final Long parentId = JdbcSupport.getLong(rs, "parentId"); final String parentName = rs.getString("parentName"); final Long staffId = JdbcSupport.getLong(rs, "staffId"); final String staffName = rs.getString("staffName"); final String hierarchy = rs.getString("hierarchy"); - return new GroupData(id, officeId, officeName, name, externalId, groupLevel, parentId, parentName, staffId, staffName, + return new GroupData(id, officeId, officeName, name, externalId, parentId, parentName, staffId, staffName, hierarchy); } } - private static final class GroupLookupMapper implements RowMapper { + private static final class GroupLookupMapper implements RowMapper { public String groupLookupSchema() { return "g.id as id, g.name as name "; } @Override - public GroupLookupData mapRow(final ResultSet rs, @SuppressWarnings("unused") final int rowNum) throws SQLException { + public GroupLookup mapRow(final ResultSet rs, @SuppressWarnings("unused") final int rowNum) throws SQLException { final Long id = rs.getLong("id"); final String name = rs.getString("name"); - return new GroupLookupData(id, name); + return new GroupLookup(id, name); } } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupWritePlatformServiceJpaRepositoryImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupWritePlatformServiceJpaRepositoryImpl.java index 37a531ca4..ed83e635d 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupWritePlatformServiceJpaRepositoryImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupWritePlatformServiceJpaRepositoryImpl.java @@ -73,6 +73,8 @@ public class GroupWritePlatformServiceJpaRepositoryImpl implements GroupWritePla @Transactional @Override public CommandProcessingResult createGroup(final JsonCommand command) { + + GroupLevel groupLevel = null; try { this.context.authenticatedUser(); @@ -84,7 +86,7 @@ public class GroupWritePlatformServiceJpaRepositoryImpl implements GroupWritePla final Long parentId = command.longValueOfParameterNamed("parentId"); Long officeId = null; - GroupLevel groupLevel = null; + Group parentGroup = null; if (parentId == null) { @@ -152,7 +154,7 @@ public class GroupWritePlatformServiceJpaRepositoryImpl implements GroupWritePla .build(); } catch (final DataIntegrityViolationException dve) { - handleGroupDataIntegrityIssues(command, dve); + handleGroupDataIntegrityIssues(command, dve , groupLevel ); return new CommandProcessingResult(Long.valueOf(-1)); } } @@ -161,6 +163,8 @@ public class GroupWritePlatformServiceJpaRepositoryImpl implements GroupWritePla @Override public CommandProcessingResult updateGroup(final Long grouptId, final JsonCommand command) { + GroupLevel groupLevel = null; + try { this.context.authenticatedUser(); @@ -197,12 +201,16 @@ public class GroupWritePlatformServiceJpaRepositoryImpl implements GroupWritePla if (command.isChangeInLongParameterNamed(staffIdParamName, presentStaffId)) { final Long newValue = command.longValueOfParameterNamed(staffIdParamName); actualChanges.put(staffIdParamName, newValue); - final Staff newStaff = this.staffRepository.findByOffice(newValue, officeId); - if (newStaff == null) { throw new StaffNotFoundException(newValue); } + + Staff newStaff = null; + if(newValue != null){ + newStaff = this.staffRepository.findByOffice(newValue, officeId); + if (newStaff == null) { throw new StaffNotFoundException(newValue); } + } groupForUpdate.setStaff(newStaff); } - final GroupLevel groupLevel = this.groupLevelRepository.findOne(groupForUpdate.getGroupLevel().getId()); + groupLevel = this.groupLevelRepository.findOne(groupForUpdate.getGroupLevel().getId()); /* * Ignoring parentId param, if group for update is super parent. @@ -216,27 +224,28 @@ public class GroupWritePlatformServiceJpaRepositoryImpl implements GroupWritePla final Long newValue = command.longValueOfParameterNamed(parentIdParamName); actualChanges.put(parentIdParamName, newValue); - final Group newParentGroup = this.groupRepository.findOne(newValue); + Group newParentGroup = null; + if (newValue != null) { + newParentGroup = this.groupRepository.findOne(newValue); + if (newParentGroup == null || newParentGroup.isDeleted()) { throw new StaffNotFoundException(newValue); } - if (newParentGroup == null || newParentGroup.isDeleted()) { throw new StaffNotFoundException(newValue); } + if (!newParentGroup.isOfficeIdentifiedBy(officeId)) { + final String errorMessage = "Group and parent group must have the same office"; + throw new InvalidOfficeException("group", "attach.to.parent.group", errorMessage); + } + /* + * If Group is not super parent then validate group + * level's parent level is same as group parent's level + * this check makes sure new group is added at immediate + * next level in hierarchy + */ - if (!newParentGroup.isOfficeIdentifiedBy(officeId)) { - final String errorMessage = "Group and parent group must have the same office"; - throw new InvalidOfficeException("group", "attach.to.parent.group", errorMessage); + if (!groupForUpdate.getGroupLevel().isIdentifiedByParentId(newParentGroup.getGroupLevel().getId())) { + final String errorMessage = "Parent group's level is not equal to child level's parent level "; + throw new InvalidGroupLevelException("add", "invalid.level", errorMessage); + } } - - /* - * If Group is not super parent then validate group level's - * parent level is same as group parent's level this check - * makes sure new group is added at immediate next level in - * hierarchy - */ - - if (!groupForUpdate.getGroupLevel().isIdentifiedByParentId(newParentGroup.getGroupLevel().getId())) { - final String errorMessage = "Parent group's level is not equal to child level's parent level "; - throw new InvalidGroupLevelException("add", "invalid.level", errorMessage); - } - + groupForUpdate.setParent(newParentGroup); } } @@ -262,7 +271,7 @@ public class GroupWritePlatformServiceJpaRepositoryImpl implements GroupWritePla .build(); } catch (final DataIntegrityViolationException dve) { - handleGroupDataIntegrityIssues(command, dve); + handleGroupDataIntegrityIssues(command, dve , groupLevel); return new CommandProcessingResult(Long.valueOf(-1)); } } @@ -378,14 +387,27 @@ public class GroupWritePlatformServiceJpaRepositoryImpl implements GroupWritePla * Guaranteed to throw an exception no matter what the data integrity issue * is. */ - private void handleGroupDataIntegrityIssues(final JsonCommand command, final DataIntegrityViolationException dve) { + private void handleGroupDataIntegrityIssues(final JsonCommand command, final DataIntegrityViolationException dve, GroupLevel groupLevel) { final Throwable realCause = dve.getMostSpecificCause(); + String errorMessageForUser = null; + String errorMessageForMachine = null; + if (realCause.getMessage().contains("external_id")) { - throw new PlatformDataIntegrityException("error.msg.group.duplicate.externalId", "Group with externalId {0} already exists", - "externalId", command.stringValueOfParameterNamed("externalId")); - } else if (realCause.getMessage().contains("name")) { throw new PlatformDataIntegrityException("error.msg.group.duplicate.name", - "Group with name {0} already exists", "name", command.stringValueOfParameterNamed("name")); } + + errorMessageForUser = groupLevel.getLevelName() + " with externalId {0} already exists"; + errorMessageForMachine = "error.msg." + groupLevel.getLevelName().toLowerCase() + ".duplicate.externalId"; + throw new PlatformDataIntegrityException(errorMessageForMachine, errorMessageForUser, "externalId", + command.stringValueOfParameterNamed("externalId")); + + } else if (realCause.getMessage().contains("name")) { + + errorMessageForUser = groupLevel.getLevelName() + " with name {0} already exists"; + errorMessageForMachine = "error.msg." + groupLevel.getLevelName().toLowerCase() + ".duplicate.name"; + throw new PlatformDataIntegrityException(errorMessageForMachine, errorMessageForUser, "name", + command.stringValueOfParameterNamed("name")); + + } logger.error(dve.getMessage(), dve); throw new PlatformDataIntegrityException("error.msg.group.unknown.data.integrity.issue",