start of client api work.

This commit is contained in:
Keith Woodlock
2012-05-15 16:45:15 +01:00
parent 29ab498e39
commit dc1c39c5ec
9 changed files with 405 additions and 241 deletions
@@ -14,10 +14,6 @@ public interface ReadPlatformService {
Collection<OrganisationReadModel> retrieveAll();
Collection<ClientData> retrieveAllIndividualClients();
ClientData retrieveIndividualClient(Long clientId);
ClientDataWithAccountsData retrieveClientAccountDetails(Long clientId);
NewLoanWorkflowStepOneData retrieveClientAndProductDetails(Long clientId, Long productId);
@@ -13,7 +13,6 @@ import java.util.List;
import javax.sql.DataSource;
import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.mifosng.data.AppUserData;
@@ -31,6 +30,7 @@ import org.mifosng.data.OfficeData;
import org.mifosng.data.OrganisationReadModel;
import org.mifosng.platform.client.domain.Client;
import org.mifosng.platform.client.domain.ClientRepository;
import org.mifosng.platform.client.service.ClientReadPlatformService;
import org.mifosng.platform.currency.domain.ApplicationCurrency;
import org.mifosng.platform.currency.domain.ApplicationCurrencyRepository;
import org.mifosng.platform.currency.domain.Money;
@@ -41,7 +41,6 @@ import org.mifosng.platform.loan.domain.LoanRepository;
import org.mifosng.platform.loan.domain.LoanTransaction;
import org.mifosng.platform.loan.domain.LoanTransactionRepository;
import org.mifosng.platform.loanproduct.service.LoanProductReadPlatformService;
import org.mifosng.platform.organisation.domain.Organisation;
import org.mifosng.platform.user.domain.AppUser;
import org.mifosng.platform.user.service.AppUserReadPlatformService;
import org.springframework.beans.factory.annotation.Autowired;
@@ -63,11 +62,13 @@ public class ReadPlatformServiceImpl implements ReadPlatformService {
private final ApplicationCurrencyRepository applicationCurrencyRepository;
private final LoanProductReadPlatformService loanProductReadPlatformService;
private final AppUserReadPlatformService appUserReadPlatformService;
private final ClientReadPlatformService clientReadPlatformService;
@Autowired
public ReadPlatformServiceImpl(
final LoanProductReadPlatformService loanProductReadPlatformService,
final AppUserReadPlatformService appUserReadPlatformService,
final ClientReadPlatformService clientReadPlatformService,
final DataSource dataSource,
final ClientRepository clientRepository,
final LoanRepository loanRepository,
@@ -75,6 +76,7 @@ public class ReadPlatformServiceImpl implements ReadPlatformService {
final ApplicationCurrencyRepository applicationCurrencyRepository) {
this.loanProductReadPlatformService = loanProductReadPlatformService;
this.appUserReadPlatformService = appUserReadPlatformService;
this.clientReadPlatformService = clientReadPlatformService;
this.loanTransactionRepository = loanTransactionRepository;
this.applicationCurrencyRepository = applicationCurrencyRepository;
this.jdbcTemplate = new SimpleJdbcTemplate(dataSource);
@@ -125,37 +127,6 @@ public class ReadPlatformServiceImpl implements ReadPlatformService {
}
}
@Override
public Collection<ClientData> retrieveAllIndividualClients() {
AppUser currentUser = extractAuthenticatedUser();
List<OfficeData> offices = retrieveOffices();
String officeIdsList = generateOfficeIdInClause(offices);
ClientMapper rm = new ClientMapper(offices,
currentUser.getOrganisation());
String sql = "select " + rm.clientSchema()
+ " where c.org_id = ? and c.office_id in (" + officeIdsList
+ ") order by c.lastname ASC, c.firstname ASC";
return this.jdbcTemplate.query(sql, rm, new Object[] { currentUser
.getOrganisation().getId() });
}
private String generateOfficeIdInClause(List<OfficeData> offices) {
String officeIdsList = "";
for (int i = 0; i < offices.size(); i++) {
Long id = offices.get(i).getId();
if (i == 0) {
officeIdsList = id.toString();
} else {
officeIdsList += "," + id.toString();
}
}
return officeIdsList;
}
@Override
public NoteData retrieveClientNote(Long clientId, Long noteId) {
@@ -249,60 +220,6 @@ public class ReadPlatformServiceImpl implements ReadPlatformService {
}
}
protected static final class ClientMapper implements RowMapper<ClientData> {
private final List<OfficeData> offices;
private final Organisation organisation;
public ClientMapper(final List<OfficeData> offices,
Organisation organisation) {
this.offices = offices;
this.organisation = organisation;
}
public String clientSchema() {
return "c.org_id as orgId, c.office_id as officeId, c.id as id, c.firstname as firstname, c.lastname as lastname, c.external_id as externalId, c.joining_date as joinedDate from portfolio_client c";
}
@Override
public ClientData mapRow(final ResultSet rs, final int rowNum)
throws SQLException {
Long orgId = rs.getLong("orgId");
Long officeId = rs.getLong("officeId");
Long id = rs.getLong("id");
String firstname = rs.getString("firstname");
if (StringUtils.isBlank(firstname)) {
firstname = "";
}
String lastname = rs.getString("lastname");
String externalId = rs.getString("externalId");
LocalDate joinedDate = new LocalDate(rs.getDate("joinedDate"));
String officeName = fromOfficeList(this.offices, officeId);
String orgname = "";
if (organisation.getId().equals(orgId)) {
orgname = organisation.getName();
}
return new ClientData(orgId, orgname, officeId, officeName, id,
firstname, lastname, externalId, joinedDate);
}
private String fromOfficeList(final List<OfficeData> officeList,
final Long officeId) {
String match = "";
for (OfficeData office : officeList) {
if (office.getId().equals(officeId)) {
match = office.getName();
}
}
return match;
}
}
private List<OfficeData> retrieveOffices() {
AppUser currentUser = extractAuthenticatedUser();
@@ -343,35 +260,13 @@ public class ReadPlatformServiceImpl implements ReadPlatformService {
}
}
@Override
public ClientData retrieveIndividualClient(final Long clientId) {
try {
AppUser currentUser = extractAuthenticatedUser();
List<OfficeData> offices = retrieveOffices();
ClientMapper rm = new ClientMapper(offices,
currentUser.getOrganisation());
String sql = "select " + rm.clientSchema()
+ " where c.id = ? and c.org_id = ?";
return this.jdbcTemplate.queryForObject(sql, rm, new Object[] {
clientId, currentUser.getOrganisation().getId() });
} catch (EmptyResultDataAccessException e) {
throw new PlatformResourceNotFoundException(
"error.msg.client.id.invalid",
"Client with identifier {0} does not exist", clientId);
}
}
@Override
public ClientDataWithAccountsData retrieveClientAccountDetails(
final Long clientId) {
AppUser currentUser = extractAuthenticatedUser();
ClientData clientAccount = retrieveIndividualClient(clientId);
ClientData clientAccount = this.clientReadPlatformService.retrieveIndividualClient(clientId);
// TODO - rewrite using jdbc sql approach
Client client = this.clientRepository.findOne(clientsThatMatch(
@@ -539,7 +434,7 @@ public class ReadPlatformServiceImpl implements ReadPlatformService {
workflowData.setSelectedProduct(selectedProduct);
}
ClientData clientAccount = retrieveIndividualClient(clientId);
ClientData clientAccount = this.clientReadPlatformService.retrieveIndividualClient(clientId);
workflowData.setClientId(clientAccount.getId());
workflowData.setClientName(clientAccount.getDisplayName());
@@ -0,0 +1,147 @@
package org.mifosng.platform.api;
import java.util.Collection;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.mifosng.data.ClientData;
import org.mifosng.data.ClientList;
import org.mifosng.platform.WritePlatformService;
import org.mifosng.platform.client.service.ClientReadPlatformService;
import org.mifosng.platform.user.domain.AppUser;
import org.mifosng.platform.user.domain.AppUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
@Path("/v1/clients")
@Component
@Scope("singleton")
public class ClientApiResource {
@Autowired
private ClientReadPlatformService clientReadPlatformService;
@Autowired
private WritePlatformService writePlatformService;
@Autowired
private AppUserRepository appUserRepository;
private void hardcodeUserIntoSecurityContext() {
AppUser currentUser = this.appUserRepository.findOne(Long.valueOf(1));
Authentication auth = new UsernamePasswordAuthenticationToken(currentUser, currentUser, currentUser.getAuthorities());
SecurityContext context = SecurityContextHolder.getContext();
context.setAuthentication(auth);
}
@GET
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Produces({ MediaType.APPLICATION_JSON})
public Response retrieveAllIndividualClients() {
hardcodeUserIntoSecurityContext();
Collection<ClientData> clients = this.clientReadPlatformService.retrieveAllIndividualClients();
return Response.ok().entity(new ClientList(clients)).build();
}
@GET
@Path("{clientId}")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Produces({ MediaType.APPLICATION_JSON})
public Response retrieveClientData(@PathParam("clientId") final Long clientId) {
hardcodeUserIntoSecurityContext();
ClientData clientData = this.clientReadPlatformService.retrieveIndividualClient(clientId);
return Response.ok().entity(clientData).build();
}
// @GET
// @Path("template")
// @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
// @Produces({ MediaType.APPLICATION_JSON})
// public Response newClientDetails() {
//
// hardcodeUserIntoSecurityContext();
//
// ClientData clientData = this.clientReadPlatformService.retrieveNewClientDetails();
//
// return Response.ok().entity(clientData).build();
// }
//
// @POST
// @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
// @Produces({ MediaType.APPLICATION_JSON })
// public Response enrollClient(final EnrollClientCommand command) {
//
// hardcodeUserIntoSecurityContext();
//
// Long clientId = this.writePlatformService.enrollClient(command);
//
// return Response.ok().entity(new EntityIdentifier(clientId)).build();
// }
//
//
// @GET
// @Path("{clientId}/withaccounts")
// @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
// @Produces({ MediaType.APPLICATION_JSON })
// public Response retrieveClientAccount(@PathParam("clientId") final Long clientId) {
//
// hardcodeUserIntoSecurityContext();
//
// ClientDataWithAccountsData clientAccount = this.clientReadPlatformService.retrieveClientAccountDetails(clientId);
//
// return Response.ok().entity(clientAccount).build();
// }
//
// @POST
// @Path("{clientId}/note/new")
// @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
// @Produces({ MediaType.APPLICATION_JSON})
// public Response addNewClientNote(@PathParam("clientId") final Long clientId, final NoteCommand command) {
//
// hardcodeUserIntoSecurityContext();
//
// EntityIdentifier identifier = this.writePlatformService.addClientNote(command);
//
// return Response.ok().entity(identifier).build();
// }
//
// @GET
// @Path("{clientId}/note/all")
// @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
// @Produces({ MediaType.APPLICATION_JSON})
// public Response retrieveAllClientNotes(@PathParam("clientId") final Long clientId) {
//
// Collection<NoteData> notes = this.clientReadPlatformService.retrieveAllClientNotes(clientId);
//
// return Response.ok().entity(new NoteDataList(notes)).build();
// }
//
// @GET
// @Path("{clientId}/note/{noteId}")
// @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
// @Produces({ MediaType.APPLICATION_JSON })
// public Response retrieveClientNote(@PathParam("clientId") final Long clientId, @PathParam("noteId") final Long noteId) {
//
// NoteData note = this.clientReadPlatformService.retrieveClientNote(clientId, noteId);
//
// return Response.ok().entity(note).build();
// }
}
@@ -0,0 +1,12 @@
package org.mifosng.platform.client.service;
import java.util.Collection;
import org.mifosng.data.ClientData;
public interface ClientReadPlatformService {
Collection<ClientData> retrieveAllIndividualClients();
ClientData retrieveIndividualClient(Long clientId);
}
@@ -0,0 +1,143 @@
package org.mifosng.platform.client.service;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.sql.DataSource;
import org.apache.commons.lang.StringUtils;
import org.joda.time.LocalDate;
import org.mifosng.data.ClientData;
import org.mifosng.data.OfficeData;
import org.mifosng.platform.exceptions.PlatformResourceNotFoundException;
import org.mifosng.platform.organisation.domain.Organisation;
import org.mifosng.platform.organisation.service.OfficeReadPlatformService;
import org.mifosng.platform.security.PlatformSecurityContext;
import org.mifosng.platform.user.domain.AppUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.stereotype.Service;
@Service
public class ClientReadPlatformServiceImpl implements ClientReadPlatformService {
private final SimpleJdbcTemplate jdbcTemplate;
private final PlatformSecurityContext context;
private final OfficeReadPlatformService officeReadPlatformService;
@Autowired
public ClientReadPlatformServiceImpl(final PlatformSecurityContext context, final DataSource dataSource, final OfficeReadPlatformService officeReadPlatformService) {
this.context = context;
this.officeReadPlatformService = officeReadPlatformService;
this.jdbcTemplate = new SimpleJdbcTemplate(dataSource);
}
private String generateOfficeIdInClause(List<OfficeData> offices) {
String officeIdsList = "";
for (int i = 0; i < offices.size(); i++) {
Long id = offices.get(i).getId();
if (i == 0) {
officeIdsList = id.toString();
} else {
officeIdsList += "," + id.toString();
}
}
return officeIdsList;
}
@Override
public Collection<ClientData> retrieveAllIndividualClients() {
AppUser currentUser = this.context.authenticatedUser();
List<OfficeData> offices = new ArrayList<OfficeData>(officeReadPlatformService.retrieveAllOffices());
String officeIdsList = generateOfficeIdInClause(offices);
ClientMapper rm = new ClientMapper(offices, currentUser.getOrganisation());
String sql = "select " + rm.clientSchema()
+ " where c.org_id = ? and c.office_id in (" + officeIdsList
+ ") order by c.lastname ASC, c.firstname ASC";
return this.jdbcTemplate.query(sql, rm, new Object[] { currentUser
.getOrganisation().getId() });
}
@Override
public ClientData retrieveIndividualClient(final Long clientId) {
try {
AppUser currentUser = this.context.authenticatedUser();
List<OfficeData> offices = new ArrayList<OfficeData>(officeReadPlatformService.retrieveAllOffices());
ClientMapper rm = new ClientMapper(offices, currentUser.getOrganisation());
String sql = "select " + rm.clientSchema() + " where c.id = ? and c.org_id = ?";
return this.jdbcTemplate.queryForObject(sql, rm, new Object[] {clientId, currentUser.getOrganisation().getId() });
} catch (EmptyResultDataAccessException e) {
throw new PlatformResourceNotFoundException(
"error.msg.client.id.invalid",
"Client with identifier {0} does not exist", clientId);
}
}
private static final class ClientMapper implements RowMapper<ClientData> {
private final List<OfficeData> offices;
private final Organisation organisation;
public ClientMapper(final List<OfficeData> offices,
Organisation organisation) {
this.offices = offices;
this.organisation = organisation;
}
public String clientSchema() {
return "c.org_id as orgId, c.office_id as officeId, c.id as id, c.firstname as firstname, c.lastname as lastname, c.external_id as externalId, c.joining_date as joinedDate from portfolio_client c";
}
@Override
public ClientData mapRow(final ResultSet rs, final int rowNum)
throws SQLException {
Long orgId = rs.getLong("orgId");
Long officeId = rs.getLong("officeId");
Long id = rs.getLong("id");
String firstname = rs.getString("firstname");
if (StringUtils.isBlank(firstname)) {
firstname = "";
}
String lastname = rs.getString("lastname");
String externalId = rs.getString("externalId");
LocalDate joinedDate = new LocalDate(rs.getDate("joinedDate"));
String officeName = fromOfficeList(this.offices, officeId);
String orgname = "";
if (organisation.getId().equals(orgId)) {
orgname = organisation.getName();
}
return new ClientData(orgId, orgname, officeId, officeName, id,
firstname, lastname, externalId, joinedDate);
}
private String fromOfficeList(final List<OfficeData> officeList,
final Long officeId) {
String match = "";
for (OfficeData office : officeList) {
if (office.getId().equals(officeId)) {
match = office.getName();
}
}
return match;
}
}
}
@@ -26,6 +26,7 @@ import org.mifosng.data.command.EnrollClientCommand;
import org.mifosng.data.command.NoteCommand;
import org.mifosng.platform.ReadPlatformService;
import org.mifosng.platform.WritePlatformService;
import org.mifosng.platform.client.service.ClientReadPlatformService;
import org.mifosng.platform.exceptions.ApplicationDomainRuleException;
import org.mifosng.platform.exceptions.UnAuthenticatedUserException;
import org.mifosng.platform.exceptions.NewDataValidationException;
@@ -41,6 +42,9 @@ public class ClientResource {
@Autowired
private ReadPlatformService readPlatformService;
@Autowired
private ClientReadPlatformService clientReadPlatformService;
@Autowired
private WritePlatformService writePlatformService;
@@ -51,21 +55,9 @@ public class ClientResource {
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response retrieveAllIndividualClients() {
try {
Collection<ClientData> clients = this.readPlatformService.retrieveAllIndividualClients();
Collection<ClientData> clients = this.clientReadPlatformService.retrieveAllIndividualClients();
return Response.ok().entity(new ClientList(clients)).build();
} catch (UnAuthenticatedUserException e) {
throw new WebApplicationException(Response.status(Status.UNAUTHORIZED).build());
} catch (AccessDeniedException e) {
ErrorResponse errorResponse = new ErrorResponse("error.msg.no.permission", "id");
ErrorResponseList list = new ErrorResponseList(Arrays.asList(errorResponse));
throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity(list).build());
} catch (ApplicationDomainRuleException e) {
throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity(new ErrorResponseList(e.getErrors())).build());
} catch (NewDataValidationException e) {
throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity(new ErrorResponseList(e.getValidationErrors())).build());
}
}
@GET
@@ -118,23 +110,13 @@ public class ClientResource {
@Path("{clientId}")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response retrieveClientData(@PathParam("clientId") final Long clientId) {
try {
ClientData clientData = this.readPlatformService.retrieveIndividualClient(clientId);
public Response retrieveClientData(
@PathParam("clientId") final Long clientId) {
return Response.ok().entity(clientData).build();
} catch (UnAuthenticatedUserException e) {
throw new WebApplicationException(Response.status(Status.UNAUTHORIZED).build());
} catch (AccessDeniedException e) {
ErrorResponse errorResponse = new ErrorResponse("error.msg.no.permission", "id");
ErrorResponseList list = new ErrorResponseList(Arrays.asList(errorResponse));
throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity(list).build());
} catch (ApplicationDomainRuleException e) {
throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity(new ErrorResponseList(e.getErrors())).build());
} catch (NewDataValidationException e) {
throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity(new ErrorResponseList(e.getValidationErrors())).build());
}
ClientData clientData = this.clientReadPlatformService
.retrieveIndividualClient(clientId);
return Response.ok().entity(clientData).build();
}
@GET
@@ -1,38 +0,0 @@
package org.mifosng.platform.rest;
import java.util.Collection;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.mifosng.data.OrganisationList;
import org.mifosng.data.OrganisationReadModel;
import org.mifosng.platform.ReadPlatformService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Path("/protected/org")
@Component
@Scope("singleton")
public class TenantResource {
@Autowired
private ReadPlatformService readPlatformService;
@GET
@Path("view")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response retrieveExistingOrganisations() {
Collection<OrganisationReadModel> organisations = this.readPlatformService
.retrieveAll();
return Response.ok().entity(new OrganisationList(organisations))
.build();
}
}