diff --git a/mifosng-provider/src/main/java/org/mifosplatform/organisation/staff/serialization/StaffCommandFromApiJsonDeserializer.java b/mifosng-provider/src/main/java/org/mifosplatform/organisation/staff/serialization/StaffCommandFromApiJsonDeserializer.java index 7a65d3e12..18dfeacf3 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/organisation/staff/serialization/StaffCommandFromApiJsonDeserializer.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/organisation/staff/serialization/StaffCommandFromApiJsonDeserializer.java @@ -20,6 +20,7 @@ import org.mifosplatform.infrastructure.core.data.DataValidatorBuilder; import org.mifosplatform.infrastructure.core.exception.InvalidJsonException; import org.mifosplatform.infrastructure.core.exception.PlatformApiDataValidationException; import org.mifosplatform.infrastructure.core.serialization.FromJsonHelper; +import org.mifosplatform.organisation.staff.service.StaffReadPlatformService; import org.mifosplatform.portfolio.client.api.ClientApiConstants; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -34,13 +35,18 @@ public final class StaffCommandFromApiJsonDeserializer { * The parameters supported for this command. */ private final Set supportedParameters = new HashSet<>(Arrays.asList("firstname", "lastname", "officeId", "externalId", - "mobileNo", "isLoanOfficer", "isActive", "joiningDate", "dateFormat", "locale")); + "mobileNo", "isLoanOfficer", "isActive", "joiningDate", "dateFormat", "locale", "forceStatus")); private final FromJsonHelper fromApiJsonHelper; + + private final StaffReadPlatformService staffReadPlatformService; + @Autowired - public StaffCommandFromApiJsonDeserializer(final FromJsonHelper fromApiJsonHelper) { + public StaffCommandFromApiJsonDeserializer(final FromJsonHelper fromApiJsonHelper, + final StaffReadPlatformService staffReadPlatformService) { this.fromApiJsonHelper = fromApiJsonHelper; + this.staffReadPlatformService = staffReadPlatformService; } public void validateForCreate(final String json) { @@ -98,6 +104,10 @@ public final class StaffCommandFromApiJsonDeserializer { } public void validateForUpdate(final String json) { + validateForUpdate(json, null); + } + + public void validateForUpdate(final String json,Long staffId) { if (StringUtils.isBlank(json)) { throw new InvalidJsonException(); } final Type typeOfMap = new TypeToken>() {}.getType(); @@ -134,6 +144,17 @@ public final class StaffCommandFromApiJsonDeserializer { if (this.fromApiJsonHelper.parameterExists("isActive", element)) { final Boolean activeFlag = this.fromApiJsonHelper.extractBooleanNamed("isActive", element); + //Need to add here check to see if any clients, group, account and loans are assigned to this staff if staff is being set to inactive --LJB + final Boolean forceStatus = this.fromApiJsonHelper.extractBooleanNamed("forceStatus", element); + if ((!activeFlag && forceStatus == null) || + (!activeFlag && forceStatus)) { + Object[] result = staffReadPlatformService.hasAssociatedItems(staffId); + + if (result != null && result.length > 0) { + baseDataValidator.reset().parameter("isactive").failWithCode("staff.is.assigned",result); + } + + } baseDataValidator.reset().parameter("isActive").value(activeFlag).notNull(); } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/organisation/staff/service/StaffReadPlatformService.java b/mifosng-provider/src/main/java/org/mifosplatform/organisation/staff/service/StaffReadPlatformService.java index 75c8bc028..aaa35852c 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/organisation/staff/service/StaffReadPlatformService.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/organisation/staff/service/StaffReadPlatformService.java @@ -24,4 +24,6 @@ public interface StaffReadPlatformService { Collection retrieveAllStaffInOfficeAndItsParentOfficeHierarchy(Long officeId, boolean loanOfficersOnly); Collection retrieveAllStaff(String sqlSearch, Long officeId, boolean loanOfficersOnly, String status); + + Object[] hasAssociatedItems(final Long staffId); } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/organisation/staff/service/StaffReadPlatformServiceImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/organisation/staff/service/StaffReadPlatformServiceImpl.java index 1aefddb13..e4e717b6f 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/organisation/staff/service/StaffReadPlatformServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/organisation/staff/service/StaffReadPlatformServiceImpl.java @@ -7,7 +7,10 @@ package org.mifosplatform.organisation.staff.service; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; import java.util.Collection; +import java.util.List; +import java.util.Map; import org.apache.commons.lang.StringUtils; import org.joda.time.LocalDate; @@ -17,6 +20,9 @@ import org.mifosplatform.infrastructure.core.service.RoutingDataSource; import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext; import org.mifosplatform.organisation.staff.data.StaffData; import org.mifosplatform.organisation.staff.exception.StaffNotFoundException; +import org.mifosplatform.portfolio.client.domain.ClientStatus; +import org.mifosplatform.portfolio.loanaccount.domain.LoanStatus; +import org.mifosplatform.portfolio.savings.domain.SavingsAccountStatusType; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.jdbc.core.JdbcTemplate; @@ -239,4 +245,39 @@ public class StaffReadPlatformServiceImpl implements StaffReadPlatformService { sql = sql + " order by s.lastname"; return this.jdbcTemplate.query(sql, this.staffInOfficeHierarchyMapper, new Object[] { officeId }); } + + @Override + public Object[] hasAssociatedItems(final Long staffId){ + ArrayList params = new ArrayList(); + + String sql = "select c.display_name as client, g.display_name as grp,l.loan_officer_id as loan, s.field_officer_id as sav"+ + " from m_staff staff "+ + " left outer join m_client c on staff.id = c.staff_id AND c.status_enum < "+ ClientStatus.CLOSED.getValue() + + " left outer join m_group g on staff.id = g.staff_id " + + " left outer join m_loan l on staff.id = l.loan_officer_id and l.loan_status_id < " + LoanStatus.WITHDRAWN_BY_CLIENT.getValue() + + " left outer join m_savings_account s on c.staff_id = s.field_officer_id and s.status_enum < "+ SavingsAccountStatusType.WITHDRAWN_BY_APPLICANT.getValue() + + " where staff.id = " + staffId + + " group by staff.id"; + + + List> result = this.jdbcTemplate.queryForList(sql); + if (result != null) { + for (Map map : result) { + if (map.get("client") != null) { + params.add("client"); + } + if (map.get("grp") != null) { + params.add("group"); + } + if (map.get("loan") != null) { + params.add("loan"); + } + if (map.get("sav") != null) { + params.add("savings account"); + } + } + } + return params.toArray(); + + } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/organisation/staff/service/StaffWritePlatformServiceJpaRepositoryImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/organisation/staff/service/StaffWritePlatformServiceJpaRepositoryImpl.java index 5db2ef828..891ff6139 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/organisation/staff/service/StaffWritePlatformServiceJpaRepositoryImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/organisation/staff/service/StaffWritePlatformServiceJpaRepositoryImpl.java @@ -74,7 +74,7 @@ public class StaffWritePlatformServiceJpaRepositoryImpl implements StaffWritePla public CommandProcessingResult updateStaff(final Long staffId, final JsonCommand command) { try { - this.fromApiJsonDeserializer.validateForUpdate(command.json()); + this.fromApiJsonDeserializer.validateForUpdate(command.json(), staffId); final Staff staffForUpdate = this.staffRepository.findOne(staffId); if (staffForUpdate == null) { throw new StaffNotFoundException(staffId); }