add JdbcSupport class for retrieving long and integer values with possible nulls.

This commit is contained in:
Keith Woodlock
2012-05-27 22:00:09 +01:00
parent cc8bd8ba5e
commit 1465f24298
4 changed files with 64 additions and 60 deletions
@@ -17,8 +17,6 @@ public class LoanProductData implements Serializable {
private Long id; private Long id;
private String name; private String name;
private String description; private String description;
private boolean isFlexible;
private boolean isInterestRebateAllowed;
private MoneyData principalMoney; private MoneyData principalMoney;
private BigDecimal interestRatePerPeriod = BigDecimal.ZERO; private BigDecimal interestRatePerPeriod = BigDecimal.ZERO;
private int interestRatePeriod; private int interestRatePeriod;
@@ -47,8 +45,7 @@ public class LoanProductData implements Serializable {
} }
public LoanProductData(final Long id, final String name, public LoanProductData(final Long id, final String name,
String description, final boolean isFlexible, String description,
final boolean isInterestRebateAllowed,
final MoneyData principalMoney, final MoneyData principalMoney,
final BigDecimal interestRatePerPeriod, final BigDecimal interestRatePerPeriod,
final int interestRatePeriod, final BigDecimal annualInterestRate, final int interestRatePeriod, final BigDecimal annualInterestRate,
@@ -59,8 +56,6 @@ public class LoanProductData implements Serializable {
this.id = id; this.id = id;
this.name = name; this.name = name;
this.description = description; this.description = description;
this.isFlexible = isFlexible;
this.isInterestRebateAllowed = isInterestRebateAllowed;
this.principalMoney = principalMoney; this.principalMoney = principalMoney;
this.interestRatePerPeriod = interestRatePerPeriod; this.interestRatePerPeriod = interestRatePerPeriod;
this.interestRatePeriod = interestRatePeriod; this.interestRatePeriod = interestRatePeriod;
@@ -100,22 +95,6 @@ public class LoanProductData implements Serializable {
this.description = description; this.description = description;
} }
public boolean isFlexible() {
return isFlexible;
}
public void setFlexible(boolean isFlexible) {
this.isFlexible = isFlexible;
}
public boolean isInterestRebateAllowed() {
return isInterestRebateAllowed;
}
public void setInterestRebateAllowed(boolean isInterestRebateAllowed) {
this.isInterestRebateAllowed = isInterestRebateAllowed;
}
public MoneyData getPrincipalMoney() { public MoneyData getPrincipalMoney() {
return principalMoney; return principalMoney;
} }
@@ -0,0 +1,42 @@
package org.mifosng.platform.infrastructure;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.springframework.jdbc.support.JdbcUtils;
/**
* Support for retrieving possibly null values from jdbc recordset delegating to springs {@link JdbcUtils} where possible.
*/
public class JdbcSupport {
public static DateTime getDateTime(ResultSet rs, String columnName) throws SQLException {
DateTime dateTime = null;
Timestamp dateValue = rs.getTimestamp(columnName);
if (dateValue != null) {
dateTime = new DateTime(dateValue.getTime());
}
return dateTime;
}
public static LocalDate getLocalDate(final ResultSet rs, final String columnName) throws SQLException {
LocalDate localDate = null;
Date dateValue = rs.getDate(columnName);
if (dateValue != null) {
localDate = new LocalDate(dateValue);
}
return localDate;
}
public static Long getLong(final ResultSet rs, final String columnName) throws SQLException {
return (Long) JdbcUtils.getResultSetValue(rs, rs.findColumn(columnName), Long.class);
}
public static Integer getInteger(ResultSet rs, String columnName) throws SQLException {
return (Integer) JdbcUtils.getResultSetValue(rs, rs.findColumn(columnName), Integer.class);
}
}
@@ -15,6 +15,7 @@ import org.mifosng.data.LoanProductData;
import org.mifosng.data.MoneyData; import org.mifosng.data.MoneyData;
import org.mifosng.platform.currency.service.CurrencyReadPlatformService; import org.mifosng.platform.currency.service.CurrencyReadPlatformService;
import org.mifosng.platform.exceptions.PlatformResourceNotFoundException; import org.mifosng.platform.exceptions.PlatformResourceNotFoundException;
import org.mifosng.platform.infrastructure.JdbcSupport;
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.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -126,12 +127,9 @@ public class LoanProductReadPlatformServiceImpl implements LoanProductReadPlatfo
Long id = rs.getLong("id"); Long id = rs.getLong("id");
String name = rs.getString("name"); String name = rs.getString("name");
String description = rs.getString("description"); String description = rs.getString("description");
boolean isFlexible = rs.getBoolean("isFlexible");
boolean isInterestRebateAllowed = rs
.getBoolean("isInterestRebateAllowed");
String currencyCode = rs.getString("currencyCode"); String currencyCode = rs.getString("currencyCode");
int currencyDigits = rs.getInt("currencyDigits"); Integer currencyDigits = JdbcSupport.getInteger(rs, "currencyDigits");
CurrencyData currencyData = findCurrencyByCode(currencyCode, allowedCurrencies); CurrencyData currencyData = findCurrencyByCode(currencyCode, allowedCurrencies);
if (currencyData != null) { if (currencyData != null) {
@@ -144,25 +142,21 @@ public class LoanProductReadPlatformServiceImpl implements LoanProductReadPlatfo
MoneyData principalMoney = MoneyData.of(currencyData, principal); MoneyData principalMoney = MoneyData.of(currencyData, principal);
MoneyData toleranceMoney = MoneyData.of(currencyData, tolerance); MoneyData toleranceMoney = MoneyData.of(currencyData, tolerance);
BigDecimal interestRatePerPeriod = rs.getBigDecimal("interestRatePerPeriod");
Integer interestRatePeriod = JdbcSupport.getInteger(rs, "interestRatePerPeriodFreq");
BigDecimal annualInterestRate = rs.getBigDecimal("annualInterestRate");
Integer interestMethod = JdbcSupport.getInteger(rs, "interestMethod");
Integer interestCalculationInPeriodMethod = JdbcSupport.getInteger(rs, "interestCalculationInPeriodMethod");
BigDecimal interestRatePerPeriod = rs Integer repaidEvery = JdbcSupport.getInteger(rs, "repaidEvery");
.getBigDecimal("interestRatePerPeriod"); Integer repaymentFrequency = JdbcSupport.getInteger(rs, "repaymentPeriodFrequency");
int interestRatePeriod = rs.getInt("interestRatePerPeriodFreq"); Integer numberOfRepayments = JdbcSupport.getInteger(rs, "numberOfRepayments");
BigDecimal annualInterestRate = rs Integer amortizationMethod = JdbcSupport.getInteger(rs, "amortizationMethod");
.getBigDecimal("annualInterestRate");
int interestMethod = rs.getInt("interestMethod");
int interestCalculationInPeriodMethod = rs.getInt("interestCalculationInPeriodMethod");
int repaidEvery = rs.getInt("repaidEvery"); DateTime createdOn = JdbcSupport.getDateTime(rs, "createdon");
int repaymentFrequency = rs.getInt("repaymentPeriodFrequency"); DateTime lastModifedOn = JdbcSupport.getDateTime(rs, "modifiedon");
int numberOfRepayments = rs.getInt("numberOfRepayments");
int amortizationMethod = rs.getInt("amortizationMethod");
DateTime createdOn = new DateTime(rs.getDate("createdOn")); return new LoanProductData(id, name, description, principalMoney,
DateTime lastModifedOn = new DateTime(rs.getDate("modifiedon"));
return new LoanProductData(id, name, description, isFlexible,
isInterestRebateAllowed, principalMoney,
interestRatePerPeriod, interestRatePeriod, interestRatePerPeriod, interestRatePeriod,
annualInterestRate, interestMethod, interestCalculationInPeriodMethod, annualInterestRate, interestMethod, interestCalculationInPeriodMethod,
repaidEvery, repaidEvery,
@@ -12,6 +12,7 @@ import org.joda.time.LocalDate;
import org.mifosng.data.OfficeData; import org.mifosng.data.OfficeData;
import org.mifosng.data.OfficeLookup; import org.mifosng.data.OfficeLookup;
import org.mifosng.platform.exceptions.PlatformResourceNotFoundException; import org.mifosng.platform.exceptions.PlatformResourceNotFoundException;
import org.mifosng.platform.infrastructure.JdbcSupport;
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.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -47,20 +48,16 @@ public class OfficeReadPlatformServiceImpl implements OfficeReadPlatformService
Long id = rs.getLong("id"); Long id = rs.getLong("id");
String name = rs.getString("name"); String name = rs.getString("name");
String externalId = rs.getString("externalId"); String externalId = rs.getString("externalId");
LocalDate openingDate = new LocalDate(rs.getDate("openingDate")); LocalDate openingDate = JdbcSupport.getLocalDate(rs, "openingDate");
String hierarchy = rs.getString("hierarchy"); String hierarchy = rs.getString("hierarchy");
Long parentId = JdbcSupport.getLong(rs, "parentId");
Long parentId = getRSLong(rs.getString("parentId"), rs.getLong("parentId"));
String parentName = rs.getString("parentName"); String parentName = rs.getString("parentName");
return new OfficeData(id, name, externalId, openingDate, hierarchy, return new OfficeData(id, name, externalId, openingDate, hierarchy, parentId, parentName);
parentId, parentName);
} }
} }
private static final class OfficeLookupMapper implements private static final class OfficeLookupMapper implements RowMapper<OfficeLookup> {
RowMapper<OfficeLookup> {
public String officeLookupSchema() { public String officeLookupSchema() {
return " o.id as id, o.name as name from org_office o "; return " o.id as id, o.name as name from org_office o ";
@@ -106,8 +103,7 @@ public class OfficeReadPlatformServiceImpl implements OfficeReadPlatformService
+ rm.officeLookupSchema() + rm.officeLookupSchema()
+ "where o.org_id = ? and o.hierarchy like ? order by o.hierarchy"; + "where o.org_id = ? and o.hierarchy like ? order by o.hierarchy";
return this.jdbcTemplate.query(sql, rm, new Object[] { return this.jdbcTemplate.query(sql, rm, new Object[] {currentUser.getOrganisation().getId(), hierarchySearchString });
currentUser.getOrganisation().getId(), hierarchySearchString });
} }
@Override @Override
@@ -166,11 +162,4 @@ public class OfficeReadPlatformServiceImpl implements OfficeReadPlatformService
return filterParentLookups; return filterParentLookups;
} }
public static Long getRSLong(String stringVal, Long longVal) {
if (stringVal == null)
return null;
return longVal;
}
} }