Added option for group to have loan officer assigned

This commit is contained in:
Nayan Ambali
2013-02-14 20:17:13 +05:30
parent d6431bffc1
commit 0ec56c32f8
6 changed files with 85 additions and 10 deletions
@@ -0,0 +1,2 @@
ALTER TABLE m_group ADD COLUMN `loan_officer_id` BIGINT(20) NULL DEFAULT NULL AFTER `office_id`;
ALTER TABLE `m_group` ADD CONSTRAINT `FK_m_group_m_staff` FOREIGN KEY (`loan_officer_id`) REFERENCES `m_staff` (`id`);
@@ -13,25 +13,32 @@ public class GroupCommand {
private final String externalId; private final String externalId;
private final String name; private final String name;
private final Long officeId; private final Long officeId;
private final Long loanOfficerId;
private final String[] clientMembers; private final String[] clientMembers;
private final Set<String> modifiedParameters; private final Set<String> modifiedParameters;
public GroupCommand(Set<String> modifiedParameters, final Long id, final String externalId, final String name, public GroupCommand(Set<String> modifiedParameters, final Long id, final String externalId, final String name,
final Long officeId, String[] clientMembers) { final Long officeId, final Long loanOfficerId, String[] clientMembers) {
this.id = id; this.id = id;
this.officeId = officeId; this.officeId = officeId;
this.loanOfficerId = loanOfficerId;
this.externalId = externalId; this.externalId = externalId;
this.name = name; this.name = name;
this.clientMembers = clientMembers; this.clientMembers = clientMembers;
this.modifiedParameters = modifiedParameters; this.modifiedParameters = modifiedParameters;
} }
public Long getOfficeId() { public Long getOfficeId() {
return officeId; return officeId;
} }
public Long getLoanOfficeId() {
return loanOfficerId;
}
public String getExternalId() { public String getExternalId() {
return externalId; return externalId;
} }
@@ -52,6 +59,10 @@ public class GroupCommand {
return this.modifiedParameters.contains("officeId"); return this.modifiedParameters.contains("officeId");
} }
public boolean isLoanOfficerChanged() {
return this.modifiedParameters.contains("loanOfficerId");
}
public boolean isNameChanged() { public boolean isNameChanged() {
return this.modifiedParameters.contains("name"); return this.modifiedParameters.contains("name");
} }
@@ -18,6 +18,7 @@ import javax.persistence.Table;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.mifosplatform.organisation.office.domain.Office; import org.mifosplatform.organisation.office.domain.Office;
import org.mifosplatform.organisation.staff.domain.Staff;
import org.mifosplatform.portfolio.client.domain.Client; import org.mifosplatform.portfolio.client.domain.Client;
import org.mifosplatform.portfolio.group.command.GroupCommand; import org.mifosplatform.portfolio.group.command.GroupCommand;
import org.springframework.data.jpa.domain.AbstractPersistable; import org.springframework.data.jpa.domain.AbstractPersistable;
@@ -31,6 +32,11 @@ public class Group extends AbstractPersistable<Long> {
@JoinColumn(name = "office_id", nullable = false) @JoinColumn(name = "office_id", nullable = false)
private Office office; private Office office;
@SuppressWarnings("unused")
@ManyToOne
@JoinColumn(name = "loan_officer_id", nullable = false)
private Staff loanOfficer;
@Column(name = "name", length = 100, unique = true) @Column(name = "name", length = 100, unique = true)
private String name; private String name;
@@ -50,12 +56,13 @@ public class Group extends AbstractPersistable<Long> {
this.clientMembers = new HashSet<Client>(); this.clientMembers = new HashSet<Client>();
} }
public static Group newGroup(Office office, String name, String externalId, Set<Client> clientMembers) { public static Group newGroup(Office office, Staff loanOfficer , String name, String externalId, Set<Client> clientMembers) {
return new Group(office, name, externalId, clientMembers); return new Group(office, loanOfficer , name, externalId, clientMembers);
} }
public Group(Office office, String name, String externalId, Set<Client> clientMembers) { public Group(Office office, Staff loanOfficer , String name, String externalId, Set<Client> clientMembers) {
this.office = office; this.office = office;
this.loanOfficer = loanOfficer;
if (StringUtils.isNotBlank(name)) { if (StringUtils.isNotBlank(name)) {
this.name = name.trim(); this.name = name.trim();
} else { } else {
@@ -71,7 +78,7 @@ public class Group extends AbstractPersistable<Long> {
} }
} }
public void update(final GroupCommand command, final Office groupOffice, final Set<Client> clientMembers) { public void update(final GroupCommand command, final Office groupOffice, final Staff newLoanOfficer, final Set<Client> clientMembers) {
if (command.isExternalIdChanged()) { if (command.isExternalIdChanged()) {
this.externalId = command.getExternalId(); this.externalId = command.getExternalId();
} }
@@ -80,6 +87,10 @@ public class Group extends AbstractPersistable<Long> {
this.office = groupOffice; this.office = groupOffice;
} }
if (command.isLoanOfficerChanged()) {
this.loanOfficer = newLoanOfficer;
}
if (command.isNameChanged()) { if (command.isNameChanged()) {
this.name = command.getName(); this.name = command.getName();
} }
@@ -0,0 +1,19 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package org.mifosplatform.portfolio.group.exception;
import org.mifosplatform.infrastructure.core.exception.AbstractPlatformResourceNotFoundException;
/**
* A {@link RuntimeException} thrown when group resources are not found.
*/
public class LoanOfficerNotFoundException extends AbstractPlatformResourceNotFoundException {
public LoanOfficerNotFoundException(Long id){
super("error.msg.loanofficer.id.invalid", "Loan Officer with identifier " + id + " does not exist", id);
}
}
@@ -15,6 +15,10 @@ import org.mifosplatform.organisation.office.domain.Office;
import org.mifosplatform.organisation.office.domain.OfficeRepository; import org.mifosplatform.organisation.office.domain.OfficeRepository;
import org.mifosplatform.organisation.office.exception.InvalidOfficeException; import org.mifosplatform.organisation.office.exception.InvalidOfficeException;
import org.mifosplatform.organisation.office.exception.OfficeNotFoundException; import org.mifosplatform.organisation.office.exception.OfficeNotFoundException;
import org.mifosplatform.organisation.staff.domain.Staff;
import org.mifosplatform.organisation.staff.domain.StaffRepository;
import org.mifosplatform.organisation.staff.exception.StaffNotFoundException;
import org.mifosplatform.organisation.staff.exception.StaffRoleException;
import org.mifosplatform.portfolio.client.domain.Client; import org.mifosplatform.portfolio.client.domain.Client;
import org.mifosplatform.portfolio.client.domain.ClientRepository; import org.mifosplatform.portfolio.client.domain.ClientRepository;
import org.mifosplatform.portfolio.client.exception.ClientNotFoundException; import org.mifosplatform.portfolio.client.exception.ClientNotFoundException;
@@ -23,6 +27,7 @@ import org.mifosplatform.portfolio.group.command.GroupCommandValidator;
import org.mifosplatform.portfolio.group.domain.Group; import org.mifosplatform.portfolio.group.domain.Group;
import org.mifosplatform.portfolio.group.domain.GroupRepository; import org.mifosplatform.portfolio.group.domain.GroupRepository;
import org.mifosplatform.portfolio.group.exception.GroupNotFoundException; import org.mifosplatform.portfolio.group.exception.GroupNotFoundException;
import org.mifosplatform.portfolio.group.exception.LoanOfficerNotFoundException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -44,13 +49,16 @@ public class GroupWritePlatformServiceJpaRepositoryImpl implements GroupWritePla
private final OfficeRepository officeRepository; private final OfficeRepository officeRepository;
private final StaffRepository staffRepository;
@Autowired @Autowired
public GroupWritePlatformServiceJpaRepositoryImpl(PlatformSecurityContext context, GroupRepository groupRepository, public GroupWritePlatformServiceJpaRepositoryImpl(PlatformSecurityContext context, GroupRepository groupRepository,
ClientRepository clientRepository, OfficeRepository officeRepository) { ClientRepository clientRepository, OfficeRepository officeRepository , StaffRepository staffRepository) {
this.context = context; this.context = context;
this.groupRepository = groupRepository; this.groupRepository = groupRepository;
this.clientRepository = clientRepository; this.clientRepository = clientRepository;
this.officeRepository = officeRepository; this.officeRepository = officeRepository;
this.staffRepository = staffRepository;
} }
@Transactional @Transactional
@@ -63,13 +71,26 @@ public class GroupWritePlatformServiceJpaRepositoryImpl implements GroupWritePla
validator.validateForCreate(); validator.validateForCreate();
Office groupOffice = this.officeRepository.findOne(command.getOfficeId()); Office groupOffice = this.officeRepository.findOne(command.getOfficeId());
Staff loanOfficer = null;
Long loanOfficerId = command.getLoanOfficeId();
if (loanOfficerId != null) {
// TODO Set the scope for staffRepository.findOneByOfficeId to a officer equal to groupOffice
loanOfficer = this.staffRepository.findOne(loanOfficerId);
if (loanOfficer == null) {
throw new StaffNotFoundException(loanOfficerId);
}
}
if (groupOffice == null) { if (groupOffice == null) {
throw new OfficeNotFoundException(command.getOfficeId()); throw new OfficeNotFoundException(command.getOfficeId());
} }
final Set<Client> clientMembers = assembleSetOfClients(command); final Set<Client> clientMembers = assembleSetOfClients(command);
Group newGroup = Group.newGroup(groupOffice, command.getName(), command.getExternalId(), clientMembers); Group newGroup = Group.newGroup(groupOffice, loanOfficer , command.getName(), command.getExternalId(), clientMembers);
this.groupRepository.saveAndFlush(newGroup); this.groupRepository.saveAndFlush(newGroup);
@@ -103,10 +124,20 @@ public class GroupWritePlatformServiceJpaRepositoryImpl implements GroupWritePla
throw new OfficeNotFoundException(command.getOfficeId()); throw new OfficeNotFoundException(command.getOfficeId());
} }
} }
Staff loanOfficer = null;
Long loanOfficerId = command.getLoanOfficeId();
if (command.isLoanOfficerChanged() && loanOfficerId != null) {
loanOfficer = this.staffRepository.findOne(loanOfficerId);
if (loanOfficer == null) {
throw new LoanOfficerNotFoundException(loanOfficerId);
}
}
final Set<Client> clientMembers = assembleSetOfClients(command); final Set<Client> clientMembers = assembleSetOfClients(command);
groupForUpdate.update(command, groupOffice, clientMembers); groupForUpdate.update(command, groupOffice, loanOfficer , clientMembers);
groupRepository.saveAndFlush(groupForUpdate); groupRepository.saveAndFlush(groupForUpdate);
@@ -68,13 +68,14 @@ public class PortfolioApiDataConversionServiceImpl implements PortfolioApiDataCo
final Type typeOfMap = new TypeToken<Map<String, Object>>() {}.getType(); final Type typeOfMap = new TypeToken<Map<String, Object>>() {}.getType();
final Map<String, String> requestMap = gsonConverter.fromJson(json, typeOfMap); final Map<String, String> requestMap = gsonConverter.fromJson(json, typeOfMap);
final Set<String> supportedParams = new HashSet<String>(Arrays.asList("name", "officeId", "externalId", "clientMembers")); final Set<String> supportedParams = new HashSet<String>(Arrays.asList("name", "officeId", "loanOfficerId" ,"externalId", "clientMembers"));
checkForUnsupportedParameters(requestMap, supportedParams); checkForUnsupportedParameters(requestMap, supportedParams);
final Set<String> modifiedParameters = new HashSet<String>(); final Set<String> modifiedParameters = new HashSet<String>();
final Long officeId = extractLongParameter("officeId", requestMap, modifiedParameters); final Long officeId = extractLongParameter("officeId", requestMap, modifiedParameters);
final Long loanOfficeId = extractLongParameter("loanOfficerId", requestMap, modifiedParameters);
final String externalId = extractStringParameter("externalId", requestMap, modifiedParameters); final String externalId = extractStringParameter("externalId", requestMap, modifiedParameters);
final String name = extractStringParameter("name", requestMap, modifiedParameters); final String name = extractStringParameter("name", requestMap, modifiedParameters);
@@ -96,7 +97,7 @@ public class PortfolioApiDataConversionServiceImpl implements PortfolioApiDataCo
} }
// //
return new GroupCommand(modifiedParameters, resourceIdentifier, externalId, name, officeId, clientMembers); return new GroupCommand(modifiedParameters, resourceIdentifier, externalId, name, officeId, loanOfficeId , clientMembers);
} }
private void checkForUnsupportedParameters(Map<String, ?> requestMap, Set<String> supportedParams) { private void checkForUnsupportedParameters(Map<String, ?> requestMap, Set<String> supportedParams) {