Validation changes when setting staff to inactive.

This commit is contained in:
LisaBennett
2015-10-14 09:41:22 -04:00
parent 1f0ce4670e
commit 25ddb88db9
4 changed files with 67 additions and 3 deletions
@@ -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<String> 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<Map<String, Object>>() {}.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();
}
@@ -24,4 +24,6 @@ public interface StaffReadPlatformService {
Collection<StaffData> retrieveAllStaffInOfficeAndItsParentOfficeHierarchy(Long officeId, boolean loanOfficersOnly);
Collection<StaffData> retrieveAllStaff(String sqlSearch, Long officeId, boolean loanOfficersOnly, String status);
Object[] hasAssociatedItems(final Long staffId);
}
@@ -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<String> params = new ArrayList<String>();
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<Map<String, Object>> result = this.jdbcTemplate.queryForList(sql);
if (result != null) {
for (Map<String, Object> 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();
}
}
@@ -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); }