Merge pull request #495 from ShruthiRajaram/FINERACT-665

FINERACT-665 activation date validation for group and center
This commit is contained in:
MexinaD
2018-12-05 14:00:10 +03:00
committed by GitHub
4 changed files with 57 additions and 27 deletions
@@ -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<Group, Long>, JpaSpecificationExecutor<Group> {
Collection<Group> 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);
}
@@ -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 class GroupRepositoryWrapper {
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;
}
}
@@ -26,7 +26,6 @@ import java.util.List;
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.exception.PlatformApiDataValidati
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 @@ import com.google.gson.reflect.TypeToken;
public final class GroupingTypesDataValidator {
private final FromJsonHelper fromApiJsonHelper;
private final GroupRepositoryWrapper groupRepositoryWrapper;
private static final Set<String> 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<ApiParameterError> dataValidationErrors) {
if (!dataValidationErrors.isEmpty()) {
@@ -285,7 +289,7 @@ public final class GroupingTypesDataValidator {
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 final class GroupingTypesDataValidator {
final Long staffId = this.fromApiJsonHelper.extractLongNamed(GroupingTypesApiConstants.staffIdParamName, element);
baseDataValidator.reset().parameter(GroupingTypesApiConstants.staffIdParamName).value(staffId).integerGreaterThanZero();
}
LocalDate submittedOnDate = this.groupRepositoryWrapper.retrieveSubmittedOndate(centerId);
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();
}
}
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 final class GroupingTypesDataValidator {
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);
}
@@ -356,7 +356,7 @@ public class GroupingTypesWritePlatformServiceJpaRepositoryImpl implements 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 class GroupingTypesWritePlatformServiceJpaRepositoryImpl implements Group
@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);
}