update docs on funds api

This commit is contained in:
Keith Woodlock
2013-01-28 12:21:31 +00:00
parent 495ca15fad
commit 17f6e6ba66
5 changed files with 177 additions and 78 deletions
@@ -10,19 +10,14 @@ import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.mifosplatform.infrastructure.core.exception.InvalidJsonException;
import org.mifosplatform.infrastructure.core.serialization.AbstractFromApiJsonDeserializer;
import org.mifosplatform.infrastructure.core.serialization.FromApiJsonDeserializer;
import org.mifosplatform.infrastructure.core.serialization.FromJsonHelper;
import org.mifosplatform.portfolio.charge.command.ChargeDefinitionCommand;
import org.mifosplatform.portfolio.fund.command.FundCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
/**
* Implementation of {@link FromApiJsonDeserializer} for {@link FundCommand}'s.
*/
@Component
public final class ChargeDefinitionCommandFromApiJsonDeserializer extends AbstractFromApiJsonDeserializer<ChargeDefinitionCommand> {
@@ -1,48 +0,0 @@
package org.mifosplatform.portfolio.fund.command;
import java.util.ArrayList;
import java.util.List;
import org.mifosplatform.infrastructure.core.data.ApiParameterError;
import org.mifosplatform.infrastructure.core.data.DataValidatorBuilder;
import org.mifosplatform.infrastructure.core.exception.PlatformApiDataValidationException;
/**
* Immutable command for creating or updating details of a fund.
*/
public class FundCommand {
private final String name;
private final String externalId;
public FundCommand(final String fundName, final String externalId) {
this.name = fundName;
this.externalId = externalId;
}
public void validateForCreate() {
List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("fund");
baseDataValidator.reset().parameter("name").value(this.name).notBlank();
baseDataValidator.reset().parameter("externalId").value(this.externalId).notExceedingLengthOf(100);
if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist",
"Validation errors exist.", dataValidationErrors); }
}
public void validateForUpdate() {
List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("fund");
baseDataValidator.reset().parameter("name").value(this.name).ignoreIfNull().notBlank();
baseDataValidator.reset().parameter("externalId").value(this.externalId).ignoreIfNull().notExceedingLengthOf(100);
baseDataValidator.reset().anyOfNotNull(this.name, this.externalId);
if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist",
"Validation errors exist.", dataValidationErrors); }
}
}
@@ -1,28 +1,27 @@
package org.mifosplatform.portfolio.fund.serialization;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.mifosplatform.infrastructure.core.data.ApiParameterError;
import org.mifosplatform.infrastructure.core.data.DataValidatorBuilder;
import org.mifosplatform.infrastructure.core.exception.InvalidJsonException;
import org.mifosplatform.infrastructure.core.serialization.AbstractFromApiJsonDeserializer;
import org.mifosplatform.infrastructure.core.serialization.FromApiJsonDeserializer;
import org.mifosplatform.infrastructure.core.exception.PlatformApiDataValidationException;
import org.mifosplatform.infrastructure.core.serialization.FromJsonHelper;
import org.mifosplatform.portfolio.fund.command.FundCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
/**
* Implementation of {@link FromApiJsonDeserializer} for {@link FundCommand}'s.
*/
@Component
public final class FundCommandFromApiJsonDeserializer extends AbstractFromApiJsonDeserializer<FundCommand> {
public final class FundCommandFromApiJsonDeserializer {
/**
* The parameters supported for this command.
@@ -36,18 +35,51 @@ public final class FundCommandFromApiJsonDeserializer extends AbstractFromApiJso
this.fromApiJsonHelper = fromApiJsonHelper;
}
@Override
public FundCommand commandFromApiJson(final String json) {
public void validateForCreate(final String json) {
if (StringUtils.isBlank(json)) { throw new InvalidJsonException(); }
final Type typeOfMap = new TypeToken<Map<String, Object>>() {}.getType();
fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, supportedParameters);
final JsonElement element = fromApiJsonHelper.parse(json);
final String name = fromApiJsonHelper.extractStringNamed("name", element);
final String externalId = fromApiJsonHelper.extractStringNamed("externalId", element);
final List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("code");
return new FundCommand(name, externalId);
final JsonElement element = fromApiJsonHelper.parse(json);
final String name = fromApiJsonHelper.extractStringNamed("name", element);
baseDataValidator.reset().parameter("name").value(name).notBlank().notExceedingLengthOf(255);
final String externalId = fromApiJsonHelper.extractStringNamed("externalId", element);
baseDataValidator.reset().parameter("externalId").value(externalId).notExceedingLengthOf(255);
throwExceptionIfValidationWarningsExist(dataValidationErrors);
}
public void validateForUpdate(final String json) {
if (StringUtils.isBlank(json)) { throw new InvalidJsonException(); }
final Type typeOfMap = new TypeToken<Map<String, Object>>() {}.getType();
fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, supportedParameters);
final List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("code");
final JsonElement element = fromApiJsonHelper.parse(json);
if (fromApiJsonHelper.parameterExists("name", element)) {
final String name = fromApiJsonHelper.extractStringNamed("name", element);
baseDataValidator.reset().parameter("name").value(name).notBlank().notExceedingLengthOf(255);
}
if (fromApiJsonHelper.parameterExists("externalId", element)) {
final String externalId = fromApiJsonHelper.extractStringNamed("externalId", element);
baseDataValidator.reset().parameter("externalId").value(externalId).notExceedingLengthOf(100);
}
throwExceptionIfValidationWarningsExist(dataValidationErrors);
}
private void throwExceptionIfValidationWarningsExist(final List<ApiParameterError> dataValidationErrors) {
if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist",
"Validation errors exist.", dataValidationErrors); }
}
}
@@ -7,7 +7,6 @@ import org.mifosplatform.infrastructure.core.data.CommandProcessingResult;
import org.mifosplatform.infrastructure.core.data.CommandProcessingResultBuilder;
import org.mifosplatform.infrastructure.core.exception.PlatformDataIntegrityException;
import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext;
import org.mifosplatform.portfolio.fund.command.FundCommand;
import org.mifosplatform.portfolio.fund.domain.Fund;
import org.mifosplatform.portfolio.fund.domain.FundRepository;
import org.mifosplatform.portfolio.fund.exception.FundNotFoundException;
@@ -30,8 +29,7 @@ public class FundWritePlatformServiceJpaRepositoryImpl implements FundWritePlatf
@Autowired
public FundWritePlatformServiceJpaRepositoryImpl(final PlatformSecurityContext context,
final FundCommandFromApiJsonDeserializer fromApiJsonDeserializer,
final FundRepository fundRepository) {
final FundCommandFromApiJsonDeserializer fromApiJsonDeserializer, final FundRepository fundRepository) {
this.context = context;
this.fromApiJsonDeserializer = fromApiJsonDeserializer;
this.fundRepository = fundRepository;
@@ -43,11 +41,10 @@ public class FundWritePlatformServiceJpaRepositoryImpl implements FundWritePlatf
try {
context.authenticatedUser();
final FundCommand fundCommand = this.fromApiJsonDeserializer.commandFromApiJson(command.json());
fundCommand.validateForCreate();
Fund fund = Fund.fromJson(command);
this.fromApiJsonDeserializer.validateForCreate(command.json());
final Fund fund = Fund.fromJson(command);
this.fundRepository.save(fund);
@@ -64,13 +61,12 @@ public class FundWritePlatformServiceJpaRepositoryImpl implements FundWritePlatf
try {
context.authenticatedUser();
final FundCommand fundCommand = this.fromApiJsonDeserializer.commandFromApiJson(command.json());
fundCommand.validateForUpdate();
this.fromApiJsonDeserializer.validateForUpdate(command.json());
final Fund fund = this.fundRepository.findOne(fundId);
if (fund == null) { throw new FundNotFoundException(fundId); }
final Map<String, Object> changes = fund.update(command);
if (!changes.isEmpty()) {
this.fundRepository.saveAndFlush(fund);