From 06fa26b2a28911b6a6a2ff1c22674314f8eabd22 Mon Sep 17 00:00:00 2001 From: Emmanuel Nnaa Date: Thu, 14 Jul 2016 16:54:31 +0200 Subject: [PATCH] commit for FINERACT-46 (Throw an exception if staff to be deactivated is attached to an entity with status enum id not equal to 600) --- .../staff/domain/StaffRepository.java | 28 +++++++++++++++++- .../StaffCannotBeDeactivatedException.java | 29 +++++++++++++++++++ ...WritePlatformServiceJpaRepositoryImpl.java | 12 ++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 fineract-provider/src/main/java/org/apache/fineract/organisation/staff/exception/StaffCannotBeDeactivatedException.java diff --git a/fineract-provider/src/main/java/org/apache/fineract/organisation/staff/domain/StaffRepository.java b/fineract-provider/src/main/java/org/apache/fineract/organisation/staff/domain/StaffRepository.java index e716a00abdb..4eaeb1d7340 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/organisation/staff/domain/StaffRepository.java +++ b/fineract-provider/src/main/java/org/apache/fineract/organisation/staff/domain/StaffRepository.java @@ -27,10 +27,36 @@ public interface StaffRepository extends JpaRepository, JpaSpecific public final static String FIND_BY_OFFICE_QUERY = "select s from Staff s where s.id = :id AND s.office.id = :officeId"; + public final static String IS_ANY_ACTIVE_ENTITY_ASSOCIATED_WITH_STAFF_QUERY = "select " + + "case when (sum(total) > 0) then true else false end " + + "from (" + + "(select count(mg.id) as total " + + "from m_staff ms " + + "join m_group mg on mg.staff_id = ms.id " + + "where ms.id = :staffId " + + "and mg.status_enum in (100,300)) " + + "union all " + + "(select count(ml.id) as total " + + "from m_loan ml " + + "join m_staff ms on ml.loan_officer_id = ms.id " + + "where ms.id = :staffId " + + "and ml.loan_status_id in (100,200,300,700)) " + + "union all " + + "(select count(msa.id) as total " + + "from m_savings_account msa " + + "join m_staff ms on msa.field_officer_id = ms.id " + + "where ms.id = :staffId and " + + "msa.status_enum in (100,300))) as t1 "; + /** * Find staff by officeid. */ @Query(FIND_BY_OFFICE_QUERY) public Staff findByOffice(@Param("id") Long id, @Param("officeId") Long officeId); -} \ No newline at end of file + /** + * Query finds if a staff is attached to active, pending group or loans or savings + */ + @Query(value=IS_ANY_ACTIVE_ENTITY_ASSOCIATED_WITH_STAFF_QUERY, nativeQuery = true) + public boolean isAnyActiveEntityAssociatedWithStaff(@Param("staffId") Long staffId); +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/organisation/staff/exception/StaffCannotBeDeactivatedException.java b/fineract-provider/src/main/java/org/apache/fineract/organisation/staff/exception/StaffCannotBeDeactivatedException.java new file mode 100644 index 00000000000..1629b07fcff --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/organisation/staff/exception/StaffCannotBeDeactivatedException.java @@ -0,0 +1,29 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.fineract.organisation.staff.exception; + +import org.apache.fineract.infrastructure.core.exception.AbstractPlatformDomainRuleException; + +public class StaffCannotBeDeactivatedException extends AbstractPlatformDomainRuleException { + private static final long serialVersionUID = -4991377873402585935L; + + public StaffCannotBeDeactivatedException(final Long id) { + super("error.mgs.staff.cannot.be.deactivated", "Staff with identifier " + id + " cannot be deactivated."); + } +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/organisation/staff/service/StaffWritePlatformServiceJpaRepositoryImpl.java b/fineract-provider/src/main/java/org/apache/fineract/organisation/staff/service/StaffWritePlatformServiceJpaRepositoryImpl.java index a8a6e4f627f..a9d3b2f88f3 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/organisation/staff/service/StaffWritePlatformServiceJpaRepositoryImpl.java +++ b/fineract-provider/src/main/java/org/apache/fineract/organisation/staff/service/StaffWritePlatformServiceJpaRepositoryImpl.java @@ -30,6 +30,7 @@ import org.apache.fineract.organisation.office.exception.OfficeNotFoundException; import org.apache.fineract.organisation.staff.domain.Staff; import org.apache.fineract.organisation.staff.domain.StaffRepository; +import org.apache.fineract.organisation.staff.exception.StaffCannotBeDeactivatedException; import org.apache.fineract.organisation.staff.exception.StaffNotFoundException; import org.apache.fineract.organisation.staff.serialization.StaffCommandFromApiJsonDeserializer; import org.slf4j.Logger; @@ -91,6 +92,17 @@ public CommandProcessingResult updateStaff(final Long staffId, final JsonCommand final Staff staffForUpdate = this.staffRepository.findOne(staffId); if (staffForUpdate == null) { throw new StaffNotFoundException(staffId); } + + final boolean isActive = command.booleanPrimitiveValueOfParameterNamed("isActive"); + + if(staffForUpdate.isActive() && !isActive) { + final boolean isAnyActiveEntityAssociatedWithStaff = this.staffRepository. + isAnyActiveEntityAssociatedWithStaff(staffId); + + if (isAnyActiveEntityAssociatedWithStaff) { + throw new StaffCannotBeDeactivatedException(staffId); + } + } final Map changesOnly = staffForUpdate.update(command);