updating Client crud service & Db schema for MifosX-35

This commit is contained in:
Vishwas Babu A J
2012-09-05 01:27:54 +05:30
parent b9c8f63441
commit a22d5d10aa
4 changed files with 672 additions and 649 deletions
@@ -0,0 +1,4 @@
ALTER TABLE `m_client`
ADD COLUMN `display_name` VARCHAR(100) NOT NULL AFTER `lastname`;
update m_client set display_name = concat_ws(' ',firstname,lastname);
@@ -1,106 +1,103 @@
package org.mifosng.platform.api.data; package org.mifosng.platform.api.data;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.joda.time.LocalDate; import org.joda.time.LocalDate;
public class ClientData { public class ClientData {
private final Long id; private final Long id;
private final String firstname; private final String firstname;
private final String lastname; private final String lastname;
private final String clientOrBusinessName; private final String clientOrBusinessName;
private final String displayName; private final String displayName;
private final Long officeId; private final Long officeId;
private final String officeName; private final String officeName;
private final String externalId; private final String externalId;
private final LocalDate joinedDate; private final LocalDate joinedDate;
private List<OfficeLookup> allowedOffices = new ArrayList<OfficeLookup>(); private List<OfficeLookup> allowedOffices = new ArrayList<OfficeLookup>();
public ClientData(final Long officeId, final String officeName, public ClientData(final Long officeId, final String officeName,
final Long id, final String firstname, final String lastname, final Long id, final String firstname, final String lastname,
final String externalId, final LocalDate joinedDate) { final String displayName, final String externalId,
this.officeId = officeId; final LocalDate joinedDate) {
this.officeName = officeName; this.officeId = officeId;
this.id = id; this.officeName = officeName;
this.firstname = firstname; this.id = id;
this.firstname = firstname;
StringBuilder nameBuilder = new StringBuilder(this.firstname); this.displayName = displayName;
if (StringUtils.isNotBlank(nameBuilder.toString())) {
nameBuilder.append(' '); /*** unset last name for business name **/
} if (StringUtils.isBlank(this.firstname)) {
nameBuilder.append(lastname); this.lastname = null;
this.clientOrBusinessName = lastname;
this.displayName = nameBuilder.toString(); } else {
if (StringUtils.isBlank(this.firstname)) { this.lastname = lastname;
this.lastname = null; this.clientOrBusinessName = null;
this.clientOrBusinessName = nameBuilder.toString(); }
} else {
this.lastname = lastname; this.externalId = externalId;
this.clientOrBusinessName = null; this.joinedDate = joinedDate;
} }
this.externalId = externalId; public ClientData(final Long officeId, final LocalDate joinedDate,
this.joinedDate = joinedDate; final List<OfficeLookup> offices) {
} this.id = null;
this.officeId = officeId;
public ClientData(final Long officeId, final LocalDate joinedDate, final List<OfficeLookup> offices) { this.officeName = null;
this.id = null; this.firstname = null;
this.officeId = officeId; this.lastname = null;
this.officeName = null; this.clientOrBusinessName = null;
this.firstname = null; this.displayName = null;
this.lastname = null; this.externalId = null;
this.clientOrBusinessName = null; this.joinedDate = joinedDate;
this.displayName = null; this.allowedOffices = offices;
this.externalId = null; }
this.joinedDate = joinedDate;
this.allowedOffices = offices; public Long getId() {
} return id;
}
public Long getId() {
return id; public Long getOfficeId() {
} return officeId;
}
public Long getOfficeId() {
return officeId; public String getOfficeName() {
} return officeName;
}
public String getOfficeName() {
return officeName; public String getFirstname() {
} return firstname;
}
public String getFirstname() {
return firstname; public String getLastname() {
} return lastname;
}
public String getLastname() {
return lastname; public String getClientOrBusinessName() {
} return clientOrBusinessName;
}
public String getClientOrBusinessName() {
return clientOrBusinessName; public String getDisplayName() {
} return displayName;
}
public String getDisplayName() {
return displayName; public String getExternalId() {
} return externalId;
}
public String getExternalId() {
return externalId; public LocalDate getJoinedDate() {
} return joinedDate;
}
public LocalDate getJoinedDate() {
return joinedDate; public List<OfficeLookup> getAllowedOffices() {
} return allowedOffices;
}
public List<OfficeLookup> getAllowedOffices() {
return allowedOffices; public void setAllowedOffices(List<OfficeLookup> allowedOffices) {
} this.allowedOffices = allowedOffices;
}
public void setAllowedOffices(List<OfficeLookup> allowedOffices) {
this.allowedOffices = allowedOffices;
}
} }
@@ -1,138 +1,159 @@
package org.mifosng.platform.client.domain; package org.mifosng.platform.client.domain;
import java.util.Date; import java.util.Date;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.JoinColumn; import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import javax.persistence.Table; import javax.persistence.Table;
import javax.persistence.Temporal; import javax.persistence.Temporal;
import javax.persistence.TemporalType; import javax.persistence.TemporalType;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.joda.time.LocalDate; import org.joda.time.LocalDate;
import org.mifosng.platform.api.commands.ClientCommand; import org.mifosng.platform.api.commands.ClientCommand;
import org.mifosng.platform.infrastructure.AbstractAuditableCustom; import org.mifosng.platform.infrastructure.AbstractAuditableCustom;
import org.mifosng.platform.organisation.domain.Office; import org.mifosng.platform.organisation.domain.Office;
import org.mifosng.platform.user.domain.AppUser; import org.mifosng.platform.user.domain.AppUser;
@Entity @Entity
@Table(name = "m_client") @Table(name = "m_client")
public class Client extends AbstractAuditableCustom<AppUser, Long> { public class Client extends AbstractAuditableCustom<AppUser, Long> {
@SuppressWarnings("unused") @SuppressWarnings("unused")
@ManyToOne @ManyToOne
@JoinColumn(name = "office_id", nullable = false) @JoinColumn(name = "office_id", nullable = false)
private Office office; private Office office;
@SuppressWarnings("unused") @SuppressWarnings("unused")
@Column(name = "firstname", length=50) @Column(name = "firstname", length=50)
private String firstName; private String firstName;
@SuppressWarnings("unused") @SuppressWarnings("unused")
@Column(name = "lastname", length=50) @Column(name = "lastname", length=50)
private String lastName; private String lastName;
@SuppressWarnings("unused") @SuppressWarnings("unused")
@Column(name = "joining_date") @Column(name = "display_name", length=50)
@Temporal(TemporalType.DATE) private String displayName;
private Date joiningDate;
@SuppressWarnings("unused")
@Column(name = "external_id", length=100, unique=true) @Column(name = "joining_date")
private String externalId; @Temporal(TemporalType.DATE)
private Date joiningDate;
@Column(name = "is_deleted", nullable=false)
private boolean deleted = false; @Column(name = "external_id", length=100, unique=true)
private String externalId;
public static Client newClient(Office clientOffice, String firstname, String lastname, LocalDate joiningDate, String externalId) {
return new Client(clientOffice, firstname, lastname, joiningDate, externalId); @Column(name = "is_deleted", nullable=false)
} private boolean deleted = false;
public Client() { public static Client newClient(Office clientOffice, String firstname, String lastname, LocalDate joiningDate, String externalId) {
this.office = null; return new Client(clientOffice, firstname, lastname, joiningDate, externalId);
this.joiningDate = null; }
this.firstName = null;
this.lastName = null; public Client() {
this.externalId = null; this.office = null;
} this.joiningDate = null;
this.firstName = null;
private Client(final Office office, final String firstName, final String lastName, final LocalDate openingDate, final String externalId) { this.lastName = null;
this.office = office; this.externalId = null;
if (StringUtils.isNotBlank(externalId)) { }
this.externalId = externalId.trim();
} else { private Client(final Office office, final String firstName, final String lastName, final LocalDate openingDate, final String externalId) {
this.externalId = null; this.office = office;
} if (StringUtils.isNotBlank(externalId)) {
this.joiningDate = openingDate.toDateMidnight().toDate(); this.externalId = externalId.trim();
if (StringUtils.isNotBlank(firstName)) { } else {
this.firstName = firstName.trim(); this.externalId = null;
} else { }
this.firstName = null; this.joiningDate = openingDate.toDateMidnight().toDate();
} if (StringUtils.isNotBlank(firstName)) {
this.lastName = lastName.trim(); this.firstName = firstName.trim();
} } else {
this.firstName = null;
public boolean identifiedBy(final String identifier) { }
return identifier.equalsIgnoreCase(this.externalId); this.lastName = lastName.trim();
} //populate display name
if(this.firstName!=null){
public boolean identifiedBy(Long clientId) { this.displayName = this.firstName + " " + this.lastName;
return getId().equals(clientId); }else{
} this.displayName = this.lastName;
}
public void update(ClientCommand command) { }
this.joiningDate = command.getJoiningDate().toDate();
this.firstName = command.getFirstname(); public boolean identifiedBy(final String identifier) {
this.lastName = command.getLastname(); return identifier.equalsIgnoreCase(this.externalId);
}
if (StringUtils.isNotBlank(command.getClientOrBusinessName())) {
this.firstName = null; public boolean identifiedBy(Long clientId) {
this.lastName = command.getClientOrBusinessName().trim(); return getId().equals(clientId);
} }
if (StringUtils.isNotBlank(command.getExternalId())) { public void update(ClientCommand command) {
this.externalId = command.getExternalId().trim(); this.joiningDate = command.getJoiningDate().toDate();
} else { this.firstName = command.getFirstname();
this.externalId = null; this.lastName = command.getLastname();
}
} if (StringUtils.isNotBlank(command.getClientOrBusinessName())) {
this.firstName = null;
public void update(final Office clientOffice, final ClientCommand command) { this.lastName = command.getClientOrBusinessName().trim();
if (command.isOfficeChanged()) { this.displayName = this.lastName;
this.office = clientOffice; }else{
} deriveDisplayName();
if (command.isJoiningDateChanged()) { }
this.joiningDate = command.getJoiningDate().toDate();
} if (StringUtils.isNotBlank(command.getExternalId())) {
if (command.isFirstnameChanged()) { this.externalId = command.getExternalId().trim();
this.firstName = command.getFirstname(); } else {
} this.externalId = null;
if (command.isLastnameChanged()) { }
this.lastName = command.getLastname(); }
}
public void update(final Office clientOffice, final ClientCommand command) {
if (command.isClientOrBusinessNameChanged() && StringUtils.isNotBlank(command.getClientOrBusinessName())) { if (command.isOfficeChanged()) {
this.lastName = command.getClientOrBusinessName(); this.office = clientOffice;
this.firstName = null; }
} if (command.isJoiningDateChanged()) {
this.joiningDate = command.getJoiningDate().toDate();
if (command.isExternalIdChanged()) { }
this.externalId = StringUtils.defaultIfEmpty(command.getExternalId().trim(), null); if (command.isFirstnameChanged()) {
} this.firstName = command.getFirstname();
} }
if (command.isLastnameChanged()) {
/** this.lastName = command.getLastname();
* Delete is a <i>soft delete</i>. Updates flag on client so it wont appear in query/report results. }
*
* Any fields with unique constraints and prepended with id of record. if (command.isClientOrBusinessNameChanged() && StringUtils.isNotBlank(command.getClientOrBusinessName())) {
*/ this.lastName = command.getClientOrBusinessName();
public void delete() { this.firstName = null;
this.deleted = true; this.displayName = this.lastName;
this.externalId = this.getId() + "_" + this.externalId; }else if(command.isFirstnameChanged() || command.isLastnameChanged()) {
} //do not want to enter this condition for business names
deriveDisplayName();
public boolean isDeleted() { }
return deleted;
} if (command.isExternalIdChanged()) {
this.externalId = StringUtils.defaultIfEmpty(command.getExternalId().trim(), null);
}
}
private void deriveDisplayName() {
this.displayName = this.firstName + " " + this.lastName;
}
/**
* Delete is a <i>soft delete</i>. Updates flag on client so it wont appear in query/report results.
*
* Any fields with unique constraints and prepended with id of record.
*/
public void delete() {
this.deleted = true;
this.externalId = this.getId() + "_" + this.externalId;
}
public boolean isDeleted() {
return deleted;
}
} }
@@ -1,408 +1,409 @@
package org.mifosng.platform.client.service; package org.mifosng.platform.client.service;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.joda.time.LocalDate; import org.joda.time.LocalDate;
import org.mifosng.platform.api.data.*; import org.mifosng.platform.api.data.*;
import org.mifosng.platform.client.domain.NoteEnumerations; import org.mifosng.platform.client.domain.NoteEnumerations;
import org.mifosng.platform.exceptions.ClientNotFoundException; import org.mifosng.platform.exceptions.ClientNotFoundException;
import org.mifosng.platform.exceptions.NoteNotFoundException; import org.mifosng.platform.exceptions.NoteNotFoundException;
import org.mifosng.platform.infrastructure.JdbcSupport; import org.mifosng.platform.infrastructure.JdbcSupport;
import org.mifosng.platform.infrastructure.TenantAwareRoutingDataSource; import org.mifosng.platform.infrastructure.TenantAwareRoutingDataSource;
import org.mifosng.platform.organisation.service.OfficeReadPlatformService; import org.mifosng.platform.organisation.service.OfficeReadPlatformService;
import org.mifosng.platform.security.PlatformSecurityContext; import org.mifosng.platform.security.PlatformSecurityContext;
import org.mifosng.platform.user.domain.AppUser; import org.mifosng.platform.user.domain.AppUser;
import org.mifosng.platform.user.service.AppUserReadPlatformService; import org.mifosng.platform.user.service.AppUserReadPlatformService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@Service @Service
public class ClientReadPlatformServiceImpl implements ClientReadPlatformService { public class ClientReadPlatformServiceImpl implements ClientReadPlatformService {
private final JdbcTemplate jdbcTemplate; private final JdbcTemplate jdbcTemplate;
private final PlatformSecurityContext context; private final PlatformSecurityContext context;
private final OfficeReadPlatformService officeReadPlatformService; private final OfficeReadPlatformService officeReadPlatformService;
private final AppUserReadPlatformService appUserReadPlatformService; private final AppUserReadPlatformService appUserReadPlatformService;
@Autowired @Autowired
public ClientReadPlatformServiceImpl(final PlatformSecurityContext context, public ClientReadPlatformServiceImpl(final PlatformSecurityContext context,
final TenantAwareRoutingDataSource dataSource, final TenantAwareRoutingDataSource dataSource,
final OfficeReadPlatformService officeReadPlatformService, final OfficeReadPlatformService officeReadPlatformService,
final AppUserReadPlatformService appUserReadPlatformService) { final AppUserReadPlatformService appUserReadPlatformService) {
this.context = context; this.context = context;
this.officeReadPlatformService = officeReadPlatformService; this.officeReadPlatformService = officeReadPlatformService;
this.appUserReadPlatformService = appUserReadPlatformService; this.appUserReadPlatformService = appUserReadPlatformService;
this.jdbcTemplate = new JdbcTemplate(dataSource); this.jdbcTemplate = new JdbcTemplate(dataSource);
} }
private String generateOfficeIdInClause(List<OfficeData> offices) { private String generateOfficeIdInClause(List<OfficeData> offices) {
String officeIdsList = ""; String officeIdsList = "";
for (int i = 0; i < offices.size(); i++) { for (int i = 0; i < offices.size(); i++) {
Long id = offices.get(i).getId(); Long id = offices.get(i).getId();
if (i == 0) { if (i == 0) {
officeIdsList = id.toString(); officeIdsList = id.toString();
} else { } else {
officeIdsList += "," + id.toString(); officeIdsList += "," + id.toString();
} }
} }
return officeIdsList; return officeIdsList;
} }
@Override @Override
public Collection<ClientData> retrieveAllIndividualClients(final String extraCriteria) { public Collection<ClientData> retrieveAllIndividualClients(final String extraCriteria) {
this.context.authenticatedUser(); this.context.authenticatedUser();
List<OfficeData> offices = new ArrayList<OfficeData>(officeReadPlatformService.retrieveAllOffices()); List<OfficeData> offices = new ArrayList<OfficeData>(officeReadPlatformService.retrieveAllOffices());
String officeIdsList = generateOfficeIdInClause(offices); String officeIdsList = generateOfficeIdInClause(offices);
ClientMapper rm = new ClientMapper(offices); ClientMapper rm = new ClientMapper(offices);
String sql = "select " + rm.clientSchema() + " where c.office_id in (" + officeIdsList + ") and c.is_deleted=0"; String sql = "select " + rm.clientSchema() + " where c.office_id in (" + officeIdsList + ") and c.is_deleted=0";
if (StringUtils.isNotBlank(extraCriteria)) if (StringUtils.isNotBlank(extraCriteria))
sql += " and " + extraCriteria; sql += " and " + extraCriteria;
sql += " order by c.lastname ASC, c.firstname ASC"; sql += " order by c.lastname ASC, c.firstname ASC";
return this.jdbcTemplate.query(sql, rm, new Object[] {}); return this.jdbcTemplate.query(sql, rm, new Object[] {});
} }
@Override @Override
public ClientData retrieveIndividualClient(final Long clientId) { public ClientData retrieveIndividualClient(final Long clientId) {
try { try {
this.context.authenticatedUser(); this.context.authenticatedUser();
// TODO - JW include office name in query rather than get the lot // TODO - JW include office name in query rather than get the lot
// for office name // for office name
List<OfficeData> offices = new ArrayList<OfficeData>( List<OfficeData> offices = new ArrayList<OfficeData>(
officeReadPlatformService.retrieveAllOffices()); officeReadPlatformService.retrieveAllOffices());
ClientMapper rm = new ClientMapper(offices); ClientMapper rm = new ClientMapper(offices);
String sql = "select " + rm.clientSchema() + " where c.id = ? and c.is_deleted=0"; String sql = "select " + rm.clientSchema() + " where c.id = ? and c.is_deleted=0";
return this.jdbcTemplate.queryForObject(sql, rm, new Object[] {clientId}); return this.jdbcTemplate.queryForObject(sql, rm, new Object[] {clientId});
} catch (EmptyResultDataAccessException e) { } catch (EmptyResultDataAccessException e) {
throw new ClientNotFoundException(clientId); throw new ClientNotFoundException(clientId);
} }
} }
@Override @Override
public Collection<ClientLookup> retrieveAllIndividualClientsForLookup() { public Collection<ClientLookup> retrieveAllIndividualClientsForLookup() {
this.context.authenticatedUser(); this.context.authenticatedUser();
ClientLookupMapper rm = new ClientLookupMapper(); ClientLookupMapper rm = new ClientLookupMapper();
String sql = "select " + rm.clientLookupSchema() + " where c.is_deleted = 0"; String sql = "select " + rm.clientLookupSchema() + " where c.is_deleted = 0";
return this.jdbcTemplate.query(sql, rm, new Object[] {}); return this.jdbcTemplate.query(sql, rm, new Object[] {});
} }
@Override @Override
public ClientData retrieveNewClientDetails() { public ClientData retrieveNewClientDetails() {
AppUser currentUser = context.authenticatedUser(); AppUser currentUser = context.authenticatedUser();
List<OfficeLookup> offices = new ArrayList<OfficeLookup>( List<OfficeLookup> offices = new ArrayList<OfficeLookup>(
officeReadPlatformService.retrieveAllOfficesForLookup()); officeReadPlatformService.retrieveAllOfficesForLookup());
final Long officeId = currentUser.getOffice().getId(); final Long officeId = currentUser.getOffice().getId();
return new ClientData(officeId, new LocalDate(), offices); return new ClientData(officeId, new LocalDate(), offices);
} }
private static final class ClientMapper implements RowMapper<ClientData> { private static final class ClientMapper implements RowMapper<ClientData> {
private final List<OfficeData> offices; private final List<OfficeData> offices;
public ClientMapper(final List<OfficeData> offices) { public ClientMapper(final List<OfficeData> offices) {
this.offices = offices; this.offices = offices;
} }
public String clientSchema() { public String clientSchema() {
return "c.office_id as officeId, c.id as id, c.firstname as firstname, c.lastname as lastname, c.external_id as externalId, " + return "c.office_id as officeId, c.id as id, c.firstname as firstname, c.lastname as lastname, c.display_name as displayName, " +
"c.joining_date as joinedDate from m_client c"; "c.external_id as externalId, c.joining_date as joinedDate from m_client c";
} }
@Override @Override
public ClientData mapRow(final ResultSet rs, final int rowNum) public ClientData mapRow(final ResultSet rs, final int rowNum)
throws SQLException { throws SQLException {
Long officeId = JdbcSupport.getLong(rs, "officeId"); Long officeId = JdbcSupport.getLong(rs, "officeId");
Long id = JdbcSupport.getLong(rs, "id"); Long id = JdbcSupport.getLong(rs, "id");
String firstname = rs.getString("firstname"); String firstname = rs.getString("firstname");
if (StringUtils.isBlank(firstname)) { if (StringUtils.isBlank(firstname)) {
firstname = ""; firstname = "";
} }
String lastname = rs.getString("lastname"); String lastname = rs.getString("lastname");
String externalId = rs.getString("externalId"); String displayName = rs.getString("displayName");
LocalDate joinedDate = JdbcSupport.getLocalDate(rs, "joinedDate"); String externalId = rs.getString("externalId");
LocalDate joinedDate = JdbcSupport.getLocalDate(rs, "joinedDate");
String officeName = fromOfficeList(this.offices, officeId);
String officeName = fromOfficeList(this.offices, officeId);
return new ClientData(officeId, officeName, id, firstname,
lastname, externalId, joinedDate); return new ClientData(officeId, officeName, id, firstname,
} lastname, displayName, externalId, joinedDate);
}
private String fromOfficeList(final List<OfficeData> officeList,
final Long officeId) { private String fromOfficeList(final List<OfficeData> officeList,
String match = ""; final Long officeId) {
for (OfficeData office : officeList) { String match = "";
if (office.getId().equals(officeId)) { for (OfficeData office : officeList) {
match = office.getName(); if (office.getId().equals(officeId)) {
} match = office.getName();
} }
}
return match;
} return match;
} }
}
private static final class ClientLookupMapper implements RowMapper<ClientLookup>{
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 m_client c"; public String clientLookupSchema() {
} return "c.id as id, c.firstname as firstname, c.lastname as lastname from m_client c";
}
@Override
public ClientLookup mapRow(ResultSet rs, int rowNum) throws SQLException { @Override
Long id = rs.getLong("id"); public ClientLookup mapRow(ResultSet rs, int rowNum) throws SQLException {
String firstname = rs.getString("firstname"); Long id = rs.getLong("id");
String lastname = rs.getString("lastname"); String firstname = rs.getString("firstname");
if (StringUtils.isBlank(firstname)) { String lastname = rs.getString("lastname");
firstname = ""; if (StringUtils.isBlank(firstname)) {
} firstname = "";
return new ClientLookup(id, firstname, lastname); }
} return new ClientLookup(id, firstname, lastname);
} }
}
@Override
public ClientAccountSummaryCollectionData retrieveClientAccountDetails(final Long clientId) { @Override
public ClientAccountSummaryCollectionData retrieveClientAccountDetails(final Long clientId) {
try {
this.context.authenticatedUser(); try {
this.context.authenticatedUser();
// Check if client exists
retrieveIndividualClient(clientId); // Check if client exists
retrieveIndividualClient(clientId);
List<ClientAccountSummaryData> pendingApprovalLoans = new ArrayList<ClientAccountSummaryData>();
List<ClientAccountSummaryData> awaitingDisbursalLoans = new ArrayList<ClientAccountSummaryData>(); List<ClientAccountSummaryData> pendingApprovalLoans = new ArrayList<ClientAccountSummaryData>();
List<ClientAccountSummaryData> openLoans = new ArrayList<ClientAccountSummaryData>(); List<ClientAccountSummaryData> awaitingDisbursalLoans = new ArrayList<ClientAccountSummaryData>();
List<ClientAccountSummaryData> closedLoans = new ArrayList<ClientAccountSummaryData>(); List<ClientAccountSummaryData> openLoans = new ArrayList<ClientAccountSummaryData>();
List<ClientAccountSummaryData> closedLoans = new ArrayList<ClientAccountSummaryData>();
ClientLoanAccountSummaryDataMapper rm = new ClientLoanAccountSummaryDataMapper();
ClientLoanAccountSummaryDataMapper rm = new ClientLoanAccountSummaryDataMapper();
String sql = "select " + rm.loanAccountSummarySchema() + " where l.client_id = ?";
String sql = "select " + rm.loanAccountSummarySchema() + " where l.client_id = ?";
List<ClientAccountSummaryData> results = this.jdbcTemplate.query(sql, rm, new Object[] {clientId});
if (results != null) { List<ClientAccountSummaryData> results = this.jdbcTemplate.query(sql, rm, new Object[] {clientId});
for (ClientAccountSummaryData row : results) { if (results != null) {
for (ClientAccountSummaryData row : results) {
LoanStatusMapper statusMapper = new LoanStatusMapper(row.getAccountStatusId());
LoanStatusMapper statusMapper = new LoanStatusMapper(row.getAccountStatusId());
if (statusMapper.isOpen()) {
openLoans.add(row); if (statusMapper.isOpen()) {
} else if (statusMapper.isAwaitingDisbursal()) { openLoans.add(row);
awaitingDisbursalLoans.add(row); } else if (statusMapper.isAwaitingDisbursal()) {
} else if (statusMapper.isPendingApproval()) { awaitingDisbursalLoans.add(row);
pendingApprovalLoans.add(row); } else if (statusMapper.isPendingApproval()) {
} else { pendingApprovalLoans.add(row);
closedLoans.add(row); } else {
} closedLoans.add(row);
} }
} }
}
List<ClientAccountSummaryData> pendingApprovalDepositAccounts = new ArrayList<ClientAccountSummaryData>();
List<ClientAccountSummaryData> approvedDepositAccounts = new ArrayList<ClientAccountSummaryData>(); List<ClientAccountSummaryData> pendingApprovalDepositAccounts = new ArrayList<ClientAccountSummaryData>();
// List<ClientLoanAccountSummaryData> openDepositAccounts = new ArrayList<ClientLoanAccountSummaryData>(); List<ClientAccountSummaryData> approvedDepositAccounts = new ArrayList<ClientAccountSummaryData>();
// List<ClientLoanAccountSummaryData> closedDepositAccounts = new ArrayList<ClientLoanAccountSummaryData>(); // List<ClientLoanAccountSummaryData> openDepositAccounts = new ArrayList<ClientLoanAccountSummaryData>();
// List<ClientLoanAccountSummaryData> closedDepositAccounts = new ArrayList<ClientLoanAccountSummaryData>();
ClientDespoitAccountSummaryDataMapper depositAccountMapper = new ClientDespoitAccountSummaryDataMapper();
ClientDespoitAccountSummaryDataMapper depositAccountMapper = new ClientDespoitAccountSummaryDataMapper();
String depositAccountsSql = "select " + depositAccountMapper.schema() + " where da.client_id = ? and da.is_deleted=0";
List<ClientAccountSummaryData> depositAccountResults = this.jdbcTemplate.query(depositAccountsSql, depositAccountMapper, new Object[] {clientId}); String depositAccountsSql = "select " + depositAccountMapper.schema() + " where da.client_id = ? and da.is_deleted=0";
if (depositAccountResults != null) { List<ClientAccountSummaryData> depositAccountResults = this.jdbcTemplate.query(depositAccountsSql, depositAccountMapper, new Object[] {clientId});
for (ClientAccountSummaryData row : depositAccountResults) { if (depositAccountResults != null) {
for (ClientAccountSummaryData row : depositAccountResults) {
if (row.getAccountStatusId() == 100) {
pendingApprovalDepositAccounts.add(row); if (row.getAccountStatusId() == 100) {
} else if (row.getAccountStatusId() == 200) { pendingApprovalDepositAccounts.add(row);
approvedDepositAccounts.add(row); } else if (row.getAccountStatusId() == 200) {
} approvedDepositAccounts.add(row);
} }
} }
}
return new ClientAccountSummaryCollectionData(pendingApprovalLoans, awaitingDisbursalLoans, openLoans, closedLoans,
pendingApprovalDepositAccounts, approvedDepositAccounts); return new ClientAccountSummaryCollectionData(pendingApprovalLoans, awaitingDisbursalLoans, openLoans, closedLoans,
} catch (EmptyResultDataAccessException e) { pendingApprovalDepositAccounts, approvedDepositAccounts);
throw new ClientNotFoundException(clientId); } catch (EmptyResultDataAccessException e) {
} throw new ClientNotFoundException(clientId);
} }
}
private static final class ClientLoanAccountSummaryDataMapper implements
RowMapper<ClientAccountSummaryData> { private static final class ClientLoanAccountSummaryDataMapper implements
RowMapper<ClientAccountSummaryData> {
public String loanAccountSummarySchema() {
public String loanAccountSummarySchema() {
StringBuilder accountsSummary = new StringBuilder("l.id as id, l.external_id as externalId,");
accountsSummary StringBuilder accountsSummary = new StringBuilder("l.id as id, l.external_id as externalId,");
.append("l.product_id as productId, lp.name as productName,") accountsSummary
.append("l.loan_status_id as statusId ") .append("l.product_id as productId, lp.name as productName,")
.append("from m_loan l ") .append("l.loan_status_id as statusId ")
.append("LEFT JOIN m_product_loan AS lp ON lp.id = l.product_id "); .append("from m_loan l ")
.append("LEFT JOIN m_product_loan AS lp ON lp.id = l.product_id ");
return accountsSummary.toString();
} return accountsSummary.toString();
}
@Override
public ClientAccountSummaryData mapRow(final ResultSet rs, @Override
final int rowNum) throws SQLException { public ClientAccountSummaryData mapRow(final ResultSet rs,
final int rowNum) throws SQLException {
Long id = JdbcSupport.getLong(rs, "id");
String externalId = rs.getString("externalId"); Long id = JdbcSupport.getLong(rs, "id");
Long productId = JdbcSupport.getLong(rs, "productId"); String externalId = rs.getString("externalId");
String loanProductName = rs.getString("productName"); Long productId = JdbcSupport.getLong(rs, "productId");
Integer loanStatusId = JdbcSupport.getInteger(rs, "statusId"); String loanProductName = rs.getString("productName");
Integer loanStatusId = JdbcSupport.getInteger(rs, "statusId");
return new ClientAccountSummaryData(id, externalId, productId, loanProductName, loanStatusId);
} return new ClientAccountSummaryData(id, externalId, productId, loanProductName, loanStatusId);
} }
}
private static final class ClientDespoitAccountSummaryDataMapper implements RowMapper<ClientAccountSummaryData> {
private static final class ClientDespoitAccountSummaryDataMapper implements RowMapper<ClientAccountSummaryData> {
public String schema() {
public String schema() {
StringBuilder accountsSummary = new StringBuilder("da.id as id, da.external_id as externalId,");
accountsSummary.append("da.product_id as productId, dp.name as productName,") StringBuilder accountsSummary = new StringBuilder("da.id as id, da.external_id as externalId,");
.append("da.status_enum as statusId ") accountsSummary.append("da.product_id as productId, dp.name as productName,")
.append("from m_deposit_account da ") .append("da.status_enum as statusId ")
.append("LEFT JOIN m_product_deposit AS dp ON dp.id = da.product_id "); .append("from m_deposit_account da ")
.append("LEFT JOIN m_product_deposit AS dp ON dp.id = da.product_id ");
return accountsSummary.toString();
} return accountsSummary.toString();
}
@Override
public ClientAccountSummaryData mapRow(final ResultSet rs, @Override
final int rowNum) throws SQLException { public ClientAccountSummaryData mapRow(final ResultSet rs,
final int rowNum) throws SQLException {
Long id = JdbcSupport.getLong(rs, "id");
String externalId = rs.getString("externalId"); Long id = JdbcSupport.getLong(rs, "id");
Long productId = JdbcSupport.getLong(rs, "productId"); String externalId = rs.getString("externalId");
String productName = rs.getString("productName"); Long productId = JdbcSupport.getLong(rs, "productId");
Integer accountStatusId = JdbcSupport.getInteger(rs, "statusId"); String productName = rs.getString("productName");
Integer accountStatusId = JdbcSupport.getInteger(rs, "statusId");
return new ClientAccountSummaryData(id, externalId, productId, productName, accountStatusId);
} return new ClientAccountSummaryData(id, externalId, productId, productName, accountStatusId);
} }
}
@Override
public NoteData retrieveClientNote(Long clientId, Long noteId) { @Override
public NoteData retrieveClientNote(Long clientId, Long noteId) {
try {
context.authenticatedUser(); try {
context.authenticatedUser();
// Check if client exists
retrieveIndividualClient(clientId); // Check if client exists
retrieveIndividualClient(clientId);
// FIXME - use join on sql query to fetch user information for note
// rather than fetching users? // FIXME - use join on sql query to fetch user information for note
Collection<AppUserData> allUsers = this.appUserReadPlatformService // rather than fetching users?
.retrieveAllUsers(); Collection<AppUserData> allUsers = this.appUserReadPlatformService
.retrieveAllUsers();
NoteMapper noteMapper = new NoteMapper(allUsers);
NoteMapper noteMapper = new NoteMapper(allUsers);
String sql = "select " + noteMapper.schema()
+ " where n.client_id = ? and n.id = ?"; String sql = "select " + noteMapper.schema()
+ " where n.client_id = ? and n.id = ?";
return this.jdbcTemplate.queryForObject(sql, noteMapper, new Object[] {clientId, noteId});
} catch (EmptyResultDataAccessException e) { return this.jdbcTemplate.queryForObject(sql, noteMapper, new Object[] {clientId, noteId});
throw new NoteNotFoundException(clientId); } catch (EmptyResultDataAccessException e) {
} throw new NoteNotFoundException(clientId);
} }
}
@Override
public Collection<NoteData> retrieveAllClientNotes(Long clientId) { @Override
public Collection<NoteData> retrieveAllClientNotes(Long clientId) {
context.authenticatedUser();
context.authenticatedUser();
// Check if client exists
retrieveIndividualClient(clientId); // Check if client exists
retrieveIndividualClient(clientId);
// FIXME - use join on sql query to fetch user information for note
// rather than fetching users? // FIXME - use join on sql query to fetch user information for note
Collection<AppUserData> allUsers = this.appUserReadPlatformService // rather than fetching users?
.retrieveAllUsers(); Collection<AppUserData> allUsers = this.appUserReadPlatformService
.retrieveAllUsers();
NoteMapper noteMapper = new NoteMapper(allUsers);
NoteMapper noteMapper = new NoteMapper(allUsers);
String sql = "select "
+ noteMapper.schema() String sql = "select "
+ " where n.client_id = ? order by n.created_date DESC"; + noteMapper.schema()
+ " where n.client_id = ? order by n.created_date DESC";
return this.jdbcTemplate.query(sql, noteMapper, new Object[] {clientId});
} return this.jdbcTemplate.query(sql, noteMapper, new Object[] {clientId});
}
private static final class NoteMapper implements RowMapper<NoteData> {
private static final class NoteMapper implements RowMapper<NoteData> {
private final Collection<AppUserData> allUsers;
private final Collection<AppUserData> allUsers;
public NoteMapper(Collection<AppUserData> allUsers) {
this.allUsers = allUsers; public NoteMapper(Collection<AppUserData> allUsers) {
} this.allUsers = allUsers;
}
public String schema() {
return "n.id as id, n.client_id as clientId, n.loan_id as loanId, n.loan_transaction_id as transactionId, n.note_type_enum as noteTypeEnum, n.note as note, " public String schema() {
+ "n.created_date as createdDate, n.createdby_id as createdById, n.lastmodified_date as lastModifiedDate, n.lastmodifiedby_id as lastModifiedById" return "n.id as id, n.client_id as clientId, n.loan_id as loanId, n.loan_transaction_id as transactionId, n.note_type_enum as noteTypeEnum, n.note as note, "
+ " from m_note n"; + "n.created_date as createdDate, n.createdby_id as createdById, n.lastmodified_date as lastModifiedDate, n.lastmodifiedby_id as lastModifiedById"
} + " from m_note n";
}
@Override
public NoteData mapRow(final ResultSet rs, final int rowNum) @Override
throws SQLException { public NoteData mapRow(final ResultSet rs, final int rowNum)
throws SQLException {
Long id = JdbcSupport.getLong(rs, "id");
Long clientId = JdbcSupport.getLong(rs, "clientId"); Long id = JdbcSupport.getLong(rs, "id");
Long loanId = JdbcSupport.getLong(rs, "loanId"); Long clientId = JdbcSupport.getLong(rs, "clientId");
Long transactionId = JdbcSupport.getLong(rs, "transactionId"); Long loanId = JdbcSupport.getLong(rs, "loanId");
Integer noteTypeId = JdbcSupport.getInteger(rs, "noteTypeEnum"); Long transactionId = JdbcSupport.getLong(rs, "transactionId");
EnumOptionData noteType = NoteEnumerations.noteType(noteTypeId); Integer noteTypeId = JdbcSupport.getInteger(rs, "noteTypeEnum");
String note = rs.getString("note"); EnumOptionData noteType = NoteEnumerations.noteType(noteTypeId);
String note = rs.getString("note");
DateTime createdDate = JdbcSupport.getDateTime(rs, "createdDate");
Long createdById = JdbcSupport.getLong(rs, "createdById"); DateTime createdDate = JdbcSupport.getDateTime(rs, "createdDate");
String createdByUsername = findUserById(createdById, allUsers); Long createdById = JdbcSupport.getLong(rs, "createdById");
String createdByUsername = findUserById(createdById, allUsers);
DateTime lastModifiedDate = JdbcSupport.getDateTime(rs,
"lastModifiedDate"); DateTime lastModifiedDate = JdbcSupport.getDateTime(rs,
Long lastModifiedById = JdbcSupport.getLong(rs, "lastModifiedById"); "lastModifiedDate");
String updatedByUsername = findUserById(createdById, allUsers); Long lastModifiedById = JdbcSupport.getLong(rs, "lastModifiedById");
String updatedByUsername = findUserById(createdById, allUsers);
return new NoteData(id, clientId, loanId, transactionId, noteType,
note, createdDate, createdById, createdByUsername, return new NoteData(id, clientId, loanId, transactionId, noteType,
lastModifiedDate, lastModifiedById, updatedByUsername); note, createdDate, createdById, createdByUsername,
} lastModifiedDate, lastModifiedById, updatedByUsername);
}
private String findUserById(Long createdById, Collection<AppUserData> allUsers) {
String username = ""; private String findUserById(Long createdById, Collection<AppUserData> allUsers) {
for (AppUserData appUserData : allUsers) { String username = "";
if (appUserData.getId().equals(createdById)) { for (AppUserData appUserData : allUsers) {
username = appUserData.getUsername(); if (appUserData.getId().equals(createdById)) {
break; username = appUserData.getUsername();
} break;
} }
return username; }
} return username;
} }
}
} }