MIFOSX-25: minor api changes.

This commit is contained in:
Michal Dudzinski
2012-08-14 16:50:23 +02:00
parent ee9656c5a6
commit 6642aacff5
9 changed files with 132 additions and 30 deletions
@@ -19,11 +19,12 @@ import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import org.mifosng.platform.api.commands.GroupCommand;
import org.mifosng.platform.api.data.ClientMemberData;
import org.mifosng.platform.api.data.ClientLookup;
import org.mifosng.platform.api.data.EntityIdentifier;
import org.mifosng.platform.api.data.GroupData;
import org.mifosng.platform.api.infrastructure.ApiDataConversionService;
import org.mifosng.platform.api.infrastructure.ApiParameterHelper;
import org.mifosng.platform.client.service.ClientReadPlatformService;
import org.mifosng.platform.group.service.GroupReadPlatformService;
import org.mifosng.platform.group.service.GroupWritePlatformService;
import org.springframework.beans.factory.annotation.Autowired;
@@ -40,7 +41,10 @@ public class GroupsApiResource {
@Autowired
private GroupWritePlatformService groupWritePlatformService;
@Autowired
private ClientReadPlatformService clientReadPlatformService;
@Autowired
private ApiDataConversionService apiDataConversionService;
@@ -84,15 +88,43 @@ public class GroupsApiResource {
}
boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters());
boolean template = ApiParameterHelper.template(uriInfo.getQueryParameters());
GroupData group = this.groupReadPlatformService.retrieveGroup(groupId);
Collection<ClientMemberData> clientMembers = this.groupReadPlatformService.retrieveClientMembers(groupId);
Collection<ClientLookup> clientMembers = this.groupReadPlatformService.retrieveClientMembers(groupId);
group.setClientMembers(clientMembers);
if (template) {
Collection<ClientLookup> availableClients = this.clientReadPlatformService.retrieveAllIndividualClientsForLookup();
availableClients.removeAll(group.getClientMembers());
group.setAllowedClients(availableClients);
responseParameters.add("allowedClients");
}
return this.apiDataConversionService.convertGroupDataToJson(prettyPrint, responseParameters, group);
}
@GET
@Path("template")
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
public String newGroupDetails(@Context final UriInfo uriInfo) {
Set<String> typicalResponseParameters = new HashSet<String>(Arrays.asList("id", "name", "externalId", "clientMembers", "allowedClients"));
Set<String> responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters());
if (responseParameters.isEmpty()) {
responseParameters.addAll(typicalResponseParameters);
}
boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters());
GroupData groupData = this.groupReadPlatformService.retrieveNewGroupDetails();
return this.apiDataConversionService.convertGroupDataToJson(prettyPrint, responseParameters, groupData);
}
@POST
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
@@ -2,12 +2,12 @@ package org.mifosng.platform.api.data;
import org.apache.commons.lang.StringUtils;
public class ClientMemberData {
public class ClientLookup {
private final Long id;
private final String displayName;
public ClientMemberData(final Long id, final String firstname, final String lastname) {
public ClientLookup(final Long id, final String firstname, final String lastname) {
this.id = id;
StringBuilder displayNameBuilder = new StringBuilder(firstname);
@@ -28,4 +28,14 @@ public class ClientMemberData {
return displayName;
}
@Override
public boolean equals(Object obj) {
ClientLookup clientLookup = (ClientLookup) obj;
return this.id.equals(clientLookup.getId());
}
@Override
public int hashCode() {
return this.id.hashCode();
}
}
@@ -9,15 +9,22 @@ public class GroupData {
private final String name;
private final String externalId;
private Collection<ClientMemberData> clientMembers = new ArrayList<ClientMemberData>();
private Collection<ClientLookup> clientMembers = new ArrayList<ClientLookup>();
private Collection<ClientLookup> allowedClients = new ArrayList<ClientLookup>();
public GroupData(Long id, String name, String externalId) {
super();
this.id = id;
this.name = name;
this.externalId = externalId;
}
public GroupData(Collection<ClientLookup> allowedClients) {
this.id = null;
this.name = null;
this.externalId = null;
this.allowedClients = allowedClients;
}
public Long getId() {
return id;
}
@@ -30,12 +37,19 @@ public class GroupData {
return externalId;
}
public Collection<ClientMemberData> getClientMembers() {
public Collection<ClientLookup> getClientMembers() {
return clientMembers;
}
public void setClientMembers(Collection<ClientMemberData> clientMembers) {
public void setClientMembers(Collection<ClientLookup> clientMembers) {
this.clientMembers = clientMembers;
}
public Collection<ClientLookup> getAllowedClients() {
return allowedClients;
}
public void setAllowedClients(Collection<ClientLookup> allowedClients) {
this.allowedClients = allowedClients;
}
}
@@ -380,7 +380,7 @@ public class ApiDataConversionServiceImpl implements ApiDataConversionService {
@Override
public String convertGroupDataToJson(boolean prettyPrint, Set<String> responseParameters, GroupData... groups) {
final Set<String> supportedParameters = new HashSet<String>(
Arrays.asList("id", "name", "externalId", "clientMembers")
Arrays.asList("id", "name", "externalId", "clientMembers", "allowedClients")
);
final Set<String> parameterNamesToSkip = new HashSet<String>();
@@ -4,6 +4,7 @@ import java.util.Collection;
import org.mifosng.platform.api.data.ClientData;
import org.mifosng.platform.api.data.ClientLoanAccountSummaryCollectionData;
import org.mifosng.platform.api.data.ClientLookup;
import org.mifosng.platform.api.data.NoteData;
public interface ClientReadPlatformService {
@@ -14,6 +15,8 @@ public interface ClientReadPlatformService {
ClientData retrieveNewClientDetails();
Collection<ClientLookup> retrieveAllIndividualClientsForLookup();
ClientLoanAccountSummaryCollectionData retrieveClientAccountDetails(Long clientId);
Collection<NoteData> retrieveAllClientNotes(Long clientId);
@@ -9,14 +9,7 @@ import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.mifosng.platform.api.data.AppUserData;
import org.mifosng.platform.api.data.ClientData;
import org.mifosng.platform.api.data.ClientLoanAccountSummaryCollectionData;
import org.mifosng.platform.api.data.ClientLoanAccountSummaryData;
import org.mifosng.platform.api.data.EnumOptionData;
import org.mifosng.platform.api.data.NoteData;
import org.mifosng.platform.api.data.OfficeData;
import org.mifosng.platform.api.data.OfficeLookup;
import org.mifosng.platform.api.data.*;
import org.mifosng.platform.client.domain.NoteEnumerations;
import org.mifosng.platform.exceptions.ClientNotFoundException;
import org.mifosng.platform.exceptions.NoteNotFoundException;
@@ -103,7 +96,19 @@ public class ClientReadPlatformServiceImpl implements ClientReadPlatformService
}
}
@Override
@Override
public Collection<ClientLookup> retrieveAllIndividualClientsForLookup() {
this.context.authenticatedUser();
ClientLookupMapper rm = new ClientLookupMapper();
String sql = "select " + rm.clientLookupSchema() + " where c.is_deleted = 0";
return this.jdbcTemplate.query(sql, rm, new Object[] {});
}
@Override
public ClientData retrieveNewClientDetails() {
AppUser currentUser = context.authenticatedUser();
@@ -115,6 +120,8 @@ public class ClientReadPlatformServiceImpl implements ClientReadPlatformService
return new ClientData(officeId, new LocalDate(), offices);
}
private static final class ClientMapper implements RowMapper<ClientData> {
private final List<OfficeData> offices;
@@ -161,6 +168,24 @@ public class ClientReadPlatformServiceImpl implements ClientReadPlatformService
}
}
private static final class ClientLookupMapper implements RowMapper<ClientLookup>{
public String clientLookupSchema() {
return "c.id as id, c.firstname as firstname, c.lastname as lastname from portfolio_client c";
}
@Override
public ClientLookup mapRow(ResultSet rs, int rowNum) throws SQLException {
Long id = rs.getLong("id");
String firstname = rs.getString("firstname");
String lastname = rs.getString("lastname");
if (StringUtils.isBlank(firstname)) {
firstname = "";
}
return new ClientLookup(id, firstname, lastname);
}
}
@Override
public ClientLoanAccountSummaryCollectionData retrieveClientAccountDetails(final Long clientId) {
@@ -25,7 +25,7 @@ public class GroupCommandValidator {
baseDataValidator.reset().parameter("name").value(command.getName()).notBlank();
baseDataValidator.reset().parameter("externalId").value(command.getExternalId()).ignoreIfNull().notExceedingLengthOf(100);
baseDataValidator.reset().parameter("clientMembers").value(command.getClientMembers()).ignoreIfNull().arrayNotEmpty();
baseDataValidator.reset().parameter("clientMembers").value(command.getClientMembers()).arrayNotEmpty();
if (!dataValidationErrors.isEmpty()) {
throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.", dataValidationErrors);
@@ -42,7 +42,7 @@ public class GroupCommandValidator {
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()).ignoreIfNull();
baseDataValidator.reset().parameter("clientMembers").value(command.getClientMembers()).arrayNotEmpty();
if (!dataValidationErrors.isEmpty()) {
throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.", dataValidationErrors);
@@ -2,7 +2,7 @@ package org.mifosng.platform.group.service;
import java.util.Collection;
import org.mifosng.platform.api.data.ClientMemberData;
import org.mifosng.platform.api.data.ClientLookup;
import org.mifosng.platform.api.data.GroupData;
public interface GroupReadPlatformService {
@@ -10,6 +10,8 @@ public interface GroupReadPlatformService {
Collection<GroupData> retrieveAllGroups();
GroupData retrieveGroup(Long groupId);
Collection<ClientMemberData> retrieveClientMembers(Long groupId);
GroupData retrieveNewGroupDetails();
Collection<ClientLookup> retrieveClientMembers(Long groupId);
}
@@ -2,11 +2,14 @@ package org.mifosng.platform.group.service;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.mifosng.platform.api.data.ClientMemberData;
import org.mifosng.platform.api.data.ClientLookup;
import org.mifosng.platform.api.data.GroupData;
import org.mifosng.platform.client.service.ClientReadPlatformService;
import org.mifosng.platform.exceptions.GroupNotFoundException;
import org.mifosng.platform.infrastructure.TenantAwareRoutingDataSource;
import org.mifosng.platform.security.PlatformSecurityContext;
@@ -21,12 +24,14 @@ public class GroupReadPlatformServiceImpl implements GroupReadPlatformService {
private final JdbcTemplate jdbcTemplate;
private final PlatformSecurityContext context;
private final ClientReadPlatformService clientReadPlatformService;
@Autowired
public GroupReadPlatformServiceImpl(final PlatformSecurityContext context,
final TenantAwareRoutingDataSource dataSource) {
final TenantAwareRoutingDataSource dataSource, ClientReadPlatformService clientReadPlatformService) {
this.context = context;
this.jdbcTemplate = new JdbcTemplate(dataSource);
this.clientReadPlatformService = clientReadPlatformService;
}
@Override
@@ -57,6 +62,17 @@ public class GroupReadPlatformServiceImpl implements GroupReadPlatformService {
}
}
@Override
public GroupData retrieveNewGroupDetails() {
this.context.authenticatedUser();
List<ClientLookup> allowedClients = new ArrayList<ClientLookup>(
this.clientReadPlatformService.retrieveAllIndividualClientsForLookup());
return new GroupData(allowedClients);
}
private static final class GroupMapper implements RowMapper<GroupData> {
public String groupSchema() {
@@ -76,7 +92,7 @@ public class GroupReadPlatformServiceImpl implements GroupReadPlatformService {
}
@Override
public Collection<ClientMemberData> retrieveClientMembers(Long groupId){
public Collection<ClientLookup> retrieveClientMembers(Long groupId){
this.context.authenticatedUser();
@@ -87,21 +103,21 @@ public class GroupReadPlatformServiceImpl implements GroupReadPlatformService {
return this.jdbcTemplate.query(sql, rm, new Object[] {groupId});
}
private static final class ClientMemberSummaryDataMapper implements RowMapper<ClientMemberData> {
private static final class ClientMemberSummaryDataMapper implements RowMapper<ClientLookup> {
public String clientMemberSummarySchema() {
return "cm.id, cm.firstname, cm.lastname from portfolio_client cm INNER JOIN portfolio_group_client pgc ON pgc.client_id = cm.id";
}
@Override
public ClientMemberData mapRow(ResultSet rs, int rowNum) throws SQLException {
public ClientLookup mapRow(ResultSet rs, int rowNum) throws SQLException {
Long id = rs.getLong("id");
String firstname = rs.getString("firstname");
String lastname = rs.getString("lastname");
if (StringUtils.isBlank(firstname)) {
firstname = "";
}
return new ClientMemberData(id, firstname, lastname);
return new ClientLookup(id, firstname, lastname);
}
}