diff --git a/api-docs/apiLive.htm b/api-docs/apiLive.htm index fb375e1b4..3bdcd0cad 100644 --- a/api-docs/apiLive.htm +++ b/api-docs/apiLive.htm @@ -213,9 +213,24 @@ function getBaseURL(docURL) GLOBAL CONFIGURATIONconfigurations Retrieve Global Configuration - Update Global Configuration + Update Global Configuration + + CODEScodes + Create a Code + List Codes + + + + + + codes/{codeId} + + Retrieve a Code + Update a Code + Delete a Code + @@ -2315,7 +2330,7 @@ Request Body: -   +  

Update Global Configuration

@@ -2336,6 +2351,125 @@ Request Body:
+   +
+
+

Codes

+

Code and code values: Codes represent a specific category of data, their code values are a specific instance of that category.

+

Codes are mostly system defined which means the code itself comes out of the box and cannot be modifed however its code values can be. e.g. 'Customer Identifier', it defaults to a code value of 'Passport' but could be 'Drivers License, National Id' etc

+

+
+ +   +
+
+

Create a Code

+

Creates a code. Codes created through api are always 'user defined' and so system defined is marked as false.

+
+
+ POST https://Domain Name/api/v1/codes + POST codes +Content-Type: application/json +Request Body: +{ + "name": "MyNewCode" +} + + { + "resourceId": 2 +} + +
+
+ +   +
+
+

Retrieve Codes

+

Returns the list of codes.

+

Example Requests:

+
codes
+
+
+ GET https://Domain Name/api/v1/codes + [ + { + "id": 1, + "name": "Customer Identifier", + "systemDefined": true + }, + { + "id": 2, + "name": "MyNewCode", + "systemDefined": false + } +] + +
+
+ +   +
+
+

Retrieve a Code

+

Returns the details of a Code.

+

Example Requests:

+
codes/1
+
+
+ GET https://Domain Name/api/v1/codes/1 + { + "id": 1, + "name": "Customer Identifier", + "systemDefined": true +} + +
+
+ +   +
+
+

Update a Code

+

Updates the details of a code if it is not system defined.

+
+
+ PUT https://Domain Name/api/v1/codes/2 + PUT codes/2 +Content-Type: application/json +Request Body: +{ + "name": "MyNewCode(changed)" +} + + { + "resourceId": 2, + "changes": { + "name": "MyNewCode(changed)" + } +} + +
+
+ +   +
+
+

Delete a Code

+

Deletes a code if it is not system defined.

+
+
+ DELETE https://Domain Name/api/v1/codes/2 + DELETE codes/2 +Content-Type: application/json + + { + "resourceId": 2 +} + +
+
+  
@@ -3424,9 +3558,24 @@ Request body: GLOBAL CONFIGURATIONconfigurations Retrieve Global Configuration - Update Global Configuration + Update Global Configuration + + CODEScodes + Create a Code + List Codes + + + + + + codes/{codeId} + + Retrieve a Code + Update a Code + Delete a Code + DATA TABLEdatatablesList Data Tables datatables/register/{datatable}/{apptable}Register Data Table datatables/deregister/{datatable}Deregister Data Table diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/command/CodeCommand.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/command/CodeCommand.java deleted file mode 100644 index ee29bec52..000000000 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/command/CodeCommand.java +++ /dev/null @@ -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; - -/** - *

Immutable command for creating or updating details of a code.

- */ -public class CodeCommand { - - private final String name; - - public CodeCommand(final String name) { - this.name = name; - } - - public void validateForCreate() { - List dataValidationErrors = new ArrayList(); - - 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 dataValidationErrors = new ArrayList(); - 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); } - } -} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/serialization/CodeCommandFromApiJsonDeserializer.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/serialization/CodeCommandFromApiJsonDeserializer.java index dd5d9967b..b3c956e1d 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/serialization/CodeCommandFromApiJsonDeserializer.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/serialization/CodeCommandFromApiJsonDeserializer.java @@ -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 { +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>() {}.getType(); fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, supportedParameters); - final JsonElement element = fromApiJsonHelper.parse(json); - final String name = fromApiJsonHelper.extractStringNamed("name", element); + final List dataValidationErrors = new ArrayList(); + 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>() {}.getType(); + fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, supportedParameters); + + final List dataValidationErrors = new ArrayList(); + 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 dataValidationErrors) { + if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", + "Validation errors exist.", dataValidationErrors); } } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/service/CodeWritePlatformServiceJpaRepositoryImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/service/CodeWritePlatformServiceJpaRepositoryImpl.java index 1d1cf4f62..07d27e9db 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/service/CodeWritePlatformServiceJpaRepositoryImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/service/CodeWritePlatformServiceJpaRepositoryImpl.java @@ -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 changes = code.update(command); + final Map 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;