diff --git a/fineract-provider/src/main/java/org/apache/fineract/portfolio/group/domain/GroupRepository.java b/fineract-provider/src/main/java/org/apache/fineract/portfolio/group/domain/GroupRepository.java index 5082033ffbf..6095a807fe8 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/portfolio/group/domain/GroupRepository.java +++ b/fineract-provider/src/main/java/org/apache/fineract/portfolio/group/domain/GroupRepository.java @@ -19,11 +19,20 @@ package org.apache.fineract.portfolio.group.domain; import java.util.Collection; +import java.util.Date; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; public interface GroupRepository extends JpaRepository, JpaSpecificationExecutor { Collection findByParentId(Long parentId); + + public static final String RETRIEVE_SUBMITTED_ON_DATE = "select g.submittedOnDate from Group g where g.id = :groupId"; + + @Query(RETRIEVE_SUBMITTED_ON_DATE) + Date retrieveGroupTypeSubmitteOndDate(@Param("groupId") Long groupId); + } diff --git a/fineract-provider/src/main/java/org/apache/fineract/portfolio/group/domain/GroupRepositoryWrapper.java b/fineract-provider/src/main/java/org/apache/fineract/portfolio/group/domain/GroupRepositoryWrapper.java index c8639134424..c83d739a56e 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/portfolio/group/domain/GroupRepositoryWrapper.java +++ b/fineract-provider/src/main/java/org/apache/fineract/portfolio/group/domain/GroupRepositoryWrapper.java @@ -18,8 +18,11 @@ */ package org.apache.fineract.portfolio.group.domain; +import java.util.Date; + import org.apache.fineract.organisation.office.domain.Office; import org.apache.fineract.portfolio.group.exception.GroupNotFoundException; +import org.joda.time.LocalDate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -66,4 +69,12 @@ public void delete(final Group entity) { public void flush() { this.repository.flush(); } + + public LocalDate retrieveSubmittedOndate(final Long groupId) { + Date submittedOnDate = this.repository.retrieveGroupTypeSubmitteOndDate(groupId); + if (submittedOnDate != null) { + return new LocalDate(submittedOnDate); + } + return null; + } } \ No newline at end of file diff --git a/fineract-provider/src/main/java/org/apache/fineract/portfolio/group/serialization/GroupingTypesDataValidator.java b/fineract-provider/src/main/java/org/apache/fineract/portfolio/group/serialization/GroupingTypesDataValidator.java index af0686066fb..fe7b9f2e05c 100755 --- a/fineract-provider/src/main/java/org/apache/fineract/portfolio/group/serialization/GroupingTypesDataValidator.java +++ b/fineract-provider/src/main/java/org/apache/fineract/portfolio/group/serialization/GroupingTypesDataValidator.java @@ -26,7 +26,6 @@ import java.util.Map; import java.util.Set; -import com.google.gson.JsonArray; import org.apache.commons.lang.StringUtils; import org.apache.fineract.infrastructure.core.api.JsonCommand; import org.apache.fineract.infrastructure.core.data.ApiParameterError; @@ -36,10 +35,12 @@ import org.apache.fineract.infrastructure.core.serialization.FromJsonHelper; import org.apache.fineract.portfolio.client.api.ClientApiConstants; import org.apache.fineract.portfolio.group.api.GroupingTypesApiConstants; +import org.apache.fineract.portfolio.group.domain.GroupRepositoryWrapper; import org.joda.time.LocalDate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.reflect.TypeToken; @@ -47,6 +48,7 @@ public final class GroupingTypesDataValidator { private final FromJsonHelper fromApiJsonHelper; + private final GroupRepositoryWrapper groupRepositoryWrapper; private static final Set CENTER_REQUEST_DATA_PARAMETERS = new HashSet<>( Arrays.asList(GroupingTypesApiConstants.localeParamName, GroupingTypesApiConstants.dateFormatParamName, @@ -75,9 +77,11 @@ public final class GroupingTypesDataValidator { GroupingTypesApiConstants.closureDateParamName, GroupingTypesApiConstants.closureReasonIdParamName)); @Autowired - public GroupingTypesDataValidator(final FromJsonHelper fromApiJsonHelper) { - this.fromApiJsonHelper = fromApiJsonHelper; - } + public GroupingTypesDataValidator(final FromJsonHelper fromApiJsonHelper, + final GroupRepositoryWrapper groupRepositoryWrapper) { + this.fromApiJsonHelper = fromApiJsonHelper; + this.groupRepositoryWrapper = groupRepositoryWrapper; + } private void throwExceptionIfValidationWarningsExist(final List dataValidationErrors) { if (!dataValidationErrors.isEmpty()) { @@ -285,7 +289,7 @@ public void validateForCreateGroup(final JsonCommand command) { throwExceptionIfValidationWarningsExist(dataValidationErrors); } - public void validateForUpdateCenter(final JsonCommand command) { + public void validateForUpdateCenter(final JsonCommand command, final Long centerId) { final String json = command.json(); if (StringUtils.isBlank(json)) { throw new InvalidJsonException(); } @@ -318,20 +322,23 @@ public void validateForUpdateCenter(final JsonCommand command) { final Long staffId = this.fromApiJsonHelper.extractLongNamed(GroupingTypesApiConstants.staffIdParamName, element); baseDataValidator.reset().parameter(GroupingTypesApiConstants.staffIdParamName).value(staffId).integerGreaterThanZero(); } - - final Boolean active = this.fromApiJsonHelper.extractBooleanNamed(GroupingTypesApiConstants.activeParamName, element); - if (active != null) { - if (active.booleanValue()) { - final LocalDate joinedDate = this.fromApiJsonHelper.extractLocalDateNamed( - GroupingTypesApiConstants.activationDateParamName, element); - baseDataValidator.reset().parameter(GroupingTypesApiConstants.activationDateParamName).value(joinedDate).notNull(); - } - } + + LocalDate submittedOnDate = this.groupRepositoryWrapper.retrieveSubmittedOndate(centerId); + + final Boolean active = this.fromApiJsonHelper.extractBooleanNamed(GroupingTypesApiConstants.activeParamName, + element); + if ((active != null && active) + || (this.fromApiJsonHelper.parameterExists(GroupingTypesApiConstants.activationDateParamName, element))) { + final LocalDate joinedDate = this.fromApiJsonHelper + .extractLocalDateNamed(GroupingTypesApiConstants.activationDateParamName, element); + baseDataValidator.reset().parameter(GroupingTypesApiConstants.activationDateParamName).value(joinedDate) + .notNull().validateDateAfter(submittedOnDate); + } throwExceptionIfValidationWarningsExist(dataValidationErrors); } - public void validateForUpdateGroup(final JsonCommand command) { + public void validateForUpdateGroup(final JsonCommand command, final Long groupId) { final String json = command.json(); if (StringUtils.isBlank(json)) { throw new InvalidJsonException(); } @@ -364,22 +371,25 @@ public void validateForUpdateGroup(final JsonCommand command) { final Long staffId = this.fromApiJsonHelper.extractLongNamed(GroupingTypesApiConstants.staffIdParamName, element); baseDataValidator.reset().parameter(GroupingTypesApiConstants.staffIdParamName).value(staffId).integerGreaterThanZero(); } - - final Boolean active = this.fromApiJsonHelper.extractBooleanNamed(GroupingTypesApiConstants.activeParamName, element); - if (active != null) { - if (active.booleanValue()) { - final LocalDate joinedDate = this.fromApiJsonHelper.extractLocalDateNamed( - GroupingTypesApiConstants.activationDateParamName, element); - baseDataValidator.reset().parameter(GroupingTypesApiConstants.activationDateParamName).value(joinedDate).notNull(); - } - } - + + LocalDate submittedOnDate = this.groupRepositoryWrapper.retrieveSubmittedOndate(groupId); + if (this.fromApiJsonHelper.parameterExists(GroupingTypesApiConstants.submittedOnDateParamName, element)) { - final LocalDate submittedOnDate = this.fromApiJsonHelper.extractLocalDateNamed( + submittedOnDate = this.fromApiJsonHelper.extractLocalDateNamed( GroupingTypesApiConstants.submittedOnDateParamName, element); baseDataValidator.reset().parameter(GroupingTypesApiConstants.submittedOnDateParamName).value(submittedOnDate).notNull(); } + final Boolean active = this.fromApiJsonHelper.extractBooleanNamed(GroupingTypesApiConstants.activeParamName, + element); + if ((active != null && active) || (this.fromApiJsonHelper + .parameterExists(GroupingTypesApiConstants.activationDateParamName, element))) { + final LocalDate joinedDate = this.fromApiJsonHelper + .extractLocalDateNamed(GroupingTypesApiConstants.activationDateParamName, element); + baseDataValidator.reset().parameter(GroupingTypesApiConstants.activationDateParamName).value(joinedDate) + .notNull().validateDateAfter(submittedOnDate); + } + throwExceptionIfValidationWarningsExist(dataValidationErrors); } diff --git a/fineract-provider/src/main/java/org/apache/fineract/portfolio/group/service/GroupingTypesWritePlatformServiceJpaRepositoryImpl.java b/fineract-provider/src/main/java/org/apache/fineract/portfolio/group/service/GroupingTypesWritePlatformServiceJpaRepositoryImpl.java index 2616c27cb3e..00098e7035e 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/portfolio/group/service/GroupingTypesWritePlatformServiceJpaRepositoryImpl.java +++ b/fineract-provider/src/main/java/org/apache/fineract/portfolio/group/service/GroupingTypesWritePlatformServiceJpaRepositoryImpl.java @@ -356,7 +356,7 @@ public void validateGroupRulesBeforeClientAssociation(final Group group) { @Override public CommandProcessingResult updateCenter(final Long centerId, final JsonCommand command) { - this.fromApiJsonDeserializer.validateForUpdateCenter(command); + this.fromApiJsonDeserializer.validateForUpdateCenter(command, centerId); return updateGroupingType(centerId, command, GroupTypes.CENTER); } @@ -365,7 +365,7 @@ public CommandProcessingResult updateCenter(final Long centerId, final JsonComma @Override public CommandProcessingResult updateGroup(final Long groupId, final JsonCommand command) { - this.fromApiJsonDeserializer.validateForUpdateGroup(command); + this.fromApiJsonDeserializer.validateForUpdateGroup(command, groupId); return updateGroupingType(groupId, command, GroupTypes.GROUP); }