make write service method transactional along with minor style changes.

This commit is contained in:
Keith Woodlock
2012-10-31 17:20:27 +00:00
parent 390964f361
commit 46970adb2b
11 changed files with 164 additions and 173 deletions
@@ -164,22 +164,22 @@ public class StaffApiResource {
.extractFieldsForResponseIfProvided(uriInfo
.getQueryParameters());
final boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo
.getQueryParameters());
final boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters());
Collection<OfficeLookup> offices = this.officeReadPlatformService.retrieveAllOfficesForLookup();
final Collection<OfficeLookup> offices = this.officeReadPlatformService.retrieveAllOfficesForLookup();
Collection<StaffData> loanOfficers = null;
StaffAccountSummaryCollectionData staffAccountSummaryCollectionData = null;
if (officeId != null){
if (officeId != null) {
loanOfficers = this.readPlatformService.retrieveAllLoanOfficersByOffice(officeId);
}
if (loanOfficerId != null){
if (loanOfficerId != null) {
staffAccountSummaryCollectionData = this.readPlatformService.retrieveLoanOfficerAccountSummary(loanOfficerId);
}
LoanReassignmentData loanReassignmentData = new LoanReassignmentData(officeId, loanOfficerId, offices,
final LoanReassignmentData loanReassignmentData = new LoanReassignmentData(officeId, loanOfficerId, offices,
loanOfficers, staffAccountSummaryCollectionData);
return this.apiJsonSerializerService.serializeLoanReassignmentDataToJson(prettyPrint, responseParameters, loanReassignmentData);
@@ -1,35 +1,31 @@
package org.mifosng.platform.api.commands;
import java.util.Set;
/**
* Immutable data object for reassigning loan officers on loans in bulk.
*/
public class BulkLoanReassignmentCommand {
private final Long fromLoanOfficerId;
private final Long toLoanOfficerId;
private final Long fromLoanOfficerId;
private final Long toLoanOfficerId;
private final String[] loans;
private final String[] loans;
private final Set<String> modifiedParameters;
public BulkLoanReassignmentCommand(Long fromLoanOfficerId,
Long toLoanOfficerId, String[] loans) {
this.fromLoanOfficerId = fromLoanOfficerId;
this.toLoanOfficerId = toLoanOfficerId;
this.loans = loans;
}
public BulkLoanReassignmentCommand(Set<String> modifiedParameters, Long fromLoanOfficerId,
Long toLoanOfficerId, String[] loans) {
this.fromLoanOfficerId = fromLoanOfficerId;
this.toLoanOfficerId = toLoanOfficerId;
this.loans = loans;
this.modifiedParameters = modifiedParameters;
}
public Long getFromLoanOfficerId() {
return fromLoanOfficerId;
}
public Long getFromLoanOfficerId() {
return fromLoanOfficerId;
}
public Long getToLoanOfficerId() {
return toLoanOfficerId;
}
public Long getToLoanOfficerId() {
return toLoanOfficerId;
}
public String[] getLoans() {
return loans;
}
}
public String[] getLoans() {
return loans;
}
}
@@ -2,45 +2,34 @@ package org.mifosng.platform.api.data;
import java.util.Collection;
/**
* Immutable data object returned for loan-officer reassignment screens.
*/
public class LoanReassignmentData {
private final Long officeId;
@SuppressWarnings("unused")
private final Long officeId;
@SuppressWarnings("unused")
private final Long fromLoanOfficerId;
//template
@SuppressWarnings("unused")
private final Collection<OfficeLookup> officeOptions;
@SuppressWarnings("unused")
private final Collection<StaffData> loanOfficerOptions;
@SuppressWarnings("unused")
private final StaffAccountSummaryCollectionData accountSummaryCollection;
public LoanReassignmentData(Long officeId, Long fromLoanOfficerId,
Collection<OfficeLookup> officeOptions, Collection<StaffData> loanOfficerOptions,
StaffAccountSummaryCollectionData accountSummaryCollection) {
public LoanReassignmentData(
final Long officeId,
final Long fromLoanOfficerId,
final Collection<OfficeLookup> officeOptions,
final Collection<StaffData> loanOfficerOptions,
final StaffAccountSummaryCollectionData accountSummaryCollection) {
this.officeId = officeId;
this.fromLoanOfficerId = fromLoanOfficerId;
this.officeOptions = officeOptions;
this.loanOfficerOptions = loanOfficerOptions;
this.accountSummaryCollection = accountSummaryCollection;
}
public Long getOfficeId() {
return officeId;
}
public Long getFromLoanOfficerId() {
return fromLoanOfficerId;
}
public Collection<OfficeLookup> getOfficeOptions() {
return officeOptions;
}
public Collection<StaffData> getLoanOfficerOptions() {
return loanOfficerOptions;
}
public StaffAccountSummaryCollectionData getAccountSummaryCollection() {
return accountSummaryCollection;
}
}
}
@@ -1,83 +1,78 @@
package org.mifosng.platform.api.data;
import java.util.Collection;
import java.util.List;
/**
*
*/
public class StaffAccountSummaryCollectionData {
private final List<ClientSummary> clients;
@SuppressWarnings("unused")
private final List<ClientSummary> clients;
@SuppressWarnings("unused")
private final List<GroupSummary> groups;
private final List<GroupSummary> groups;
public StaffAccountSummaryCollectionData(final List<ClientSummary> clients, final List<GroupSummary> groups) {
this.clients = clients;
this.groups = groups;
}
public StaffAccountSummaryCollectionData(List<ClientSummary> clients, List<GroupSummary> groups) {
this.clients = clients;
this.groups = groups;
}
public static final class ClientSummary {
public List<ClientSummary> getClients() {
return clients;
}
private final Long id;
private final String displayName;
public List<GroupSummary> getGroups() {
return groups;
}
private Collection<ClientAccountSummaryData> loans;
public static final class ClientSummary {
public ClientSummary(Long id, String displayName) {
this.id = id;
this.displayName = displayName;
}
private final Long id;
private final String displayName;
public Long getId() {
return id;
}
private Collection<ClientAccountSummaryData> loans;
public String getDisplayName() {
return displayName;
}
public ClientSummary(Long id, String displayName) {
this.id = id;
this.displayName = displayName;
}
public Collection<ClientAccountSummaryData> getLoans() {
return loans;
}
public Long getId() {
return id;
}
public void setLoans(Collection<ClientAccountSummaryData> loans) {
this.loans = loans;
}
}
public String getDisplayName() {
return displayName;
}
public static final class GroupSummary {
public Collection<ClientAccountSummaryData> getLoans() {
return loans;
}
private final Long id;
private final String name;
public void setLoans(Collection<ClientAccountSummaryData> loans) {
this.loans = loans;
}
}
private Collection<GroupAccountSummaryData> loans;
public static final class GroupSummary {
public GroupSummary(Long id, String name) {
this.id = id;
this.name = name;
}
private final Long id;
private final String name;
public Long getId() {
return id;
}
private Collection<GroupAccountSummaryData> loans;
public String getName() {
return name;
}
public GroupSummary(Long id, String name) {
this.id = id;
this.name = name;
}
public Collection<GroupAccountSummaryData> getLoans() {
return loans;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public Collection<GroupAccountSummaryData> getLoans() {
return loans;
}
public void setLoans(Collection<GroupAccountSummaryData> loans) {
this.loans = loans;
}
}
}
public void setLoans(Collection<GroupAccountSummaryData> loans) {
this.loans = loans;
}
}
}
@@ -155,7 +155,7 @@ public class ApiDataConversionServiceImpl implements ApiDataConversionService {
}
@Override
public BulkLoanReassignmentCommand convertJsonToBulkLoanReassignmentCommand(String json) {
public BulkLoanReassignmentCommand convertJsonToBulkLoanReassignmentCommand(final String json) {
if (StringUtils.isBlank(json)) {
throw new InvalidJsonException();
@@ -193,7 +193,7 @@ public class ApiDataConversionServiceImpl implements ApiDataConversionService {
}
//
return new BulkLoanReassignmentCommand(modifiedParameters, fromLoanOfficerId, toLoanOfficerId, loans);
return new BulkLoanReassignmentCommand(fromLoanOfficerId, toLoanOfficerId, loans);
}
@Override
@@ -296,14 +296,6 @@ public class Loan extends AbstractAuditableCustom<AppUser, Long> {
return this.loanProduct;
}
public Staff getLoanofficer() {
return loanofficer;
}
public void setLoanofficer(Staff loanofficer) {
this.loanofficer = loanofficer;
}
public LoanProductRelatedDetail repaymentScheduleDetail() {
return this.loanRepaymentScheduleDetail;
}
@@ -1374,6 +1366,18 @@ public class Loan extends AbstractAuditableCustom<AppUser, Long> {
public boolean hasIdentifyOf(final Long loanId) {
return loanId.equals(this.getId());
}
public boolean hasLoanOfficer(final Staff fromLoanOfficer) {
boolean matchesCurrentLoanOfficer = false;
if (this.loanofficer != null) {
matchesCurrentLoanOfficer = this.loanofficer.identifiedBy(fromLoanOfficer);
} else {
matchesCurrentLoanOfficer = fromLoanOfficer == null;
}
return matchesCurrentLoanOfficer;
}
public LocalDate getInterestChargedFromDate() {
LocalDate interestChargedFrom = null;
@@ -1420,4 +1424,8 @@ public class Loan extends AbstractAuditableCustom<AppUser, Long> {
private MonetaryCurrency getCurrency() {
return this.loanRepaymentScheduleDetail.getCurrency();
}
public void updateLoanofficer(final Staff newLoanOfficer) {
this.loanofficer = newLoanOfficer;
}
}
@@ -728,8 +728,9 @@ public class LoanWritePlatformServiceJpaRepositoryImpl implements LoanWritePlatf
return new EntityIdentifier(loanCharge.getId());
}
@Transactional
@Override
public EntityIdentifier bulkLoanReassignment(BulkLoanReassignmentCommand command) {
public EntityIdentifier bulkLoanReassignment(final BulkLoanReassignmentCommand command) {
this.context.authenticatedUser();
@@ -746,11 +747,12 @@ public class LoanWritePlatformServiceJpaRepositoryImpl implements LoanWritePlatf
if (loan == null) {
throw new LoanNotFoundException(loanId);
}
if (!loan.getLoanofficer().equals(fromLoanOfficer)){
if (!loan.hasLoanOfficer(fromLoanOfficer)){
throw new LoanOfficerAssignmentException(loan.getId(), fromLoanOfficer.getId());
}
loan.setLoanofficer(toLoanOfficer);
loan.updateLoanofficer(toLoanOfficer);
this.loanRepository.save(loan);
}
@@ -90,5 +90,8 @@ public class Staff extends AbstractAuditableCustom<AppUser, Long> {
public Boolean getLoanOfficerFlag() {
return loanOfficerFlag;
}
public boolean identifiedBy(Staff staff) {
return getId().equals(staff.getId());
}
}
@@ -11,7 +11,7 @@ public interface StaffReadPlatformService {
Collection<StaffData> retrieveAllStaff(final String extraCriteria);
Collection<StaffData> retrieveAllLoanOfficersByOffice(final long officeId);
Collection<StaffData> retrieveAllLoanOfficersByOffice(final Long officeId);
StaffAccountSummaryCollectionData retrieveLoanOfficerAccountSummary(final long loanOfficerId);
StaffAccountSummaryCollectionData retrieveLoanOfficerAccountSummary(final Long loanOfficerId);
}
@@ -82,10 +82,8 @@ public class StaffReadPlatformServiceImpl implements StaffReadPlatformService {
}
@Override
public Collection<StaffData> retrieveAllLoanOfficersByOffice(long officeId) {
return retrieveAllStaff(" office_id = " + officeId
+ " and is_loan_officer = 1");
public Collection<StaffData> retrieveAllLoanOfficersByOffice(final Long officeId) {
return retrieveAllStaff(" office_id=" + officeId + " and is_loan_officer=1");
}
@Override
@@ -106,73 +104,74 @@ public class StaffReadPlatformServiceImpl implements StaffReadPlatformService {
}
}
@Override
public StaffAccountSummaryCollectionData retrieveLoanOfficerAccountSummary(long loanOfficerId) {
@Override
public StaffAccountSummaryCollectionData retrieveLoanOfficerAccountSummary(final Long loanOfficerId) {
context.authenticatedUser();
context.authenticatedUser();
StaffClientMapper staffClientMapper = new StaffClientMapper();
String clientSql = "select distinct " + staffClientMapper.schema();
StaffClientMapper staffClientMapper = new StaffClientMapper();
String clientSql = "select distinct " + staffClientMapper.schema();
StaffGroupMapper staffGroupMapper = new StaffGroupMapper();
String groupSql = "select distinct " + staffGroupMapper.schema();
StaffGroupMapper staffGroupMapper = new StaffGroupMapper();
String groupSql = "select distinct " + staffGroupMapper.schema();
List<StaffAccountSummaryCollectionData.ClientSummary> clientSummaryList =
this.jdbcTemplate.query(clientSql, staffClientMapper, new Object[]{loanOfficerId});
List<StaffAccountSummaryCollectionData.ClientSummary> clientSummaryList = this.jdbcTemplate
.query(clientSql, staffClientMapper,
new Object[] { loanOfficerId });
for (StaffAccountSummaryCollectionData.ClientSummary clientSummary : clientSummaryList){
for (StaffAccountSummaryCollectionData.ClientSummary clientSummary : clientSummaryList) {
Collection<ClientAccountSummaryData> clientLoanAccounts =
this.clientReadPlatformService.retrieveClientLoanAccountsByLoanOfficerId(clientSummary.getId(), loanOfficerId);
Collection<ClientAccountSummaryData> clientLoanAccounts = this.clientReadPlatformService
.retrieveClientLoanAccountsByLoanOfficerId(
clientSummary.getId(), loanOfficerId);
clientSummary.setLoans(clientLoanAccounts);
}
clientSummary.setLoans(clientLoanAccounts);
}
List<StaffAccountSummaryCollectionData.GroupSummary> groupSummaryList =
this.jdbcTemplate.query(groupSql, staffGroupMapper, new Object[]{loanOfficerId});
List<StaffAccountSummaryCollectionData.GroupSummary> groupSummaryList = this.jdbcTemplate
.query(groupSql, staffGroupMapper, new Object[] { loanOfficerId });
for (StaffAccountSummaryCollectionData.GroupSummary groupSummary : groupSummaryList){
for (StaffAccountSummaryCollectionData.GroupSummary groupSummary : groupSummaryList) {
Collection<GroupAccountSummaryData> groupLoanAccounts =
this.groupReadPlatformService.retrieveGroupLoanAccountsByLoanOfficerId(groupSummary.getId(), loanOfficerId);
Collection<GroupAccountSummaryData> groupLoanAccounts = this.groupReadPlatformService
.retrieveGroupLoanAccountsByLoanOfficerId(
groupSummary.getId(), loanOfficerId);
groupSummary.setLoans(groupLoanAccounts);
}
groupSummary.setLoans(groupLoanAccounts);
}
return new StaffAccountSummaryCollectionData(clientSummaryList, groupSummaryList);
}
return new StaffAccountSummaryCollectionData(clientSummaryList, groupSummaryList);
}
private static final class StaffClientMapper implements RowMapper<StaffAccountSummaryCollectionData.ClientSummary> {
public String schema(){
public String schema() {
return " c.id as id, c.display_name as displayName from m_client c " +
" join m_loan l on c.id = l.client_id where l.loan_officer_id = ? ";
}
@Override
public StaffAccountSummaryCollectionData.ClientSummary mapRow(ResultSet rs, int rowNum) throws SQLException {
Long id = JdbcSupport.getLong(rs, "id");
String displayName = rs.getString("displayName");
public StaffAccountSummaryCollectionData.ClientSummary mapRow(final ResultSet rs, @SuppressWarnings("unused") final int rowNum) throws SQLException {
final Long id = JdbcSupport.getLong(rs, "id");
final String displayName = rs.getString("displayName");
return new StaffAccountSummaryCollectionData.ClientSummary(id, displayName);
}
}
private static final class StaffGroupMapper implements RowMapper<StaffAccountSummaryCollectionData.GroupSummary> {
public String schema(){
public String schema() {
return " g.id as id, g.name as name from m_group g" +
" join m_loan l on g.id = l.group_id where l.loan_officer_id = ? ";
}
@Override
public StaffAccountSummaryCollectionData.GroupSummary mapRow(ResultSet rs, int rowNum) throws SQLException {
Long id = JdbcSupport.getLong(rs, "id");
String name = rs.getString("name");
public StaffAccountSummaryCollectionData.GroupSummary mapRow(final ResultSet rs, @SuppressWarnings("unused") final int rowNum) throws SQLException {
final Long id = JdbcSupport.getLong(rs, "id");
final String name = rs.getString("name");
return new StaffAccountSummaryCollectionData.GroupSummary(id, name);
}
}
}
@@ -48,8 +48,7 @@ public class StaffWritePlatformServiceJpaRepositoryImpl implements
StaffCommandValidator validator = new StaffCommandValidator(command);
validator.validateForCreate();
Office staffOffice = this.officeRepository.findOne(command
.getOfficeId());
Office staffOffice = this.officeRepository.findOne(command.getOfficeId());
if (staffOffice == null) {
throw new OfficeNotFoundException(command.getOfficeId());
}