update api docs around codes, update validation from JSON API request.

This commit is contained in:
Keith Woodlock
2013-01-28 10:16:12 +00:00
parent b9ba666bfb
commit 495ca15fad
4 changed files with 203 additions and 66 deletions
@@ -1,42 +0,0 @@
package org.mifosplatform.infrastructure.codes.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;
/**
* <p>Immutable command for creating or updating details of a code.</p>
*/
public class CodeCommand {
private final String name;
public CodeCommand(final String name) {
this.name = name;
}
public void validateForCreate() {
List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("code");
baseDataValidator.reset().parameter("name").value(this.name).notBlank();
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("code");
baseDataValidator.reset().parameter("name").value(this.name).notBlank();
baseDataValidator.reset().anyOfNotNull(this.name);
if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist",
"Validation errors exist.", dataValidationErrors); }
}
}
@@ -1,16 +1,18 @@
package org.mifosplatform.infrastructure.codes.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.codes.command.CodeCommand;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -19,10 +21,10 @@ import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
/**
* Implementation of {@link FromApiJsonDeserializer} for {@link CodeCommand}'s.
* Deserializer for code JSON to validate API request.
*/
@Component
public final class CodeCommandFromApiJsonDeserializer extends AbstractFromApiJsonDeserializer<CodeCommand> {
public final class CodeCommandFromApiJsonDeserializer {
/**
* The parameters supported for this command.
@@ -35,17 +37,43 @@ public final class CodeCommandFromApiJsonDeserializer extends AbstractFromApiJso
this.fromApiJsonHelper = fromApiJsonHelper;
}
@Override
public CodeCommand 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 List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("code");
return new CodeCommand(name);
final JsonElement element = fromApiJsonHelper.parse(json);
final String name = fromApiJsonHelper.extractStringNamed("name", element);
baseDataValidator.reset().parameter("name").value(name).notBlank().notExceedingLengthOf(100);
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(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); }
}
}
@@ -2,7 +2,6 @@ package org.mifosplatform.infrastructure.codes.service;
import java.util.Map;
import org.mifosplatform.infrastructure.codes.command.CodeCommand;
import org.mifosplatform.infrastructure.codes.domain.Code;
import org.mifosplatform.infrastructure.codes.domain.CodeRepository;
import org.mifosplatform.infrastructure.codes.exception.CodeNotFoundException;
@@ -44,9 +43,8 @@ public class CodeWritePlatformServiceJpaRepositoryImpl implements CodeWritePlatf
try {
context.authenticatedUser();
final CodeCommand codeCommand = this.fromApiJsonDeserializer.commandFromApiJson(command.json());
codeCommand.validateForCreate();
this.fromApiJsonDeserializer.validateForCreate(command.json());
final Code code = Code.fromJson(command);
this.codeRepository.save(code);
@@ -62,17 +60,21 @@ public class CodeWritePlatformServiceJpaRepositoryImpl implements CodeWritePlatf
try {
context.authenticatedUser();
final CodeCommand codeCommand = this.fromApiJsonDeserializer.commandFromApiJson(command.json());
codeCommand.validateForUpdate();
this.fromApiJsonDeserializer.validateForUpdate(command.json());
final Code code = retrieveCodeBy(codeId);
Map<String, Object> changes = code.update(command);
final Map<String, Object> changes = code.update(command);
if (!changes.isEmpty()) {
this.codeRepository.save(code);
}
return new CommandProcessingResultBuilder().withCommandId(command.commandId()).withEntityId(codeId).with(changes).build();
return new CommandProcessingResultBuilder() //
.withCommandId(command.commandId()) //
.withEntityId(codeId) //
.with(changes) //
.build();
} catch (DataIntegrityViolationException dve) {
handleCodeDataIntegrityIssues(command, dve);
return null;