change client query to link to m_office rather than match a list of offices

This commit is contained in:
John Woodlock
2012-09-12 21:40:29 +10:00
parent 99b53995ea
commit 4c752d7f23
@@ -1,409 +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.display_name as displayName, " + return "c.office_id as officeId, c.id as id, c.firstname as firstname, c.lastname as lastname, c.display_name as displayName, " +
"c.external_id as externalId, 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 displayName = rs.getString("displayName"); String displayName = rs.getString("displayName");
String externalId = rs.getString("externalId"); String externalId = rs.getString("externalId");
LocalDate joinedDate = JdbcSupport.getLocalDate(rs, "joinedDate"); 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, return new ClientData(officeId, officeName, id, firstname,
lastname, displayName, externalId, joinedDate); lastname, displayName, externalId, joinedDate);
} }
private String fromOfficeList(final List<OfficeData> officeList, private String fromOfficeList(final List<OfficeData> officeList,
final Long officeId) { final Long officeId) {
String match = ""; String match = "";
for (OfficeData office : officeList) { for (OfficeData office : officeList) {
if (office.getId().equals(officeId)) { if (office.getId().equals(officeId)) {
match = office.getName(); 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() { public String clientLookupSchema() {
return "c.id as id, c.firstname as firstname, c.lastname as lastname from m_client c"; return "c.id as id, c.firstname as firstname, c.lastname as lastname from m_client c";
} }
@Override @Override
public ClientLookup mapRow(ResultSet rs, int rowNum) throws SQLException { public ClientLookup mapRow(ResultSet rs, int rowNum) throws SQLException {
Long id = rs.getLong("id"); Long id = rs.getLong("id");
String firstname = rs.getString("firstname"); String firstname = rs.getString("firstname");
String lastname = rs.getString("lastname"); String lastname = rs.getString("lastname");
if (StringUtils.isBlank(firstname)) { if (StringUtils.isBlank(firstname)) {
firstname = ""; firstname = "";
} }
return new ClientLookup(id, firstname, lastname); return new ClientLookup(id, firstname, lastname);
} }
} }
@Override @Override
public ClientAccountSummaryCollectionData retrieveClientAccountDetails(final Long clientId) { public ClientAccountSummaryCollectionData retrieveClientAccountDetails(final Long clientId) {
try { try {
this.context.authenticatedUser(); this.context.authenticatedUser();
// Check if client exists // Check if client exists
retrieveIndividualClient(clientId); retrieveIndividualClient(clientId);
List<ClientAccountSummaryData> pendingApprovalLoans = new ArrayList<ClientAccountSummaryData>(); List<ClientAccountSummaryData> pendingApprovalLoans = new ArrayList<ClientAccountSummaryData>();
List<ClientAccountSummaryData> awaitingDisbursalLoans = new ArrayList<ClientAccountSummaryData>(); List<ClientAccountSummaryData> awaitingDisbursalLoans = new ArrayList<ClientAccountSummaryData>();
List<ClientAccountSummaryData> openLoans = new ArrayList<ClientAccountSummaryData>(); List<ClientAccountSummaryData> openLoans = new ArrayList<ClientAccountSummaryData>();
List<ClientAccountSummaryData> closedLoans = 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}); List<ClientAccountSummaryData> results = this.jdbcTemplate.query(sql, rm, new Object[] {clientId});
if (results != null) { if (results != null) {
for (ClientAccountSummaryData row : results) { for (ClientAccountSummaryData row : results) {
LoanStatusMapper statusMapper = new LoanStatusMapper(row.getAccountStatusId()); LoanStatusMapper statusMapper = new LoanStatusMapper(row.getAccountStatusId());
if (statusMapper.isOpen()) { if (statusMapper.isOpen()) {
openLoans.add(row); openLoans.add(row);
} else if (statusMapper.isAwaitingDisbursal()) { } else if (statusMapper.isAwaitingDisbursal()) {
awaitingDisbursalLoans.add(row); awaitingDisbursalLoans.add(row);
} else if (statusMapper.isPendingApproval()) { } else if (statusMapper.isPendingApproval()) {
pendingApprovalLoans.add(row); pendingApprovalLoans.add(row);
} else { } else {
closedLoans.add(row); closedLoans.add(row);
} }
} }
} }
List<ClientAccountSummaryData> pendingApprovalDepositAccounts = new ArrayList<ClientAccountSummaryData>(); List<ClientAccountSummaryData> pendingApprovalDepositAccounts = new ArrayList<ClientAccountSummaryData>();
List<ClientAccountSummaryData> approvedDepositAccounts = new ArrayList<ClientAccountSummaryData>(); List<ClientAccountSummaryData> approvedDepositAccounts = new ArrayList<ClientAccountSummaryData>();
// List<ClientLoanAccountSummaryData> openDepositAccounts = new ArrayList<ClientLoanAccountSummaryData>(); // List<ClientLoanAccountSummaryData> openDepositAccounts = new ArrayList<ClientLoanAccountSummaryData>();
// List<ClientLoanAccountSummaryData> closedDepositAccounts = 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"; 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}); List<ClientAccountSummaryData> depositAccountResults = this.jdbcTemplate.query(depositAccountsSql, depositAccountMapper, new Object[] {clientId});
if (depositAccountResults != null) { if (depositAccountResults != null) {
for (ClientAccountSummaryData row : depositAccountResults) { for (ClientAccountSummaryData row : depositAccountResults) {
if (row.getAccountStatusId() == 100) { if (row.getAccountStatusId() == 100) {
pendingApprovalDepositAccounts.add(row); pendingApprovalDepositAccounts.add(row);
} else if (row.getAccountStatusId() == 200) { } else if (row.getAccountStatusId() == 200) {
approvedDepositAccounts.add(row); approvedDepositAccounts.add(row);
} }
} }
} }
return new ClientAccountSummaryCollectionData(pendingApprovalLoans, awaitingDisbursalLoans, openLoans, closedLoans, return new ClientAccountSummaryCollectionData(pendingApprovalLoans, awaitingDisbursalLoans, openLoans, closedLoans,
pendingApprovalDepositAccounts, approvedDepositAccounts); pendingApprovalDepositAccounts, approvedDepositAccounts);
} catch (EmptyResultDataAccessException e) { } catch (EmptyResultDataAccessException e) {
throw new ClientNotFoundException(clientId); throw new ClientNotFoundException(clientId);
} }
} }
private static final class ClientLoanAccountSummaryDataMapper implements private static final class ClientLoanAccountSummaryDataMapper implements
RowMapper<ClientAccountSummaryData> { RowMapper<ClientAccountSummaryData> {
public String loanAccountSummarySchema() { public String loanAccountSummarySchema() {
StringBuilder accountsSummary = new StringBuilder("l.id as id, l.external_id as externalId,"); StringBuilder accountsSummary = new StringBuilder("l.id as id, l.external_id as externalId,");
accountsSummary accountsSummary
.append("l.product_id as productId, lp.name as productName,") .append("l.product_id as productId, lp.name as productName,")
.append("l.loan_status_id as statusId ") .append("l.loan_status_id as statusId ")
.append("from m_loan l ") .append("from m_loan l ")
.append("LEFT JOIN m_product_loan AS lp ON lp.id = l.product_id "); .append("LEFT JOIN m_product_loan AS lp ON lp.id = l.product_id ");
return accountsSummary.toString(); return accountsSummary.toString();
} }
@Override @Override
public ClientAccountSummaryData mapRow(final ResultSet rs, public ClientAccountSummaryData mapRow(final ResultSet rs,
final int rowNum) throws SQLException { final int rowNum) throws SQLException {
Long id = JdbcSupport.getLong(rs, "id"); Long id = JdbcSupport.getLong(rs, "id");
String externalId = rs.getString("externalId"); String externalId = rs.getString("externalId");
Long productId = JdbcSupport.getLong(rs, "productId"); Long productId = JdbcSupport.getLong(rs, "productId");
String loanProductName = rs.getString("productName"); String loanProductName = rs.getString("productName");
Integer loanStatusId = JdbcSupport.getInteger(rs, "statusId"); 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,"); StringBuilder accountsSummary = new StringBuilder("da.id as id, da.external_id as externalId,");
accountsSummary.append("da.product_id as productId, dp.name as productName,") accountsSummary.append("da.product_id as productId, dp.name as productName,")
.append("da.status_enum as statusId ") .append("da.status_enum as statusId ")
.append("from m_deposit_account da ") .append("from m_deposit_account da ")
.append("LEFT JOIN m_product_deposit AS dp ON dp.id = da.product_id "); .append("LEFT JOIN m_product_deposit AS dp ON dp.id = da.product_id ");
return accountsSummary.toString(); return accountsSummary.toString();
} }
@Override @Override
public ClientAccountSummaryData mapRow(final ResultSet rs, public ClientAccountSummaryData mapRow(final ResultSet rs,
final int rowNum) throws SQLException { final int rowNum) throws SQLException {
Long id = JdbcSupport.getLong(rs, "id"); Long id = JdbcSupport.getLong(rs, "id");
String externalId = rs.getString("externalId"); String externalId = rs.getString("externalId");
Long productId = JdbcSupport.getLong(rs, "productId"); Long productId = JdbcSupport.getLong(rs, "productId");
String productName = rs.getString("productName"); String productName = rs.getString("productName");
Integer accountStatusId = JdbcSupport.getInteger(rs, "statusId"); Integer accountStatusId = JdbcSupport.getInteger(rs, "statusId");
return new ClientAccountSummaryData(id, externalId, productId, productName, accountStatusId); return new ClientAccountSummaryData(id, externalId, productId, productName, accountStatusId);
} }
} }
@Override @Override
public NoteData retrieveClientNote(Long clientId, Long noteId) { public NoteData retrieveClientNote(Long clientId, Long noteId) {
try { try {
context.authenticatedUser(); context.authenticatedUser();
// Check if client exists // Check if client exists
retrieveIndividualClient(clientId); retrieveIndividualClient(clientId);
// FIXME - use join on sql query to fetch user information for note // FIXME - use join on sql query to fetch user information for note
// rather than fetching users? // rather than fetching users?
Collection<AppUserData> allUsers = this.appUserReadPlatformService Collection<AppUserData> allUsers = this.appUserReadPlatformService
.retrieveAllUsers(); .retrieveAllUsers();
NoteMapper noteMapper = new NoteMapper(allUsers); NoteMapper noteMapper = new NoteMapper(allUsers);
String sql = "select " + noteMapper.schema() String sql = "select " + noteMapper.schema()
+ " where n.client_id = ? and n.id = ?"; + " where n.client_id = ? and n.id = ?";
return this.jdbcTemplate.queryForObject(sql, noteMapper, new Object[] {clientId, noteId}); return this.jdbcTemplate.queryForObject(sql, noteMapper, new Object[] {clientId, noteId});
} catch (EmptyResultDataAccessException e) { } catch (EmptyResultDataAccessException e) {
throw new NoteNotFoundException(clientId); throw new NoteNotFoundException(clientId);
} }
} }
@Override @Override
public Collection<NoteData> retrieveAllClientNotes(Long clientId) { public Collection<NoteData> retrieveAllClientNotes(Long clientId) {
context.authenticatedUser(); context.authenticatedUser();
// Check if client exists // Check if client exists
retrieveIndividualClient(clientId); retrieveIndividualClient(clientId);
// FIXME - use join on sql query to fetch user information for note // FIXME - use join on sql query to fetch user information for note
// rather than fetching users? // rather than fetching users?
Collection<AppUserData> allUsers = this.appUserReadPlatformService Collection<AppUserData> allUsers = this.appUserReadPlatformService
.retrieveAllUsers(); .retrieveAllUsers();
NoteMapper noteMapper = new NoteMapper(allUsers); NoteMapper noteMapper = new NoteMapper(allUsers);
String sql = "select " String sql = "select "
+ noteMapper.schema() + noteMapper.schema()
+ " where n.client_id = ? order by n.created_date DESC"; + " 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) { public NoteMapper(Collection<AppUserData> allUsers) {
this.allUsers = allUsers; this.allUsers = allUsers;
} }
public String schema() { 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, " 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, "
+ "n.created_date as createdDate, n.createdby_id as createdById, n.lastmodified_date as lastModifiedDate, n.lastmodifiedby_id as lastModifiedById" + "n.created_date as createdDate, n.createdby_id as createdById, n.lastmodified_date as lastModifiedDate, n.lastmodifiedby_id as lastModifiedById"
+ " from m_note n"; + " from m_note n";
} }
@Override @Override
public NoteData mapRow(final ResultSet rs, final int rowNum) public NoteData mapRow(final ResultSet rs, final int rowNum)
throws SQLException { throws SQLException {
Long id = JdbcSupport.getLong(rs, "id"); Long id = JdbcSupport.getLong(rs, "id");
Long clientId = JdbcSupport.getLong(rs, "clientId"); Long clientId = JdbcSupport.getLong(rs, "clientId");
Long loanId = JdbcSupport.getLong(rs, "loanId"); Long loanId = JdbcSupport.getLong(rs, "loanId");
Long transactionId = JdbcSupport.getLong(rs, "transactionId"); Long transactionId = JdbcSupport.getLong(rs, "transactionId");
Integer noteTypeId = JdbcSupport.getInteger(rs, "noteTypeEnum"); Integer noteTypeId = JdbcSupport.getInteger(rs, "noteTypeEnum");
EnumOptionData noteType = NoteEnumerations.noteType(noteTypeId); EnumOptionData noteType = NoteEnumerations.noteType(noteTypeId);
String note = rs.getString("note"); String note = rs.getString("note");
DateTime createdDate = JdbcSupport.getDateTime(rs, "createdDate"); DateTime createdDate = JdbcSupport.getDateTime(rs, "createdDate");
Long createdById = JdbcSupport.getLong(rs, "createdById"); Long createdById = JdbcSupport.getLong(rs, "createdById");
String createdByUsername = findUserById(createdById, allUsers); String createdByUsername = findUserById(createdById, allUsers);
DateTime lastModifiedDate = JdbcSupport.getDateTime(rs, DateTime lastModifiedDate = JdbcSupport.getDateTime(rs,
"lastModifiedDate"); "lastModifiedDate");
Long lastModifiedById = JdbcSupport.getLong(rs, "lastModifiedById"); Long lastModifiedById = JdbcSupport.getLong(rs, "lastModifiedById");
String updatedByUsername = findUserById(createdById, allUsers); String updatedByUsername = findUserById(createdById, allUsers);
return new NoteData(id, clientId, loanId, transactionId, noteType, return new NoteData(id, clientId, loanId, transactionId, noteType,
note, createdDate, createdById, createdByUsername, note, createdDate, createdById, createdByUsername,
lastModifiedDate, lastModifiedById, updatedByUsername); lastModifiedDate, lastModifiedById, updatedByUsername);
} }
private String findUserById(Long createdById, Collection<AppUserData> allUsers) { private String findUserById(Long createdById, Collection<AppUserData> allUsers) {
String username = ""; String username = "";
for (AppUserData appUserData : allUsers) { for (AppUserData appUserData : allUsers) {
if (appUserData.getId().equals(createdById)) { if (appUserData.getId().equals(createdById)) {
username = appUserData.getUsername(); username = appUserData.getUsername();
break; break;
} }
} }
return username; return username;
} }
} }
} }