diff --git a/mifosng-db/patches/07-alter-group_to_add_loanofficer.sql b/mifosng-db/patches/07-alter-group_to_add_loanofficer.sql new file mode 100644 index 000000000..c34f21955 --- /dev/null +++ b/mifosng-db/patches/07-alter-group_to_add_loanofficer.sql @@ -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`); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/command/GroupCommand.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/command/GroupCommand.java index ba08ea20b..d4640e72f 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/command/GroupCommand.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/command/GroupCommand.java @@ -13,25 +13,32 @@ public class GroupCommand { private final String externalId; private final String name; private final Long officeId; + private final Long loanOfficerId; private final String[] clientMembers; private final Set modifiedParameters; public GroupCommand(Set 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.officeId = officeId; + this.loanOfficerId = loanOfficerId; this.externalId = externalId; this.name = name; this.clientMembers = clientMembers; this.modifiedParameters = modifiedParameters; + } public Long getOfficeId() { return officeId; } + public Long getLoanOfficeId() { + return loanOfficerId; + } + public String getExternalId() { return externalId; } @@ -52,6 +59,10 @@ public class GroupCommand { return this.modifiedParameters.contains("officeId"); } + public boolean isLoanOfficerChanged() { + return this.modifiedParameters.contains("loanOfficerId"); + } + public boolean isNameChanged() { return this.modifiedParameters.contains("name"); } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/domain/Group.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/domain/Group.java index 888ef1f73..b60656849 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/domain/Group.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/domain/Group.java @@ -18,6 +18,7 @@ import javax.persistence.Table; import org.apache.commons.lang.StringUtils; 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.group.command.GroupCommand; import org.springframework.data.jpa.domain.AbstractPersistable; @@ -31,6 +32,11 @@ public class Group extends AbstractPersistable { @JoinColumn(name = "office_id", nullable = false) private Office office; + @SuppressWarnings("unused") + @ManyToOne + @JoinColumn(name = "loan_officer_id", nullable = false) + private Staff loanOfficer; + @Column(name = "name", length = 100, unique = true) private String name; @@ -50,12 +56,13 @@ public class Group extends AbstractPersistable { this.clientMembers = new HashSet(); } - public static Group newGroup(Office office, String name, String externalId, Set clientMembers) { - return new Group(office, name, externalId, clientMembers); + public static Group newGroup(Office office, Staff loanOfficer , String name, String externalId, Set clientMembers) { + return new Group(office, loanOfficer , name, externalId, clientMembers); } - public Group(Office office, String name, String externalId, Set clientMembers) { + public Group(Office office, Staff loanOfficer , String name, String externalId, Set clientMembers) { this.office = office; + this.loanOfficer = loanOfficer; if (StringUtils.isNotBlank(name)) { this.name = name.trim(); } else { @@ -71,7 +78,7 @@ public class Group extends AbstractPersistable { } } - public void update(final GroupCommand command, final Office groupOffice, final Set clientMembers) { + public void update(final GroupCommand command, final Office groupOffice, final Staff newLoanOfficer, final Set clientMembers) { if (command.isExternalIdChanged()) { this.externalId = command.getExternalId(); } @@ -80,6 +87,10 @@ public class Group extends AbstractPersistable { this.office = groupOffice; } + if (command.isLoanOfficerChanged()) { + this.loanOfficer = newLoanOfficer; + } + if (command.isNameChanged()) { this.name = command.getName(); } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/exception/LoanOfficerNotFoundException.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/exception/LoanOfficerNotFoundException.java new file mode 100644 index 000000000..3ec40fc7f --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/exception/LoanOfficerNotFoundException.java @@ -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); + } + +} diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupWritePlatformServiceJpaRepositoryImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupWritePlatformServiceJpaRepositoryImpl.java index ebad281e9..e47bfc6f1 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupWritePlatformServiceJpaRepositoryImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupWritePlatformServiceJpaRepositoryImpl.java @@ -15,6 +15,10 @@ import org.mifosplatform.organisation.office.domain.Office; import org.mifosplatform.organisation.office.domain.OfficeRepository; import org.mifosplatform.organisation.office.exception.InvalidOfficeException; 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.ClientRepository; 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.GroupRepository; import org.mifosplatform.portfolio.group.exception.GroupNotFoundException; +import org.mifosplatform.portfolio.group.exception.LoanOfficerNotFoundException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -44,13 +49,16 @@ public class GroupWritePlatformServiceJpaRepositoryImpl implements GroupWritePla private final OfficeRepository officeRepository; + private final StaffRepository staffRepository; + @Autowired public GroupWritePlatformServiceJpaRepositoryImpl(PlatformSecurityContext context, GroupRepository groupRepository, - ClientRepository clientRepository, OfficeRepository officeRepository) { + ClientRepository clientRepository, OfficeRepository officeRepository , StaffRepository staffRepository) { this.context = context; this.groupRepository = groupRepository; this.clientRepository = clientRepository; this.officeRepository = officeRepository; + this.staffRepository = staffRepository; } @Transactional @@ -63,13 +71,26 @@ public class GroupWritePlatformServiceJpaRepositoryImpl implements GroupWritePla validator.validateForCreate(); 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) { throw new OfficeNotFoundException(command.getOfficeId()); } final Set 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); @@ -103,10 +124,20 @@ public class GroupWritePlatformServiceJpaRepositoryImpl implements GroupWritePla 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 clientMembers = assembleSetOfClients(command); - groupForUpdate.update(command, groupOffice, clientMembers); + groupForUpdate.update(command, groupOffice, loanOfficer , clientMembers); groupRepository.saveAndFlush(groupForUpdate); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savingsaccount/PortfolioApiDataConversionServiceImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savingsaccount/PortfolioApiDataConversionServiceImpl.java index 38591c13b..c82052666 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savingsaccount/PortfolioApiDataConversionServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savingsaccount/PortfolioApiDataConversionServiceImpl.java @@ -68,13 +68,14 @@ public class PortfolioApiDataConversionServiceImpl implements PortfolioApiDataCo final Type typeOfMap = new TypeToken>() {}.getType(); final Map requestMap = gsonConverter.fromJson(json, typeOfMap); - final Set supportedParams = new HashSet(Arrays.asList("name", "officeId", "externalId", "clientMembers")); + final Set supportedParams = new HashSet(Arrays.asList("name", "officeId", "loanOfficerId" ,"externalId", "clientMembers")); checkForUnsupportedParameters(requestMap, supportedParams); final Set modifiedParameters = new HashSet(); final Long officeId = extractLongParameter("officeId", requestMap, modifiedParameters); + final Long loanOfficeId = extractLongParameter("loanOfficerId", requestMap, modifiedParameters); final String externalId = extractStringParameter("externalId", 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 requestMap, Set supportedParams) {