Merge remote-tracking branch 'nayan/MIFOSX-164'

This commit is contained in:
Keith Woodlock
2013-02-21 13:52:41 +00:00
5 changed files with 151 additions and 65 deletions
@@ -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<String> GROUP_DATA_PARAMETERS = new HashSet<String>(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<GroupData> toApiJsonSerializer;
private final ToApiJsonSerializer<GroupAccountSummaryCollectionData> 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<GroupData> toApiJsonSerializer,
final ToApiJsonSerializer<GroupAccountSummaryCollectionData> 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<ClientLookup> clientMembers = this.groupReadPlatformService.retrieveClientMembers(groupId);
Collection<ClientLookup> availableClients = null;
Collection<OfficeLookup> allowedOffices = null;
Collection<GroupLookupData> 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);
}
@@ -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();
@@ -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<ClientLookup> clientMembers;
@SuppressWarnings("unused")
private final Collection<ClientLookup> allowedClients;
@@ -30,50 +31,61 @@ public class GroupData {
private final Collection<OfficeLookup> allowedOffices;
@SuppressWarnings("unused")
private final Collection<GroupLookupData> allowedParentGroups;
@SuppressWarnings("unused")
private final Collection<StaffData> allowedStaffs;
@SuppressWarnings("unused")
private final Collection<GroupLookupData> 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<ClientLookup> clientMembers, final Collection<ClientLookup> allowedClients,
final Collection<OfficeLookup> allowedOffices, final Collection<GroupLookupData> allowedParentGroups,
final GroupLevelData groupLevelData) {
final GroupLevelData groupLevelData , final Collection<StaffData> allowedStaffs , final Collection<GroupLookupData> 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<ClientLookup> allowedClients, final Collection<OfficeLookup> allowedOffices,
final Collection<GroupLookupData> allowedParentGroups, final GroupLevelData groupLevelData) {
final Collection<GroupLookupData> allowedParentGroups, final GroupLevelData groupLevelData , final Collection<StaffData> 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<ClientLookup> clientMembers() {
return this.clientMembers;
@@ -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<GroupLookupData> retrieveAllGroupsbyOfficeIdAndLevelId(Long officeId , Long levelId);
GroupLevelData retrieveGroupLevelDetails(Long levelId);
Collection<StaffData> retrieveStaffsbyOfficeId(Long officeId);
GroupData retrieveGroupDetails(Long groupId, boolean template);
Collection<GroupLookupData> retrieveChildGroupsbyGroupId(Long groupId);
}
@@ -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<GroupLookupData> 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<ClientLookup>(
this.clientReadPlatformService.retrieveAllIndividualClientsForLookupByOfficeId(officeId));
}
List<OfficeLookup> allowedOffices = new ArrayList<OfficeLookup>(officeReadPlatformService.retrieveAllOfficesForLookup());
List<GroupLookupData> allowedParentGroups = null;
if(groupLevelData.getParentLevelId() != null){
allowedParentGroups = new ArrayList<GroupLookupData>(this.retrieveAllGroupsbyOfficeIdAndLevelId(officeId , groupLevelData.getParentLevelId()));
if (groupLevelData.getParentLevelId() != null) {
allowedParentGroups = new ArrayList<GroupLookupData>(this.retrieveAllGroupsbyOfficeIdAndLevelId(officeId,
groupLevelData.getParentLevelId()));
}
List<StaffData> allowedStaffs = new ArrayList<StaffData>(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<ClientLookup> clientMembers = this.retrieveClientMembers(groupId);
Collection<ClientLookup> availableClients = null;
Collection<OfficeLookup> allowedOffices = null;
Collection<GroupLookupData> allowedParentGroups = null;
GroupLevelData groupLevelData = this.retrieveGroupLevelDetails(group.getGroupLevel());;
Collection<StaffData> allowedStaffs = null;
Collection<GroupLookupData> 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<StaffData> 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<GroupData> {
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<GroupLookupData> {
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<GroupLevelData> {
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<StaffData> {
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<ClientLookup> retrieveClientMembers(Long groupId) {