MIFOSX-26 group template API returns only clients from specified office.

This commit is contained in:
Michal Dudzinski
2012-10-04 14:58:10 +02:00
parent 82aebe4200
commit cf865a28d9
5 changed files with 54 additions and 20 deletions
@@ -13,6 +13,7 @@ import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@@ -31,6 +32,8 @@ import org.mifosng.platform.client.service.ClientReadPlatformService;
import org.mifosng.platform.group.service.GroupReadPlatformService;
import org.mifosng.platform.group.service.GroupWritePlatformService;
import org.mifosng.platform.organisation.service.OfficeReadPlatformService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@@ -40,9 +43,11 @@ import org.springframework.stereotype.Component;
@Scope("singleton")
public class GroupsApiResource {
private final static Logger logger = LoggerFactory.getLogger(GroupsApiResource.class);
@Autowired
private GroupReadPlatformService groupReadPlatformService;
@Autowired
private GroupWritePlatformService groupWritePlatformService;
@@ -71,19 +76,21 @@ public class GroupsApiResource {
if (responseParameters.isEmpty()) {
responseParameters.addAll(typicalResponseParameters);
}
boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters());
Collection<GroupData> groups = this.groupReadPlatformService.retrieveAllGroups();
return this.apiJsonSerializerService.serializeGroupDataToJson(prettyPrint, responseParameters, groups);
}
@GET
@Path("{groupId}")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public String retrieveGroupData(@PathParam("groupId") final Long groupId, @Context final UriInfo uriInfo) {
public String retrieveGroupData(@Context final UriInfo uriInfo,
@PathParam("groupId") final Long groupId,
@QueryParam("officeId") final Long officeId) {
Set<String> responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo
.getQueryParameters());
@@ -92,27 +99,34 @@ public class GroupsApiResource {
}
boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters());
GroupData group = this.groupReadPlatformService.retrieveGroup(groupId);
final Collection<ClientLookup> clientMembers = this.groupReadPlatformService.retrieveClientMembers(groupId);
Collection<ClientLookup> availableClients = null;
Collection<OfficeLookup> allowedOffices = null;
group = new GroupData(group, clientMembers, availableClients, allowedOffices);
boolean template = ApiParameterHelper.template(uriInfo.getQueryParameters());
if (template) {
responseParameters.add("allowedClients");
responseParameters.add("allowedOffices");
availableClients = this.clientReadPlatformService.retrieveAllIndividualClientsForLookup();
final String extraCriteria;
if (officeId != null){
extraCriteria = getGroupExtraCriteria(officeId);
} else {
extraCriteria = getGroupExtraCriteria(group.getOfficeId());
}
availableClients = this.clientReadPlatformService.retrieveAllIndividualClientsForLookup(extraCriteria);
availableClients.removeAll(group.clientMembers());
allowedOffices = officeReadPlatformService.retrieveAllOfficesForLookup();
group = new GroupData(group, group.clientMembers(), availableClients, allowedOffices);
}
return this.apiJsonSerializerService.serializeGroupDataToJson(prettyPrint, responseParameters, group);
}
@@ -120,7 +134,8 @@ public class GroupsApiResource {
@Path("template")
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
public String newGroupDetails(@Context final UriInfo uriInfo) {
public String newGroupDetails(@Context final UriInfo uriInfo,
@QueryParam("officeId") final Long officeId) {
Set<String> responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters());
if (responseParameters.isEmpty()) {
@@ -130,7 +145,9 @@ public class GroupsApiResource {
responseParameters.addAll(Arrays.asList("allowedClients", "allowedOffices"));
GroupData groupData = this.groupReadPlatformService.retrieveNewGroupDetails();
final String extraCriteria = getGroupExtraCriteria(officeId);
GroupData groupData = this.groupReadPlatformService.retrieveNewGroupDetails(extraCriteria);
return this.apiJsonSerializerService.serializeGroupDataToJson(prettyPrint, responseParameters, groupData);
}
@@ -194,4 +211,17 @@ public class GroupsApiResource {
return this.apiJsonSerializerService.serializeGroupAccountSummaryCollectionDataToJson(prettyPrint, responseParameters, clientAccount);
}
private String getGroupExtraCriteria(Long officeId){
String extraCriteria = "";
if (officeId != null) {
extraCriteria += " office_id = " + officeId;
}
logger.info("extraCriteria; " + extraCriteria);
return extraCriteria;
}
}
@@ -15,7 +15,7 @@ public interface ClientReadPlatformService {
ClientData retrieveNewClientDetails();
Collection<ClientLookup> retrieveAllIndividualClientsForLookup();
Collection<ClientLookup> retrieveAllIndividualClientsForLookup(String extraCriteria);
ClientAccountSummaryCollectionData retrieveClientAccountDetails(Long clientId);
@@ -84,13 +84,17 @@ public class ClientReadPlatformServiceImpl implements ClientReadPlatformService
}
@Override
public Collection<ClientLookup> retrieveAllIndividualClientsForLookup() {
public Collection<ClientLookup> retrieveAllIndividualClientsForLookup(String extraCriteria) {
this.context.authenticatedUser();
ClientLookupMapper rm = new ClientLookupMapper();
String sql = "select " + rm.clientLookupSchema() + " where c.is_deleted = 0";
String sql = "select " + rm.clientLookupSchema();
if (StringUtils.isNotBlank(extraCriteria)){
sql += " and (" + extraCriteria + ")";
}
return this.jdbcTemplate.query(sql, rm, new Object[] {});
}
@@ -145,7 +149,7 @@ public class ClientReadPlatformServiceImpl implements ClientReadPlatformService
public String clientLookupSchema() {
return "c.id as id, c.firstname as firstname, c.lastname as lastname, " +
"c.office_id as officeId, o.name as officeName " +
"from m_client c join m_office o on o.id = c.office_id";
"from m_client c join m_office o on o.id = c.office_id where c.is_deleted=0 ";
}
@Override
@@ -12,7 +12,7 @@ public interface GroupReadPlatformService {
GroupData retrieveGroup(Long groupId);
GroupData retrieveNewGroupDetails();
GroupData retrieveNewGroupDetails(String extraCriteria);
Collection<ClientLookup> retrieveClientMembers(Long groupId);
@@ -72,12 +72,12 @@ public class GroupReadPlatformServiceImpl implements GroupReadPlatformService {
}
@Override
public GroupData retrieveNewGroupDetails() {
public GroupData retrieveNewGroupDetails(String extraCriteria) {
this.context.authenticatedUser();
List<ClientLookup> allowedClients = new ArrayList<ClientLookup>(
this.clientReadPlatformService.retrieveAllIndividualClientsForLookup());
this.clientReadPlatformService.retrieveAllIndividualClientsForLookup(extraCriteria));
List<OfficeLookup> allowedOffices = new ArrayList<OfficeLookup>(officeReadPlatformService.retrieveAllOfficesForLookup());