From b001e3f9997a7b9ace2cacd54be532469338eea4 Mon Sep 17 00:00:00 2001 From: ismail Date: Thu, 29 Nov 2012 17:58:15 +0100 Subject: [PATCH] add system defined checks to codes --- ...PortfolioApiDataConversionServiceImpl.java | 77 ++++++++++--------- ...ioCommandDeerializerServiceGoogleGson.java | 49 ++++++------ .../exceptions/CodeExceptionRule.java | 9 +++ .../codes/command/CodeCommand.java | 18 +++-- .../infrastructure/codes/domain/Code.java | 10 ++- 5 files changed, 94 insertions(+), 69 deletions(-) create mode 100644 mifosng-provider/src/main/java/org/mifosng/platform/exceptions/CodeExceptionRule.java diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/PortfolioApiDataConversionServiceImpl.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/PortfolioApiDataConversionServiceImpl.java index cef669a72..02d14b03a 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/PortfolioApiDataConversionServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/PortfolioApiDataConversionServiceImpl.java @@ -1083,9 +1083,9 @@ public class PortfolioApiDataConversionServiceImpl implements PortfolioApiDataCo /* * private LocalDate convertFrom(final String dateAsString, final String * parameterName, final String dateFormat) { - * + * * if (StringUtils.isBlank(dateFormat)) { - * + * * List dataValidationErrors = new * ArrayList(); String defaultMessage = new * StringBuilder("The parameter '" + parameterName + @@ -1093,11 +1093,11 @@ public class PortfolioApiDataConversionServiceImpl implements PortfolioApiDataCo * ApiParameterError error = ApiParameterError.parameterError( * "validation.msg.missing.dateFormat.parameter", defaultMessage, * parameterName); dataValidationErrors.add(error); - * + * * throw new PlatformApiDataValidationException( * "validation.msg.validation.errors.exist", "Validation errors exist.", * dataValidationErrors); } - * + * * LocalDate eventLocalDate = null; if * (StringUtils.isNotBlank(dateAsString)) { try { Locale locale = * LocaleContextHolder.getLocale(); eventLocalDate = @@ -1110,18 +1110,18 @@ public class PortfolioApiDataConversionServiceImpl implements PortfolioApiDataCo * " is invalid based on the dateFormat provided:" + dateFormat, * parameterName, dateAsString, dateFormat); * dataValidationErrors.add(error); - * + * * throw new PlatformApiDataValidationException( * "validation.msg.validation.errors.exist", "Validation errors exist.", * dataValidationErrors); } } - * + * * return eventLocalDate; } - * + * * private Integer convertToInteger(final String numericalValueFormatted, * final String parameterName, final Locale clientApplicationLocale) { - * + * * if (clientApplicationLocale == null) { - * + * * List dataValidationErrors = new * ArrayList(); String defaultMessage = new * StringBuilder("The parameter '" + parameterName + @@ -1129,41 +1129,41 @@ public class PortfolioApiDataConversionServiceImpl implements PortfolioApiDataCo * ApiParameterError error = * ApiParameterError.parameterError("validation.msg.missing.locale.parameter" * , defaultMessage, parameterName); dataValidationErrors.add(error); - * + * * throw new PlatformApiDataValidationException( * "validation.msg.validation.errors.exist", "Validation errors exist.", * dataValidationErrors); } - * + * * try { Integer number = null; - * + * * if (StringUtils.isNotBlank(numericalValueFormatted)) { - * + * * String source = numericalValueFormatted.trim(); - * + * * NumberFormat format = NumberFormat.getInstance(clientApplicationLocale); * DecimalFormat df = (DecimalFormat) format; DecimalFormatSymbols symbols = * df.getDecimalFormatSymbols(); df.setParseBigDecimal(true); - * + * * // http://bugs.sun.com/view_bug.do?bug_id=4510618 char groupingSeparator * = symbols.getGroupingSeparator(); if (groupingSeparator == '\u00a0') { * source = source.replaceAll(" ", Character.toString('\u00a0')); } - * + * * Number parsedNumber = df.parse(source); - * + * * double parsedNumberDouble = parsedNumber.doubleValue(); int * parsedNumberInteger = parsedNumber.intValue(); - * + * * if (source.contains(Character.toString(symbols.getDecimalSeparator()))) { * throw new ParseException(source, 0); } - * + * * if * (!Double.valueOf(parsedNumberDouble).equals(Double.valueOf(Integer.valueOf * (parsedNumberInteger)))) { throw new ParseException(source, 0); } - * + * * number = parsedNumber.intValue(); } - * + * * return number; } catch (ParseException e) { - * + * * List dataValidationErrors = new * ArrayList(); ApiParameterError error = * ApiParameterError.parameterError( @@ -1173,16 +1173,16 @@ public class PortfolioApiDataConversionServiceImpl implements PortfolioApiDataCo * clientApplicationLocale.toString() + "].", parameterName, * numericalValueFormatted, clientApplicationLocale); * dataValidationErrors.add(error); - * + * * throw new PlatformApiDataValidationException( * "validation.msg.validation.errors.exist", "Validation errors exist.", * dataValidationErrors); } } - * + * * private BigDecimal convertFrom(final String numericalValueFormatted, * final String parameterName, final Locale clientApplicationLocale) { - * + * * if (clientApplicationLocale == null) { - * + * * List dataValidationErrors = new * ArrayList(); String defaultMessage = new * StringBuilder("The parameter '" + parameterName + @@ -1190,17 +1190,17 @@ public class PortfolioApiDataConversionServiceImpl implements PortfolioApiDataCo * ApiParameterError error = * ApiParameterError.parameterError("validation.msg.missing.locale.parameter" * , defaultMessage, parameterName); dataValidationErrors.add(error); - * + * * throw new PlatformApiDataValidationException( * "validation.msg.validation.errors.exist", "Validation errors exist.", * dataValidationErrors); } - * + * * try { BigDecimal number = null; - * + * * if (StringUtils.isNotBlank(numericalValueFormatted)) { - * + * * String source = numericalValueFormatted.trim(); - * + * * NumberFormat format = * NumberFormat.getNumberInstance(clientApplicationLocale); DecimalFormat df * = (DecimalFormat) format; DecimalFormatSymbols symbols = @@ -1208,14 +1208,14 @@ public class PortfolioApiDataConversionServiceImpl implements PortfolioApiDataCo * http://bugs.sun.com/view_bug.do?bug_id=4510618 char groupingSeparator = * symbols.getGroupingSeparator(); if (groupingSeparator == '\u00a0') { * source = source.replaceAll(" ", Character.toString('\u00a0')); } - * + * * NumberFormatter numberFormatter = new NumberFormatter(); Number * parsedNumber = numberFormatter.parse(source, clientApplicationLocale); * number = BigDecimal.valueOf(Double.valueOf(parsedNumber.doubleValue())); * } - * + * * return number; } catch (ParseException e) { - * + * * List dataValidationErrors = new * ArrayList(); ApiParameterError error = * ApiParameterError.parameterError( @@ -1225,7 +1225,7 @@ public class PortfolioApiDataConversionServiceImpl implements PortfolioApiDataCo * clientApplicationLocale.toString() + "].", parameterName, * numericalValueFormatted, clientApplicationLocale); * dataValidationErrors.add(error); - * + * * throw new PlatformApiDataValidationException( * "validation.msg.validation.errors.exist", "Validation errors exist.", * dataValidationErrors); } } @@ -1543,13 +1543,14 @@ public class PortfolioApiDataConversionServiceImpl implements PortfolioApiDataCo public CodeCommand convertApiRequestJsonToCodeCommand(final Long resourceIdentifier, final String json) { if (StringUtils.isBlank(json)) { throw new InvalidJsonException(); } - Type typeOfMap = new TypeToken>() {}.getType(); - Map requestMap = gsonConverter.fromJson(json, typeOfMap); + Type typeOfMap = new TypeToken>() {}.getType(); + Map requestMap = gsonConverter.fromJson(json, typeOfMap); - Set supportedParams = new HashSet(Arrays.asList("name")); + Set supportedParams = new HashSet(Arrays.asList("name","is_system_defined")); checkForUnsupportedParameters(requestMap, supportedParams); + // no difference between api request json format and internal command // json format. return this.portfolioCommandDeserializerService.deserializeCodeCommand(resourceIdentifier, json, false); diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/PortfolioCommandDeerializerServiceGoogleGson.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/PortfolioCommandDeerializerServiceGoogleGson.java index 63915ca26..553936682 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/PortfolioCommandDeerializerServiceGoogleGson.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/PortfolioCommandDeerializerServiceGoogleGson.java @@ -41,14 +41,14 @@ public class PortfolioCommandDeerializerServiceGoogleGson implements PortfolioCo parser = new JsonParser(); gsonConverter = new Gson(); } - + @Override public RoleCommand deserializeRoleCommand(final Long roleId, final String commandAsJson, final boolean makerCheckerApproval) { if (StringUtils.isBlank(commandAsJson)) { throw new InvalidJsonException(); } final JsonParserHelper helper = new JsonParserHelper(); final JsonElement element = parser.parse(commandAsJson); - + final Set parametersPassedInRequest = new HashSet(); final String name = helper.extractStringNamed("name", element, parametersPassedInRequest); @@ -60,16 +60,16 @@ public class PortfolioCommandDeerializerServiceGoogleGson implements PortfolioCo @Override public RolePermissionCommand deserializeRolePermissionCommand(final Long roleId, final String commandAsJson, final boolean makerCheckerApproval) { if (StringUtils.isBlank(commandAsJson)) { throw new InvalidJsonException(); } - + final RolePermissionCommand command = gsonConverter.fromJson(commandAsJson, RolePermissionCommand.class); return new RolePermissionCommand(roleId, command.getPermissions(), makerCheckerApproval); } - + @Override public PermissionsCommand deserializePermissionsCommand(final String commandAsJson, final boolean makerCheckerApproval) { if (StringUtils.isBlank(commandAsJson)) { throw new InvalidJsonException(); } - + final PermissionsCommand command = gsonConverter.fromJson(commandAsJson, PermissionsCommand.class); return new PermissionsCommand(command.getPermissions(), makerCheckerApproval); @@ -81,18 +81,18 @@ public class PortfolioCommandDeerializerServiceGoogleGson implements PortfolioCo final JsonParserHelper helper = new JsonParserHelper(); final JsonElement element = parser.parse(commandAsJson); - + final Set parametersPassedInRequest = new HashSet(); - + final String username = helper.extractStringNamed("username", element, parametersPassedInRequest); final String firstname = helper.extractStringNamed("firstname", element, parametersPassedInRequest); - + final String lastname = helper.extractStringNamed("lastname", element, parametersPassedInRequest); final String password = helper.extractStringNamed("password", element, parametersPassedInRequest); final String repeatPassword = helper.extractStringNamed("repeatPassword", element, parametersPassedInRequest); final String email = helper.extractStringNamed("email", element, parametersPassedInRequest); final Long officeId = helper.extractLongNamed("officeId", element, parametersPassedInRequest); - + // check array String[] notSelectedRoles = null; String[] roles = null; @@ -117,7 +117,7 @@ public class PortfolioCommandDeerializerServiceGoogleGson implements PortfolioCo } } - return new UserCommand(parametersPassedInRequest, makerCheckerApproval, userId, username, firstname, lastname, + return new UserCommand(parametersPassedInRequest, makerCheckerApproval, userId, username, firstname, lastname, password, repeatPassword, email, officeId, notSelectedRoles, roles); } @@ -127,22 +127,23 @@ public class PortfolioCommandDeerializerServiceGoogleGson implements PortfolioCo final JsonParserHelper helper = new JsonParserHelper(); final JsonElement element = parser.parse(commandAsJson); - + final Set parametersPassedInRequest = new HashSet(); final String name = helper.extractStringNamed("name", element, parametersPassedInRequest); + final boolean isSystemDefined = helper.extractBooleanNamed("isSystemDefined", element, parametersPassedInRequest); - return new CodeCommand(parametersPassedInRequest, makerCheckerApproval, codeId, name); + return new CodeCommand(parametersPassedInRequest, makerCheckerApproval, codeId, name,isSystemDefined); } @Override public StaffCommand deserializeStaffCommand(final Long staffId, final String commandAsJson, final boolean makerCheckerApproval) { - + if (StringUtils.isBlank(commandAsJson)) { throw new InvalidJsonException(); } final JsonParserHelper helper = new JsonParserHelper(); final JsonElement element = parser.parse(commandAsJson); - + final Set parametersPassedInRequest = new HashSet(); final String firstname = helper.extractStringNamed("firstname", element, parametersPassedInRequest); @@ -155,35 +156,35 @@ public class PortfolioCommandDeerializerServiceGoogleGson implements PortfolioCo @Override public FundCommand deserializeFundCommand(final Long fundId, final String commandAsJson, final boolean makerCheckerApproval) { - + if (StringUtils.isBlank(commandAsJson)) { throw new InvalidJsonException(); } final JsonParserHelper helper = new JsonParserHelper(); final JsonElement element = parser.parse(commandAsJson); - + final Set parametersPassedInRequest = new HashSet(); final String name = helper.extractStringNamed("name", element, parametersPassedInRequest); final String externalId = helper.extractStringNamed("externalId", element, parametersPassedInRequest); - + return new FundCommand(parametersPassedInRequest, makerCheckerApproval, fundId, name, externalId); } @Override public OfficeCommand deserializeOfficeCommand(final Long officeId, final String commandAsJson, final boolean makerCheckerApproval) { - + if (StringUtils.isBlank(commandAsJson)) { throw new InvalidJsonException(); } final JsonParserHelper helper = new JsonParserHelper(); final JsonElement element = parser.parse(commandAsJson); - + final Set parametersPassedInRequest = new HashSet(); final String name = helper.extractStringNamed("name", element, parametersPassedInRequest); final String externalId = helper.extractStringNamed("externalId", element, parametersPassedInRequest); final Long parentId = helper.extractLongNamed("parentId", element, parametersPassedInRequest); final LocalDate openingLocalDate = helper.extractLocalDateAsArrayNamed("openingDate", element, parametersPassedInRequest); - + return new OfficeCommand(parametersPassedInRequest, makerCheckerApproval, officeId, name, externalId, parentId, openingLocalDate); } @@ -194,7 +195,7 @@ public class PortfolioCommandDeerializerServiceGoogleGson implements PortfolioCo final JsonParserHelper helper = new JsonParserHelper(); final JsonElement element = parser.parse(commandAsJson); - + final Set parametersPassedInRequest = new HashSet(); final Long fromOfficeId = helper.extractLongNamed("fromOfficeId", element, parametersPassedInRequest); @@ -203,7 +204,7 @@ public class PortfolioCommandDeerializerServiceGoogleGson implements PortfolioCo final String currencyCode = helper.extractStringNamed("currencyCode", element, parametersPassedInRequest); final BigDecimal transactionAmount = helper.extractBigDecimalNamed("transactionAmount", element.getAsJsonObject(), Locale.US, parametersPassedInRequest); final String description = helper.extractStringNamed("description", element, parametersPassedInRequest); - + return new BranchMoneyTransferCommand(parametersPassedInRequest, makerCheckerApproval, fromOfficeId, toOfficeId, transactionLocalDate, currencyCode, transactionAmount, description); } @@ -214,11 +215,11 @@ public class PortfolioCommandDeerializerServiceGoogleGson implements PortfolioCo final JsonParserHelper helper = new JsonParserHelper(); final JsonElement element = parser.parse(commandAsJson); - + final Set parametersPassedInRequest = new HashSet(); final String[] currencies = helper.extractArrayNamed("currencies", element, parametersPassedInRequest); - + return new CurrencyCommand(makerCheckerApproval, currencies); } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/exceptions/CodeExceptionRule.java b/mifosng-provider/src/main/java/org/mifosng/platform/exceptions/CodeExceptionRule.java new file mode 100644 index 000000000..37f7d9a81 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosng/platform/exceptions/CodeExceptionRule.java @@ -0,0 +1,9 @@ +package org.mifosng.platform.exceptions; + +public class CodeExceptionRule extends AbstractPlatformDomainRuleException +{ + public CodeExceptionRule() + { + super("error.msg.code.systemdefined","This code is system defined and cannot be modified or deleted."); + } +} 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 index 0e158d2c3..7231f3d73 100644 --- 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 @@ -18,15 +18,21 @@ public class CodeCommand { private final transient Set parametersPassedInRequest; private final transient boolean makerCheckerApproval; private final transient Long id; + private final transient boolean isSystemDefined; - public CodeCommand(final Set modifiedParameters, final boolean makerCheckerApproval, final Long id, final String name) { + public CodeCommand(final Set modifiedParameters, final boolean makerCheckerApproval, final Long id, final String name,final boolean isSystemDefined) { this.parametersPassedInRequest = modifiedParameters; this.makerCheckerApproval = makerCheckerApproval; this.id = id; this.name = name; + this.isSystemDefined = isSystemDefined; } - public String getName() { + public boolean getisSystemDefined() { + return isSystemDefined; + } + + public String getName() { return this.name; } @@ -44,10 +50,10 @@ public class CodeCommand { 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); } } @@ -55,10 +61,10 @@ public class CodeCommand { public void validateForUpdate() { List dataValidationErrors = new ArrayList(); DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("code"); - + baseDataValidator.reset().parameter("id").value(this.id).notNull(); baseDataValidator.reset().parameter("name").value(this.name).ignoreIfNull().notBlank(); - + if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.", dataValidationErrors); } } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/domain/Code.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/domain/Code.java index 0fd7a2c46..9c4d5c69b 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/domain/Code.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/domain/Code.java @@ -18,7 +18,15 @@ public class Code extends AbstractPersistable { @Column(name = "code_name", length = 100) private String codeName; - public static Code createNew(final String codeName) { + @SuppressWarnings("unused") + @Column(name = "is_system_defined") + private boolean isSystemDefined; + + public boolean getisSystemDefined() { + return isSystemDefined; + } + + public static Code createNew(final String codeName) { return new Code(codeName); }