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 55e6cc885..b78716743 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 @@ -30,14 +30,9 @@ import org.mifosplatform.infrastructure.core.data.CommandProcessingResult; import org.mifosplatform.infrastructure.core.serialization.ApiRequestJsonSerializationSettings; import org.mifosplatform.infrastructure.core.serialization.ToApiJsonSerializer; import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext; -import org.mifosplatform.organisation.office.data.OfficeLookup; -import org.mifosplatform.organisation.office.service.OfficeReadPlatformService; -import org.mifosplatform.portfolio.client.data.ClientLookup; -import org.mifosplatform.portfolio.client.service.ClientReadPlatformService; import org.mifosplatform.portfolio.group.command.GroupCommand; import org.mifosplatform.portfolio.group.data.GroupAccountSummaryCollectionData; import org.mifosplatform.portfolio.group.data.GroupData; -import org.mifosplatform.portfolio.group.data.GroupLookupData; import org.mifosplatform.portfolio.group.service.GroupReadPlatformService; import org.mifosplatform.portfolio.group.service.GroupWritePlatformService; import org.mifosplatform.portfolio.savingsaccount.PortfolioApiDataConversionService; @@ -51,13 +46,11 @@ import org.springframework.stereotype.Component; public class GroupsApiResource { private static final Set GROUP_DATA_PARAMETERS = new HashSet(Arrays.asList("id", "officeId", "name", "externalId", - "clientMembers", "allowedClients", "allowedOffices" , "allowedParentGroups" ,"groupLevel")); + "clientMembers", "allowedClients", "allowedOffices" , "allowedParentGroups" ,"groupLevel" ,"allowedStaffs" ,"childGroups")); private final PlatformSecurityContext context; private final GroupReadPlatformService groupReadPlatformService; private final GroupWritePlatformService groupWritePlatformService; - private final ClientReadPlatformService clientReadPlatformService; - private final OfficeReadPlatformService officeReadPlatformService; private final ToApiJsonSerializer toApiJsonSerializer; private final ToApiJsonSerializer groupSummaryToApiJsonSerializer; private final ApiRequestParameterHelper apiRequestParameterHelper; @@ -68,8 +61,6 @@ public class GroupsApiResource { final PlatformSecurityContext context, final GroupReadPlatformService groupReadPlatformService, final GroupWritePlatformService groupWritePlatformService, - final ClientReadPlatformService clientReadPlatformService, - final OfficeReadPlatformService officeReadPlatformService, final ToApiJsonSerializer toApiJsonSerializer, final ToApiJsonSerializer groupSummaryToApiJsonSerializer, final ApiRequestParameterHelper apiRequestParameterHelper, @@ -77,8 +68,6 @@ public class GroupsApiResource { this.context = context; this.groupReadPlatformService = groupReadPlatformService; this.groupWritePlatformService = groupWritePlatformService; - this.clientReadPlatformService = clientReadPlatformService; - this.officeReadPlatformService = officeReadPlatformService; this.toApiJsonSerializer = toApiJsonSerializer; this.groupSummaryToApiJsonSerializer = groupSummaryToApiJsonSerializer; this.apiRequestParameterHelper = apiRequestParameterHelper; @@ -105,34 +94,13 @@ public class GroupsApiResource { @Path("{groupId}") @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) - public String retrieveGroupData(@Context final UriInfo uriInfo, @PathParam("groupId") final Long groupId, - @QueryParam("officeId") final Long officeId) { + public String retrieveGroupData(@Context final UriInfo uriInfo, @PathParam("groupId") final Long groupId) { context.authenticatedUser().validateHasReadPermission("GROUP"); - - GroupData group = this.groupReadPlatformService.retrieveGroup(groupId); - final Collection clientMembers = this.groupReadPlatformService.retrieveClientMembers(groupId); - Collection availableClients = null; - Collection allowedOffices = null; - Collection allowedParentGroups = null; - - group = new GroupData(group, clientMembers, availableClients, allowedOffices , allowedParentGroups , null); - + boolean template = ApiParameterHelper.template(uriInfo.getQueryParameters()); - if (template) { - if (officeId != null) { - availableClients = this.clientReadPlatformService.retrieveAllIndividualClientsForLookupByOfficeId(officeId); - } else { - availableClients = this.clientReadPlatformService.retrieveAllIndividualClientsForLookupByOfficeId(group.getOfficeId()); - } - - availableClients.removeAll(group.clientMembers()); - - allowedOffices = officeReadPlatformService.retrieveAllOfficesForLookup(); - - group = new GroupData(group, group.clientMembers(), availableClients, allowedOffices , allowedParentGroups ,null); - } - + GroupData group = this.groupReadPlatformService.retrieveGroupDetails(groupId, template); + final ApiRequestJsonSerializationSettings settings = apiRequestParameterHelper.process(uriInfo.getQueryParameters()); return this.toApiJsonSerializer.serialize(settings, group, GROUP_DATA_PARAMETERS); } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/command/GroupCommandValidator.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/command/GroupCommandValidator.java index 85a33a75a..630e6f652 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/command/GroupCommandValidator.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/command/GroupCommandValidator.java @@ -45,8 +45,6 @@ public class GroupCommandValidator { baseDataValidator.reset().parameter("id").value(command.getId()).notNull(); baseDataValidator.reset().parameter("name").value(command.getName()).ignoreIfNull().notBlank(); baseDataValidator.reset().parameter("externalId").value(command.getExternalId()).ignoreIfNull().notExceedingLengthOf(100); - - baseDataValidator.reset().parameter("clientMembers").value(command.getClientMembers()).arrayNotEmpty(); if (command.isOfficeIdChanged()) { baseDataValidator.reset().parameter("officeId").value(command.getOfficeId()).notNull().integerGreaterThanZero(); 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 b32dbaa27..7ad51703d 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 @@ -8,6 +8,7 @@ package org.mifosplatform.portfolio.group.data; import java.util.Collection; import org.mifosplatform.organisation.office.data.OfficeLookup; +import org.mifosplatform.organisation.staff.data.StaffData; import org.mifosplatform.portfolio.client.data.ClientLookup; /** @@ -20,9 +21,9 @@ public class GroupData { private final String externalId; private final Long officeId; private final String officeName; + private final Long groupLevel; @SuppressWarnings("unused") private final GroupLevelData groupLevelData; - private final Collection clientMembers; @SuppressWarnings("unused") private final Collection allowedClients; @@ -30,50 +31,61 @@ public class GroupData { private final Collection allowedOffices; @SuppressWarnings("unused") private final Collection allowedParentGroups; + @SuppressWarnings("unused") + private final Collection allowedStaffs; + @SuppressWarnings("unused") + private final Collection childGroups; - public GroupData(final Long id, final Long officeId, final String officeName, final String name, final String externalId) { + public GroupData(final Long id, final Long officeId, final String officeName, final String name, final String externalId , final Long groupLevel ) { this.id = id; this.officeId = officeId; this.officeName = officeName; this.name = name; this.externalId = externalId; - + this.groupLevel =groupLevel; this.groupLevelData = null; this.clientMembers = null; this.allowedClients = null; this.allowedOffices = null; this.allowedParentGroups = null; + this.allowedStaffs = null; + this.childGroups = null; } public GroupData(final GroupData group, final Collection clientMembers, final Collection allowedClients, final Collection allowedOffices, final Collection allowedParentGroups, - final GroupLevelData groupLevelData) { + final GroupLevelData groupLevelData , final Collection allowedStaffs , final Collection childGroups) { 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.groupLevelData = groupLevelData; this.clientMembers = clientMembers; this.allowedClients = allowedClients; this.allowedOffices = allowedOffices; this.allowedParentGroups = allowedParentGroups; + this.allowedStaffs = allowedStaffs; + this.childGroups = childGroups; } 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; this.officeName = null; this.name = null; this.externalId = null; this.clientMembers = null; + this.groupLevel =null; this.groupLevelData = groupLevelData; this.allowedClients = allowedClients; this.allowedOffices = allowedOffices; this.allowedParentGroups = allowedParentGroups; - + this.allowedStaffs = allowedStaffs; + this.childGroups = null; } public Long getId() { @@ -95,6 +107,11 @@ public class GroupData { public String getOfficeName() { return officeName; } + + public Long getGroupLevel() { + return this.groupLevel; + } + public Collection clientMembers() { return this.clientMembers; 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 793723e91..89f5146bc 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 @@ -7,6 +7,7 @@ package org.mifosplatform.portfolio.group.service; import java.util.Collection; +import org.mifosplatform.organisation.staff.data.StaffData; import org.mifosplatform.portfolio.client.data.ClientLookup; import org.mifosplatform.portfolio.group.data.GroupAccountSummaryCollectionData; import org.mifosplatform.portfolio.group.data.GroupAccountSummaryData; @@ -31,4 +32,10 @@ public interface GroupReadPlatformService { Collection retrieveAllGroupsbyOfficeIdAndLevelId(Long officeId , Long levelId); GroupLevelData retrieveGroupLevelDetails(Long levelId); + + Collection retrieveStaffsbyOfficeId(Long officeId); + + GroupData retrieveGroupDetails(Long groupId, boolean template); + + Collection retrieveChildGroupsbyGroupId(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 9370035f9..d42037fbb 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 @@ -17,6 +17,8 @@ import org.mifosplatform.infrastructure.core.service.TenantAwareRoutingDataSourc import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext; import org.mifosplatform.organisation.office.data.OfficeLookup; import org.mifosplatform.organisation.office.service.OfficeReadPlatformService; +import org.mifosplatform.organisation.staff.data.StaffData; +import org.mifosplatform.organisation.staff.exception.StaffNotFoundException; import org.mifosplatform.portfolio.client.data.ClientLookup; import org.mifosplatform.portfolio.client.service.ClientReadPlatformService; import org.mifosplatform.portfolio.client.service.LoanStatusMapper; @@ -40,10 +42,10 @@ public class GroupReadPlatformServiceImpl implements GroupReadPlatformService { private final PlatformSecurityContext context; private final ClientReadPlatformService clientReadPlatformService; private final OfficeReadPlatformService officeReadPlatformService; - + @Autowired public GroupReadPlatformServiceImpl(final PlatformSecurityContext context, final TenantAwareRoutingDataSource dataSource, - ClientReadPlatformService clientReadPlatformService, final OfficeReadPlatformService officeReadPlatformService ) { + ClientReadPlatformService clientReadPlatformService, final OfficeReadPlatformService officeReadPlatformService) { this.context = context; this.jdbcTemplate = new JdbcTemplate(dataSource); this.clientReadPlatformService = clientReadPlatformService; @@ -90,17 +92,34 @@ public class GroupReadPlatformServiceImpl implements GroupReadPlatformService { GroupLookupMapper rm = new GroupLookupMapper(); - String sql = "select " + rm.groupLookupSchema() + " where g.office_id = ? and level_id = ? and g.is_deleted=0"; + String sql = "select " + rm.groupLookupSchema() + " from m_group g where g.office_id = ? and level_id = ? and g.is_deleted=0"; return this.jdbcTemplate.query(sql, rm, new Object[] { officeId, levelId }); } catch (EmptyResultDataAccessException e) { - + // TODO need throw proper exception throw new GroupNotFoundException(officeId); } } + @Override + public Collection retrieveChildGroupsbyGroupId(final Long groupId) { + + try { + this.context.authenticatedUser(); + + GroupLookupMapper rm = new GroupLookupMapper(); + + String sql = "select " + rm.groupLookupSchema() + " from m_group pg join m_group g where pg.id = g.parent_id and pg.id = ? and g.is_deleted=0"; + + return this.jdbcTemplate.query(sql, rm, new Object[] { groupId }); + + } catch (EmptyResultDataAccessException e) { + throw new GroupNotFoundException(groupId); + } + } + @Override public GroupLevelData retrieveGroupLevelDetails(final Long levelId) { @@ -119,9 +138,9 @@ public class GroupReadPlatformServiceImpl implements GroupReadPlatformService { } @Override - public GroupData retrieveNewGroupDetails(final Long officeId , final Long levelId) { - - GroupLevelData groupLevelData = this.retrieveGroupLevelDetails(levelId); + public GroupData retrieveNewGroupDetails(final Long officeId, final Long levelId) { + + GroupLevelData groupLevelData = this.retrieveGroupLevelDetails(levelId); if (groupLevelData == null) { throw new GroupLevelNotFoundException(levelId); } @@ -131,23 +150,78 @@ public class GroupReadPlatformServiceImpl implements GroupReadPlatformService { allowedClients = new ArrayList( this.clientReadPlatformService.retrieveAllIndividualClientsForLookupByOfficeId(officeId)); } - + List allowedOffices = new ArrayList(officeReadPlatformService.retrieveAllOfficesForLookup()); List allowedParentGroups = null; - if(groupLevelData.getParentLevelId() != null){ - allowedParentGroups = new ArrayList(this.retrieveAllGroupsbyOfficeIdAndLevelId(officeId , groupLevelData.getParentLevelId())); + if (groupLevelData.getParentLevelId() != null) { + allowedParentGroups = new ArrayList(this.retrieveAllGroupsbyOfficeIdAndLevelId(officeId, + groupLevelData.getParentLevelId())); + } + + List allowedStaffs = new ArrayList(this.retrieveStaffsbyOfficeId(officeId)); + + return new GroupData(officeId, allowedClients, allowedOffices, allowedParentGroups, groupLevelData, allowedStaffs); + } + + @Override + public GroupData retrieveGroupDetails(final Long groupId, final boolean template) { + + GroupData group = this.retrieveGroup(groupId); + final Collection clientMembers = this.retrieveClientMembers(groupId); + Collection availableClients = null; + Collection allowedOffices = null; + Collection allowedParentGroups = null; + GroupLevelData groupLevelData = this.retrieveGroupLevelDetails(group.getGroupLevel());; + Collection allowedStaffs = null; + Collection childGroups = this.retrieveChildGroupsbyGroupId(groupId); + + group = new GroupData(group, clientMembers, availableClients, allowedOffices, allowedParentGroups, groupLevelData, allowedStaffs , childGroups); + + if (template) { + + if (groupLevelData.isCanHaveClients()) { + availableClients = this.clientReadPlatformService.retrieveAllIndividualClientsForLookupByOfficeId(group.getOfficeId()); + availableClients.removeAll(group.clientMembers()); + } + allowedOffices = officeReadPlatformService.retrieveAllOfficesForLookup(); + if (groupLevelData.getParentLevelId() != null) { + allowedParentGroups = this.retrieveAllGroupsbyOfficeIdAndLevelId(group.getOfficeId(), + groupLevelData.getParentLevelId()); + } + + allowedStaffs = this.retrieveStaffsbyOfficeId(group.getOfficeId()); + + group = new GroupData(group, group.clientMembers(), availableClients, allowedOffices, allowedParentGroups, groupLevelData, + allowedStaffs , childGroups); } - - return new GroupData(officeId, allowedClients, allowedOffices , allowedParentGroups , groupLevelData); + return group; + } + + @Override + public Collection retrieveStaffsbyOfficeId(final Long officeId) { + + try { + this.context.authenticatedUser(); + + StaffDataMapper rm = new StaffDataMapper(); + + String sql = "select " + rm.staffDataSchema() + " where o.id = ?"; + + return this.jdbcTemplate.query(sql, rm, new Object[] { officeId }); + + } catch (EmptyResultDataAccessException e) { + // TODO throw Staff not found in given office + throw new StaffNotFoundException(officeId); + } } private static final class GroupDataMapper implements RowMapper { public String groupSchema() { - return "g.office_id as officeId, o.name as officeName, g.id as id, g.external_id as externalId, " + return "g.office_id as officeId, g.level_id as groupLevel , o.name as officeName, g.id as id, g.external_id as externalId, " + "g.name as name from m_group g join m_office o on o.id = g.office_id"; } @@ -157,19 +231,18 @@ public class GroupReadPlatformServiceImpl implements GroupReadPlatformService { Long id = rs.getLong("id"); String name = rs.getString("name"); String externalId = rs.getString("externalId"); - Long officeId = rs.getLong("officeId"); String officeName = rs.getString("officeName"); - - return new GroupData(id, officeId, officeName, name, externalId); + Long groupLevel = rs.getLong("groupLevel"); + return new GroupData(id, officeId, officeName, name, externalId, groupLevel); } } - + private static final class GroupLookupMapper implements RowMapper { public String groupLookupSchema() { - return "g.id as id, g.name as name from m_group g"; + return "g.id as id, g.name as name "; } @Override @@ -186,8 +259,8 @@ public class GroupReadPlatformServiceImpl implements GroupReadPlatformService { private static final class GroupLevelDataMapper implements RowMapper { public String groupLevelSchema() { - return "gl.id as id, gl.parent_id as parentLevelId , gl.level_name as levelName , gl.super_parent as superParent ," + - " gl.recursable as recursable , gl.can_have_clients as canHaveClients from m_group_level gl"; + return "gl.id as id, gl.parent_id as parentLevelId , gl.level_name as levelName , gl.super_parent as superParent ," + + " gl.recursable as recursable , gl.can_have_clients as canHaveClients from m_group_level gl"; } @Override @@ -205,6 +278,29 @@ public class GroupReadPlatformServiceImpl implements GroupReadPlatformService { } + private static final class StaffDataMapper implements RowMapper { + + public String staffDataSchema() { + return "s.id as id , s.is_loan_officer as isLoanOfficer , s.office_id as officeId , s.firstname as firstname, " + + "s.lastname as lastname , s.display_name as displayName , o.name as officeName from m_staff s join m_office o on s.office_id =o.id "; + } + + @Override + public StaffData mapRow(ResultSet rs, @SuppressWarnings("unused") int rowNum) throws SQLException { + + Long id = rs.getLong("id"); + String firstname = rs.getString("firstname"); + String lastname = rs.getString("lastname"); + String displayName = rs.getString("displayName"); + Long officeId = rs.getLong("officeId"); + String officeName = rs.getString("officeName"); + boolean isLoanOfficer = rs.getBoolean("isLoanOfficer"); + + return StaffData.instance(id, firstname, lastname, displayName, officeId, officeName, isLoanOfficer); + } + + } + @Override public Collection retrieveClientMembers(Long groupId) {