From c4ba4c54ca6641d415d638dacd7f48ca86ade7c3 Mon Sep 17 00:00:00 2001 From: John Woodlock Date: Tue, 16 Apr 2013 02:25:46 +0100 Subject: [PATCH] WIP mifosx-195 create/update validation down to domain level from json checks + put report CRUD into config bucket --- ...eport_maint_perms_really_configuration.sql | 2 + .../dataqueries/domain/Report.java | 196 +++++++++++++----- .../ReportCommandFromApiJsonDeserializer.java | 105 +--------- .../ReportWritePlatformServiceImpl.java | 14 +- 4 files changed, 153 insertions(+), 164 deletions(-) create mode 100644 mifosng-db/migrations/core_db/V20__report_maint_perms_really_configuration.sql diff --git a/mifosng-db/migrations/core_db/V20__report_maint_perms_really_configuration.sql b/mifosng-db/migrations/core_db/V20__report_maint_perms_really_configuration.sql new file mode 100644 index 000000000..2667e83d9 --- /dev/null +++ b/mifosng-db/migrations/core_db/V20__report_maint_perms_really_configuration.sql @@ -0,0 +1,2 @@ + +update m_permission set grouping = 'configuration' where entity_name = 'report'; diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/dataqueries/domain/Report.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/dataqueries/domain/Report.java index c5efb1309..29755ef69 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/dataqueries/domain/Report.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/dataqueries/domain/Report.java @@ -5,7 +5,9 @@ */ package org.mifosplatform.infrastructure.dataqueries.domain; +import java.util.ArrayList; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; import javax.persistence.Column; @@ -15,6 +17,10 @@ import javax.persistence.UniqueConstraint; import org.apache.commons.lang.StringUtils; import org.mifosplatform.infrastructure.core.api.JsonCommand; +import org.mifosplatform.infrastructure.core.data.ApiParameterError; +import org.mifosplatform.infrastructure.core.data.DataValidatorBuilder; +import org.mifosplatform.infrastructure.core.exception.PlatformApiDataValidationException; +import org.mifosplatform.infrastructure.core.exception.PlatformDataIntegrityException; import org.springframework.data.jpa.domain.AbstractPersistable; @Entity @@ -50,6 +56,7 @@ public class Report extends AbstractPersistable { * @ManyToMany * * @JoinTable(name = "stretchy_report_parameter", joinColumns = + * * @JoinColumn(name = "reportn_id"), inverseJoinColumns = @JoinColumn(name = * "parameter_id")) private List charges; */ @@ -81,8 +88,8 @@ public class Report extends AbstractPersistable { public Report(final String reportName, final String reportType, final String reportSubType, final String reportCategory, - final String description, - final boolean useReport, final String reportSql) { + final String description, final boolean useReport, + final String reportSql) { this.reportName = reportName; this.reportType = reportType; this.reportSubType = reportSubType; @@ -91,60 +98,149 @@ public class Report extends AbstractPersistable { this.coreReport = false; this.useReport = useReport; this.reportSql = reportSql; + validate(); } - public Map update(final JsonCommand command) { + public Map update(final JsonCommand command) { - final Map actualChanges = new LinkedHashMap(8); + final Map actualChanges = new LinkedHashMap( + 8); - String paramName = "reportName"; - if (command.isChangeInStringParameterNamed(paramName, this.reportName)) { - final String newValue = command.stringValueOfParameterNamed(paramName); - actualChanges.put(paramName, newValue); - this.reportName = StringUtils.defaultIfEmpty(newValue, null); - } - paramName = "reportType"; - if (command.isChangeInStringParameterNamed(paramName, this.reportType)) { - final String newValue = command.stringValueOfParameterNamed(paramName); - actualChanges.put(paramName, newValue); - this.reportType = StringUtils.defaultIfEmpty(newValue, null); - } - paramName = "reportSubType"; - if (command.isChangeInStringParameterNamed(paramName, this.reportSubType)) { - final String newValue = command.stringValueOfParameterNamed(paramName); - actualChanges.put(paramName, newValue); - this.reportSubType = StringUtils.defaultIfEmpty(newValue, null); - } - paramName = "reportCategory"; - if (command.isChangeInStringParameterNamed(paramName, this.reportCategory)) { - final String newValue = command.stringValueOfParameterNamed(paramName); - actualChanges.put(paramName, newValue); - this.reportCategory = StringUtils.defaultIfEmpty(newValue, null); - } - paramName = "description"; - if (command.isChangeInStringParameterNamed(paramName, this.description)) { - final String newValue = command.stringValueOfParameterNamed(paramName); - actualChanges.put(paramName, newValue); - this.description = StringUtils.defaultIfEmpty(newValue, null); - } - paramName = "useReport"; - if (command.isChangeInBooleanParameterNamed(paramName, this.useReport)) { - final boolean newValue = command.booleanPrimitiveValueOfParameterNamed(paramName); - actualChanges.put(paramName, newValue); - this.useReport = newValue; - } - paramName = "reportSql"; - if (command.isChangeInStringParameterNamed(paramName, this.reportSql)) { - final String newValue = command.stringValueOfParameterNamed(paramName); - actualChanges.put(paramName, newValue); - this.reportSql = StringUtils.defaultIfEmpty(newValue, null); - } + String paramName = "reportName"; + if (command.isChangeInStringParameterNamed(paramName, this.reportName)) { + final String newValue = command + .stringValueOfParameterNamed(paramName); + actualChanges.put(paramName, newValue); + this.reportName = StringUtils.defaultIfEmpty(newValue, null); + } + paramName = "reportType"; + if (command.isChangeInStringParameterNamed(paramName, this.reportType)) { + final String newValue = command + .stringValueOfParameterNamed(paramName); + actualChanges.put(paramName, newValue); + this.reportType = StringUtils.defaultIfEmpty(newValue, null); + } + paramName = "reportSubType"; + if (command.isChangeInStringParameterNamed(paramName, + this.reportSubType)) { + final String newValue = command + .stringValueOfParameterNamed(paramName); + actualChanges.put(paramName, newValue); + this.reportSubType = StringUtils.defaultIfEmpty(newValue, null); + } + paramName = "reportCategory"; + if (command.isChangeInStringParameterNamed(paramName, + this.reportCategory)) { + final String newValue = command + .stringValueOfParameterNamed(paramName); + actualChanges.put(paramName, newValue); + this.reportCategory = StringUtils.defaultIfEmpty(newValue, null); + } + paramName = "description"; + if (command.isChangeInStringParameterNamed(paramName, this.description)) { + final String newValue = command + .stringValueOfParameterNamed(paramName); + actualChanges.put(paramName, newValue); + this.description = StringUtils.defaultIfEmpty(newValue, null); + } + paramName = "useReport"; + if (command.isChangeInBooleanParameterNamed(paramName, this.useReport)) { + final boolean newValue = command + .booleanPrimitiveValueOfParameterNamed(paramName); + actualChanges.put(paramName, newValue); + this.useReport = newValue; + } + paramName = "reportSql"; + if (command.isChangeInStringParameterNamed(paramName, this.reportSql)) { + final String newValue = command + .stringValueOfParameterNamed(paramName); + actualChanges.put(paramName, newValue); + this.reportSql = StringUtils.defaultIfEmpty(newValue, null); + } - return actualChanges; - } + validate(); + + if (!actualChanges.isEmpty()) { + if (isCoreReport()) { + for (final String key : actualChanges.keySet()) { + if (!(key.equals("useReport"))) { + throw new PlatformDataIntegrityException( + "error.msg.only.use.report.can.be.updated.for.core.report", + "Only the Use Report field can be updated for Core Reports", + key); + } + } + } + } + + return actualChanges; + } public boolean isCoreReport() { return coreReport; - } - + } + + private void validate() { + + final List dataValidationErrors = new ArrayList(); + final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder( + dataValidationErrors).resource("report"); + + baseDataValidator.reset().parameter("reportName") + .value(this.reportName).notBlank().notExceedingLengthOf(100); + + baseDataValidator + .reset() + .parameter("reportType") + .value(this.reportType) + .notBlank() + .notExceedingLengthOf(20) + .isOneOfTheseValues( + new Object[] { "Table", "Pentaho", "Chart" }); + + baseDataValidator.reset().parameter("reportSubType") + .value(this.reportSubType).notExceedingLengthOf(20); + + if (this.reportType.equals("Chart")) { + baseDataValidator + .reset() + .parameter("reportSubType") + .value(this.reportSubType) + .cantBeBlankWhenParameterProvidedIs("reportType", + this.reportType) + .isOneOfTheseValues(new Object[] { "Bar", "Pie" }); + } + + baseDataValidator.reset().parameter("reportCategory").value(this.reportCategory) + .notExceedingLengthOf(45); + + if (this.reportType != null) { + if ((this.reportType.equals("Table")) + || (this.reportType.equals("Chart"))) { + baseDataValidator + .reset() + .parameter("reportSql") + .value(this.reportSql) + .cantBeBlankWhenParameterProvidedIs("reportType", + this.reportType); + } else { + baseDataValidator + .reset() + .parameter("reportSql") + .value(this.reportSql) + .mustBeBlankWhenParameterProvidedIs("reportType", + this.reportType); + } + } + throwExceptionIfValidationWarningsExist(dataValidationErrors); + } + + private void throwExceptionIfValidationWarningsExist( + final List dataValidationErrors) { + 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/dataqueries/serialization/ReportCommandFromApiJsonDeserializer.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/dataqueries/serialization/ReportCommandFromApiJsonDeserializer.java index 01e8552ba..689df151c 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/dataqueries/serialization/ReportCommandFromApiJsonDeserializer.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/dataqueries/serialization/ReportCommandFromApiJsonDeserializer.java @@ -6,23 +6,17 @@ package org.mifosplatform.infrastructure.dataqueries.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.exception.PlatformApiDataValidationException; import org.mifosplatform.infrastructure.core.serialization.FromJsonHelper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -import com.google.gson.JsonElement; import com.google.gson.reflect.TypeToken; @Component @@ -33,7 +27,7 @@ public final class ReportCommandFromApiJsonDeserializer { */ private final Set supportedParameters = new HashSet( Arrays.asList("reportName", "reportType", "reportSubType", - "reportCategory", "reportSql", "useReport")); + "reportCategory", "description", "reportSql", "useReport")); private final FromJsonHelper fromApiJsonHelper; @@ -43,108 +37,15 @@ public final class ReportCommandFromApiJsonDeserializer { this.fromApiJsonHelper = fromApiJsonHelper; } - public void validate_for_create(final String json) { - validate(json, "create"); - } - - public void validate_for_update(final String json) { - validate(json, "update"); - } - - private void validate(final String json, final String commandType) { + public void validate(final String json) { if (StringUtils.isBlank(json)) { throw new InvalidJsonException(); } final Type typeOfMap = new TypeToken>() { }.getType(); + fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, supportedParameters); - // wip jpw - boolean isCreate = false; - if (commandType.equals("create")) - isCreate = true; - - final List dataValidationErrors = new ArrayList(); - final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder( - dataValidationErrors).resource("report"); - - final JsonElement element = fromApiJsonHelper.parse(json); - - String paramName = "reportName"; - String paramValueStr = fromApiJsonHelper.extractStringNamed(paramName, - element); - - baseDataValidator.reset().parameter(paramName).value(paramValueStr) - .notBlank().notExceedingLengthOf(100); - - String paramReportType = fromApiJsonHelper.extractStringNamed( - "reportType", element); - paramName = "reportType"; - paramValueStr = fromApiJsonHelper - .extractStringNamed(paramName, element); - baseDataValidator - .reset() - .parameter(paramName) - .value(paramValueStr) - .notBlank() - .notExceedingLengthOf(20) - .isOneOfTheseValues( - new Object[] { "Table", "Pentaho", "Chart" }); - - paramName = "reportSubType"; - paramValueStr = fromApiJsonHelper - .extractStringNamed(paramName, element); - baseDataValidator.reset().parameter(paramName).value(paramValueStr) - .notExceedingLengthOf(20); - if (paramReportType != null) { - if (paramReportType.equals("Chart")) { - baseDataValidator - .reset() - .parameter(paramName) - .value(paramValueStr) - .cantBeBlankWhenParameterProvidedIs("reportType", - paramReportType) - .isOneOfTheseValues(new Object[] { "Bar", "Pie" }); - } - } - - paramName = "reportCategory"; - paramValueStr = fromApiJsonHelper - .extractStringNamed(paramName, element); - baseDataValidator.reset().parameter(paramName).value(paramValueStr) - .notExceedingLengthOf(45); - - paramName = "reportSql"; - paramValueStr = fromApiJsonHelper - .extractStringNamed(paramName, element); - if (paramReportType != null) { - if ((paramReportType.equals("Table")) - || (paramReportType.equals("Chart"))) { - baseDataValidator - .reset() - .parameter(paramName) - .value(paramValueStr) - .cantBeBlankWhenParameterProvidedIs("reportType", - paramReportType); - } else { - baseDataValidator - .reset() - .parameter(paramName) - .value(paramValueStr) - .mustBeBlankWhenParameterProvidedIs("reportType", - paramReportType); - } - } - 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/dataqueries/service/ReportWritePlatformServiceImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/dataqueries/service/ReportWritePlatformServiceImpl.java index 953ae4014..2895e3f77 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/dataqueries/service/ReportWritePlatformServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/dataqueries/service/ReportWritePlatformServiceImpl.java @@ -51,7 +51,7 @@ public class ReportWritePlatformServiceImpl implements try { context.authenticatedUser(); - this.fromApiJsonDeserializer.validate_for_create(command.json()); + this.fromApiJsonDeserializer.validate(command.json()); final Report report = Report.fromJson(command); @@ -74,7 +74,7 @@ public class ReportWritePlatformServiceImpl implements try { context.authenticatedUser(); - this.fromApiJsonDeserializer.validate_for_update(command.json()); + this.fromApiJsonDeserializer.validate(command.json()); final Report report = this.reportRepository.findOne(reportId); if (report == null) { @@ -83,16 +83,6 @@ public class ReportWritePlatformServiceImpl implements final Map changes = report.update(command); if (!changes.isEmpty()) { - if (report.isCoreReport()) { - for (final String key : changes.keySet()) { - if (!(key.equals("useReport"))) { - throw new PlatformDataIntegrityException( - "error.msg.only.use.report.can.be.updated.for.core.report", - "Only the Use Report field can be updated for Core Reports", - key); - } - } - } this.reportRepository.saveAndFlush(report); }