Skip to content

Commit

Permalink
FINERACT-665 activation date validation for group and center
Browse files Browse the repository at this point in the history
  • Loading branch information
ShruthiRajaram committed Nov 30, 2018
1 parent 989e1f0 commit 6b38a12
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,17 +35,20 @@
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;

@Component
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,
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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(); }
Expand Down Expand Up @@ -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(); }
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down

0 comments on commit 6b38a12

Please sign in to comment.