diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/service/TomcatJdbcDataSourcePerTenantService.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/service/TomcatJdbcDataSourcePerTenantService.java index 3bcc54014..c3ee791de 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/service/TomcatJdbcDataSourcePerTenantService.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/service/TomcatJdbcDataSourcePerTenantService.java @@ -5,8 +5,8 @@ */ package org.mifosplatform.infrastructure.core.service; +import java.util.HashMap; import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; import javax.sql.DataSource; @@ -28,7 +28,7 @@ import org.springframework.stereotype.Service; @Service public class TomcatJdbcDataSourcePerTenantService implements DataSourcePerTenantService { - private final Map tenantToDataSourceMap = new ConcurrentHashMap(1); + private final Map tenantToDataSourceMap = new HashMap(1); private final DataSource tenantDataSource; @Autowired @@ -44,9 +44,9 @@ public class TomcatJdbcDataSourcePerTenantService implements DataSourcePerTenant MifosPlatformTenant tenant = ThreadLocalContextUtil.getTenant(); if (tenant != null) { - synchronized(this.tenantToDataSourceMap) - { - // if tenant information available switch to appropriate datasource + synchronized (this.tenantToDataSourceMap) { + // if tenant information available switch to appropriate + // datasource // for that tenant. if (this.tenantToDataSourceMap.containsKey(tenant.getId())) { tenantDataSource = this.tenantToDataSourceMap.get(tenant.getId()); 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 7e058de03..4d621f737 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 @@ -6,7 +6,6 @@ package org.mifosplatform.infrastructure.dataqueries.domain; import java.util.ArrayList; -import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; @@ -34,323 +33,222 @@ import com.google.gson.JsonArray; @Entity @Table(name = "stretchy_report", uniqueConstraints = { @UniqueConstraint(columnNames = { "report_name" }, name = "unq_report_name") }) -public class Report extends AbstractPersistable { +public final class Report extends AbstractPersistable { - @Column(name = "report_name", nullable = false, unique = true) - private String reportName; + @Column(name = "report_name", nullable = false, unique = true) + private String reportName; - @Column(name = "report_type", nullable = false) - private String reportType; + @Column(name = "report_type", nullable = false) + private String reportType; - @Column(name = "report_subtype") - private String reportSubType; + @Column(name = "report_subtype") + private String reportSubType; - @Column(name = "report_category") - private String reportCategory; + @Column(name = "report_category") + private String reportCategory; - @Column(name = "description") - private String description; + @Column(name = "description") + private String description; - @Column(name = "core_report", nullable = false) - private boolean coreReport; + @Column(name = "core_report", nullable = false) + private boolean coreReport; - // only defines if report should appear in reference app UI List - @Column(name = "use_report", nullable = false) - private boolean useReport; + // only defines if report should appear in reference app UI List + @Column(name = "use_report", nullable = false) + private boolean useReport; - @Column(name = "report_sql") - private String reportSql; + @Column(name = "report_sql") + private String reportSql; - @LazyCollection(LazyCollectionOption.FALSE) - @OneToMany(cascade = CascadeType.ALL, mappedBy = "report", orphanRemoval = true) - private Set reportParameterUsages = new HashSet(); + @LazyCollection(LazyCollectionOption.FALSE) + @OneToMany(cascade = CascadeType.ALL, mappedBy = "report", orphanRemoval = true) + private final Set reportParameterUsages = new HashSet(); - public static Report fromJson(final JsonCommand command) { + public static Report fromJson(final JsonCommand command) { - String reportName = null; - String reportType = null; - String reportSubType = null; - String reportCategory = null; - String description = null; - boolean useReport = false; - String reportSql = null; + String reportName = null; + String reportType = null; + String reportSubType = null; + String reportCategory = null; + String description = null; + boolean useReport = false; + String reportSql = null; - if (command.parameterExists("reportName")) - reportName = command.stringValueOfParameterNamed("reportName"); - if (command.parameterExists("reportType")) - reportType = command.stringValueOfParameterNamed("reportType"); - if (command.parameterExists("reportSubType")) - reportSubType = command - .stringValueOfParameterNamed("reportSubType"); - if (command.parameterExists("reportCategory")) - reportCategory = command - .stringValueOfParameterNamed("reportCategory"); - if (command.parameterExists("description")) - description = command.stringValueOfParameterNamed("description"); - if (command.parameterExists("useReport")) - useReport = command - .booleanPrimitiveValueOfParameterNamed("useReport"); - if (command.parameterExists("reportSql")) - reportSql = command.stringValueOfParameterNamed("reportSql"); + if (command.parameterExists("reportName")) reportName = command.stringValueOfParameterNamed("reportName"); + if (command.parameterExists("reportType")) reportType = command.stringValueOfParameterNamed("reportType"); + if (command.parameterExists("reportSubType")) reportSubType = command.stringValueOfParameterNamed("reportSubType"); + if (command.parameterExists("reportCategory")) reportCategory = command.stringValueOfParameterNamed("reportCategory"); + if (command.parameterExists("description")) description = command.stringValueOfParameterNamed("description"); + if (command.parameterExists("useReport")) useReport = command.booleanPrimitiveValueOfParameterNamed("useReport"); + if (command.parameterExists("reportSql")) reportSql = command.stringValueOfParameterNamed("reportSql"); - return new Report(reportName, reportType, reportSubType, - reportCategory, description, useReport, reportSql); - } + return new Report(reportName, reportType, reportSubType, reportCategory, description, useReport, reportSql); + } - protected Report() { - // - } + protected Report() { + // + } - public Report(final String reportName, final String reportType, - final String reportSubType, final String reportCategory, - final String description, final boolean useReport, - final String reportSql) { - this.reportName = reportName; - this.reportType = reportType; - this.reportSubType = reportSubType; - this.reportCategory = reportCategory; - this.description = description; - this.coreReport = false; - this.useReport = useReport; - this.reportSql = reportSql; - validate(); - } + public Report(final String reportName, final String reportType, final String reportSubType, final String reportCategory, + final String description, final boolean useReport, final String reportSql) { + this.reportName = reportName; + this.reportType = reportType; + this.reportSubType = reportSubType; + this.reportCategory = reportCategory; + this.description = description; + 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); - } - - final String reportParametersParamName = "reportParameters"; - if (command.hasParameter(reportParametersParamName)) { - JsonArray jsonArray = command - .arrayOfParameterNamed(reportParametersParamName); - if (jsonArray != null) { - actualChanges.put(reportParametersParamName, - command.jsonFragment(reportParametersParamName)); - } - } - - 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() - .isOneOfTheseValues( - new Object[] { "Table", "Pentaho", "Chart" }); - - baseDataValidator.reset().parameter("reportSubType") - .value(this.reportSubType).notExceedingLengthOf(20); - - if (StringUtils.isNotBlank(this.reportType)) { - if (this.reportType.equals("Chart")) { - baseDataValidator - .reset() - .parameter("reportSubType") - .value(this.reportSubType) - .cantBeBlankWhenParameterProvidedIs("reportType", - this.reportType) - .isOneOfTheseValues(new Object[] { "Bar", "Pie" }); - } else { - baseDataValidator - .reset() - .parameter("reportSubType") - .value(this.reportSubType) - .mustBeBlankWhenParameterProvidedIs("reportType", - this.reportType); - } - } - - baseDataValidator.reset().parameter("reportCategory") - .value(this.reportCategory).notExceedingLengthOf(45); - - if (StringUtils.isNotBlank(this.reportType)) { - 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); - } - } - - public String getReportName() { - return reportName; - } - - public Set getReportParameterUsages() { - return reportParameterUsages; - } - - public void setReportParameterUsages( - Set reportParameterUsages) { - this.reportParameterUsages = reportParameterUsages; - } - - public boolean update( - final Set newReportParameterUsages) { - if (newReportParameterUsages == null) - return false; - - boolean updated = false; - - if (changeInReportParameters(newReportParameterUsages)) { - updated = true; - this.reportParameterUsages.clear(); - this.reportParameterUsages.addAll(newReportParameterUsages); - } - return updated; - } - - private boolean changeInReportParameters( - Set newReportParameterUsages) { - - if (!(this.reportParameterUsages.equals(newReportParameterUsages))) return true; - - Map currentRPUs = new HashMap(); - for (ReportParameterUsage rpu : this.reportParameterUsages) { - currentRPUs.put(rpu.getId(), rpu); - } - - Map newRPUs = new HashMap(); - for (ReportParameterUsage rpu : newReportParameterUsages) { - newRPUs.put(rpu.getId(), rpu); - } - - ReportParameterUsage currentRPU = null; - ReportParameterUsage newRPU = null; - for (Long key : currentRPUs.keySet()) { - currentRPU = currentRPUs.get(key); - newRPU = newRPUs.get(key); - - if (existingEntryChanged(currentRPU, newRPU)) return true; + 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 false; - } - - private boolean existingEntryChanged( - final ReportParameterUsage currentRPU, final ReportParameterUsage newRPU) { + final String reportParametersParamName = "reportParameters"; + if (command.hasParameter(reportParametersParamName)) { + JsonArray jsonArray = command.arrayOfParameterNamed(reportParametersParamName); + if (jsonArray != null) { + actualChanges.put(reportParametersParamName, command.jsonFragment(reportParametersParamName)); + } + } - if (!(currentRPU.getParameter().getId() - .equals(newRPU.getParameter().getId()))) - return true; + validate(); - if ((currentRPU.getReportParameterName() == null) - && (newRPU.getReportParameterName() == null)) - return false; + 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); } + } + } + } - if (!(currentRPU.getReportParameterName() - .equals(newRPU.getReportParameterName()))) - return true; + return actualChanges; + } - return false; - } - -} + public boolean isCoreReport() { + return coreReport; + } + + public ReportParameterUsage findReportParameterById(final Long reportParameterId) { + ReportParameterUsage reportParameterUsage = null; + for (ReportParameterUsage rpu : this.reportParameterUsages) { + if (rpu.hasIdOf(reportParameterId)) { + reportParameterUsage = rpu; + break; + } + } + return reportParameterUsage; + } + + 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() + .isOneOfTheseValues(new Object[] { "Table", "Pentaho", "Chart" }); + + baseDataValidator.reset().parameter("reportSubType").value(this.reportSubType).notExceedingLengthOf(20); + + if (StringUtils.isNotBlank(this.reportType)) { + if (this.reportType.equals("Chart")) { + baseDataValidator.reset().parameter("reportSubType").value(this.reportSubType) + .cantBeBlankWhenParameterProvidedIs("reportType", this.reportType) + .isOneOfTheseValues(new Object[] { "Bar", "Pie" }); + } else { + baseDataValidator.reset().parameter("reportSubType").value(this.reportSubType) + .mustBeBlankWhenParameterProvidedIs("reportType", this.reportType); + } + } + + baseDataValidator.reset().parameter("reportCategory").value(this.reportCategory).notExceedingLengthOf(45); + + if (StringUtils.isNotBlank(this.reportType)) { + 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(dataValidationErrors); } + } + + public String getReportName() { + return reportName; + } + + public boolean update(final Set newReportParameterUsages) { + if (newReportParameterUsages == null) return false; + + boolean updated = false; + + if (changeInReportParameters(newReportParameterUsages)) { + updated = true; + this.reportParameterUsages.clear(); + this.reportParameterUsages.addAll(newReportParameterUsages); + } + return updated; + } + + private boolean changeInReportParameters(final Set newReportParameterUsages) { + + if (!(this.reportParameterUsages.equals(newReportParameterUsages))) return true; + + return false; + } +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/dataqueries/domain/ReportParameter.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/dataqueries/domain/ReportParameter.java index e795cd30d..15529aaa1 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/dataqueries/domain/ReportParameter.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/dataqueries/domain/ReportParameter.java @@ -14,8 +14,11 @@ import org.springframework.data.jpa.domain.AbstractPersistable; @Table(name = "stretchy_parameter") public class ReportParameter extends AbstractPersistable { - protected ReportParameter() { - // - } + protected ReportParameter() { + // + } -} + public boolean hasIdOf(final Long id) { + return getId().equals(id); + } +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/dataqueries/domain/ReportParameterUsage.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/dataqueries/domain/ReportParameterUsage.java index 57f3c2aae..eb705e52f 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/dataqueries/domain/ReportParameterUsage.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/dataqueries/domain/ReportParameterUsage.java @@ -11,63 +11,68 @@ import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; +import org.apache.commons.lang.builder.EqualsBuilder; +import org.apache.commons.lang.builder.HashCodeBuilder; import org.springframework.data.jpa.domain.AbstractPersistable; @Entity @Table(name = "stretchy_report_parameter") -public class ReportParameterUsage extends AbstractPersistable { - - @SuppressWarnings("unused") - @ManyToOne(optional = false) - @JoinColumn(name = "report_id", nullable = false) - private Report report; +public final class ReportParameterUsage extends AbstractPersistable { - @SuppressWarnings("unused") - @ManyToOne(optional = false) - @JoinColumn(name = "parameter_id", nullable = false) - private ReportParameter parameter; + @ManyToOne(optional = false) + @JoinColumn(name = "report_id", nullable = false) + private Report report; - @SuppressWarnings("unused") - @Column(name = "report_parameter_name") - private String reportParameterName; + @ManyToOne(optional = false) + @JoinColumn(name = "parameter_id", nullable = false) + private ReportParameter parameter; - protected ReportParameterUsage() { - // - } + @Column(name = "report_parameter_name") + private String reportParameterName; - public ReportParameterUsage(final Report report, - final ReportParameter parameter, final String reportParameterName) { - this.report = report; - this.parameter = parameter; - this.reportParameterName = reportParameterName; - } - - public ReportParameterUsage(final ReportParameterUsage currentReportParameterUsage, final Report report, - final ReportParameter parameter, final String reportParameterName) { - this.setId(currentReportParameterUsage.getId()); - this.report = report; - this.parameter = parameter; - this.reportParameterName = reportParameterName; - } + protected ReportParameterUsage() { + // + } - public void setReport(Report report) { - this.report = report; - } + public ReportParameterUsage(final Report report, final ReportParameter parameter, final String reportParameterName) { + this.report = report; + this.parameter = parameter; + this.reportParameterName = reportParameterName; + } - public void setParameter(ReportParameter parameter) { - this.parameter = parameter; - } + @Override + public boolean equals(final Object obj) { + if (obj == null) { return false; } + if (obj == this) { return true; } + if (obj.getClass() != getClass()) { return false; } + ReportParameterUsage rhs = (ReportParameterUsage) obj; + return new EqualsBuilder().appendSuper(super.equals(obj)) // + .append(this.getId(), rhs.getId()) // + .append(this.report.getId(), rhs.report.getId()) // + .append(this.parameter.getId(), rhs.parameter.getId()) // + .append(this.reportParameterName, rhs.reportParameterName) // + .isEquals(); + } - public ReportParameter getParameter() { - return parameter; - } + @Override + public int hashCode() { + return new HashCodeBuilder(3, 5) // + .append(this.getId()) // + .append(this.report.getId()) // + .append(this.parameter.getId()) // + .append(this.reportParameterName) // + .toHashCode(); + } - public String getReportParameterName() { - return reportParameterName; - } + public boolean hasIdOf(final Long id) { + return getId().equals(id); + } - public void setReportParameterName(String reportParameterName) { - this.reportParameterName = reportParameterName; - } - -} + public boolean hasParameterIdOf(final Long parameterId) { + return this.parameter != null && this.parameter.hasIdOf(parameterId); + } + + public void updateParameterName(final String parameterName) { + this.reportParameterName = parameterName; + } +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/dataqueries/domain/ReportParameterUsageRepository.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/dataqueries/domain/ReportParameterUsageRepository.java new file mode 100644 index 000000000..c60a803b7 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/dataqueries/domain/ReportParameterUsageRepository.java @@ -0,0 +1,14 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.mifosplatform.infrastructure.dataqueries.domain; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; + +public interface ReportParameterUsageRepository extends JpaRepository, + JpaSpecificationExecutor { + // no added behaviour +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/dataqueries/service/ReportWritePlatformService.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/dataqueries/service/ReportWritePlatformService.java index 19e6de22f..908c3756f 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/dataqueries/service/ReportWritePlatformService.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/dataqueries/service/ReportWritePlatformService.java @@ -13,6 +13,6 @@ public interface ReportWritePlatformService { CommandProcessingResult createReport(JsonCommand command); CommandProcessingResult updateReport(Long reportId, JsonCommand command); - + CommandProcessingResult deleteReport(Long reportId); } \ 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 7bbc01a50..48beb0304 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 @@ -9,6 +9,7 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; +import org.apache.commons.lang.StringUtils; import org.mifosplatform.infrastructure.core.api.JsonCommand; import org.mifosplatform.infrastructure.core.data.CommandProcessingResult; import org.mifosplatform.infrastructure.core.data.CommandProcessingResultBuilder; @@ -17,6 +18,7 @@ import org.mifosplatform.infrastructure.dataqueries.domain.Report; import org.mifosplatform.infrastructure.dataqueries.domain.ReportParameter; import org.mifosplatform.infrastructure.dataqueries.domain.ReportParameterRepository; import org.mifosplatform.infrastructure.dataqueries.domain.ReportParameterUsage; +import org.mifosplatform.infrastructure.dataqueries.domain.ReportParameterUsageRepository; import org.mifosplatform.infrastructure.dataqueries.domain.ReportRepository; import org.mifosplatform.infrastructure.dataqueries.exception.ReportNotFoundException; import org.mifosplatform.infrastructure.dataqueries.exception.ReportParameterNotFoundException; @@ -36,254 +38,204 @@ import com.google.gson.JsonArray; import com.google.gson.JsonObject; @Service -public class ReportWritePlatformServiceImpl implements - ReportWritePlatformService { +public class ReportWritePlatformServiceImpl implements ReportWritePlatformService { - private final static Logger logger = LoggerFactory - .getLogger(ReportWritePlatformServiceImpl.class); + private final static Logger logger = LoggerFactory.getLogger(ReportWritePlatformServiceImpl.class); - private final PlatformSecurityContext context; - private final ReportCommandFromApiJsonDeserializer fromApiJsonDeserializer; - private final PermissionRepository permissionRepository; - private final ReportRepository reportRepository; - private final ReportParameterRepository reportParameterRepository; + private final PlatformSecurityContext context; + private final ReportCommandFromApiJsonDeserializer fromApiJsonDeserializer; + private final ReportRepository reportRepository; + private final ReportParameterUsageRepository reportParameterUsageRepository; + private final ReportParameterRepository reportParameterRepository; + private final PermissionRepository permissionRepository; - @Autowired - public ReportWritePlatformServiceImpl( - final PlatformSecurityContext context, - final ReportCommandFromApiJsonDeserializer fromApiJsonDeserializer, - final ReportRepository reportRepository, - final ReportParameterRepository reportParameterRepository, - final PermissionRepository permissionRepository) { - this.context = context; - this.fromApiJsonDeserializer = fromApiJsonDeserializer; - this.reportRepository = reportRepository; - this.reportParameterRepository = reportParameterRepository; - this.permissionRepository = permissionRepository; - } + @Autowired + public ReportWritePlatformServiceImpl(final PlatformSecurityContext context, + final ReportCommandFromApiJsonDeserializer fromApiJsonDeserializer, final ReportRepository reportRepository, + final ReportParameterRepository reportParameterRepository, final ReportParameterUsageRepository reportParameterUsageRepository, + final PermissionRepository permissionRepository) { + this.context = context; + this.fromApiJsonDeserializer = fromApiJsonDeserializer; + this.reportRepository = reportRepository; + this.reportParameterRepository = reportParameterRepository; + this.reportParameterUsageRepository = reportParameterUsageRepository; + this.permissionRepository = permissionRepository; + } - @Transactional - @Override - public CommandProcessingResult createReport(final JsonCommand command) { + @Transactional + @Override + public CommandProcessingResult createReport(final JsonCommand command) { - try { - context.authenticatedUser(); + try { + context.authenticatedUser(); - this.fromApiJsonDeserializer.validate(command.json()); + this.fromApiJsonDeserializer.validate(command.json()); - final Report report = Report.fromJson(command); - final Set reportParameterUsages = assembleSetOfReportParameterUsages( - report, command); - if (reportParameterUsages != null) { - report.setReportParameterUsages(reportParameterUsages); - } - this.reportRepository.save(report); + final Report report = Report.fromJson(command); + final Set reportParameterUsages = assembleSetOfReportParameterUsages(report, command); + report.update(reportParameterUsages); - final Permission permission = new Permission("report", - report.getReportName(), "READ"); - this.permissionRepository.save(permission); + this.reportRepository.save(report); - return new CommandProcessingResultBuilder() - .withCommandId(command.commandId()) - .withEntityId(report.getId()).build(); - } catch (DataIntegrityViolationException dve) { - handleReportDataIntegrityIssues(command, dve); - return CommandProcessingResult.empty(); - } - } + final Permission permission = new Permission("report", report.getReportName(), "READ"); + this.permissionRepository.save(permission); - @Transactional - @Override - public CommandProcessingResult updateReport(final Long reportId, - final JsonCommand command) { + return new CommandProcessingResultBuilder() // + .withCommandId(command.commandId()) // + .withEntityId(report.getId()) // + .build(); + } catch (DataIntegrityViolationException dve) { + handleReportDataIntegrityIssues(command, dve); + return CommandProcessingResult.empty(); + } + } - try { - context.authenticatedUser(); + @Transactional + @Override + public CommandProcessingResult updateReport(final Long reportId, final JsonCommand command) { - this.fromApiJsonDeserializer.validate(command.json()); + try { + context.authenticatedUser(); - final Report report = this.reportRepository.findOne(reportId); - if (report == null) { - throw new ReportNotFoundException(reportId); - } + this.fromApiJsonDeserializer.validate(command.json()); - final Map changes = report.update(command); + final Report report = this.reportRepository.findOne(reportId); + if (report == null) { throw new ReportNotFoundException(reportId); } - if (changes.containsKey("reportParameters")) { - final Set reportParameterUsages = assembleSetOfReportParameterUsages( - report, command); - boolean updated = report.update(reportParameterUsages); - if (!updated) { - changes.remove("reportParameters"); - } - } + final Map changes = report.update(command); - if (!changes.isEmpty()) { - this.reportRepository.saveAndFlush(report); - } + if (changes.containsKey("reportParameters")) { + final Set reportParameterUsages = assembleSetOfReportParameterUsages(report, command); + boolean updated = report.update(reportParameterUsages); + if (!updated) { + changes.remove("reportParameters"); + } + } - return new CommandProcessingResultBuilder() - .withCommandId(command.commandId()) - .withEntityId(report.getId()).with(changes).build(); - } catch (DataIntegrityViolationException dve) { - handleReportDataIntegrityIssues(command, dve); - return CommandProcessingResult.empty(); - } - } + if (!changes.isEmpty()) { + this.reportRepository.saveAndFlush(report); + } - @Transactional - @Override - public CommandProcessingResult deleteReport(final Long reportId) { + return new CommandProcessingResultBuilder() // + .withCommandId(command.commandId()) // + .withEntityId(report.getId()) // + .with(changes) // + .build(); + } catch (DataIntegrityViolationException dve) { + handleReportDataIntegrityIssues(command, dve); + return CommandProcessingResult.empty(); + } + } - final Report report = this.reportRepository.findOne(reportId); - if (report == null) { - throw new ReportNotFoundException(reportId); - } + @Transactional + @Override + public CommandProcessingResult deleteReport(final Long reportId) { - if (report.isCoreReport()) { - throw new PlatformDataIntegrityException( - "error.msg.cant.delete.core.report", - "Core Reports Can't be Deleted", ""); - } + final Report report = this.reportRepository.findOne(reportId); + if (report == null) { throw new ReportNotFoundException(reportId); } - final Permission permission = this.permissionRepository - .findOneByCode("READ" + "_" + report.getReportName()); - if (permission == null) { - throw new PermissionNotFoundException("READ" + "_" - + report.getReportName()); - } + if (report.isCoreReport()) { + // + throw new PlatformDataIntegrityException("error.msg.cant.delete.core.report", "Core Reports Can't be Deleted", ""); + } - this.reportRepository.delete(report); - this.permissionRepository.delete(permission); + final Permission permission = this.permissionRepository.findOneByCode("READ" + "_" + report.getReportName()); + if (permission == null) { throw new PermissionNotFoundException("READ" + "_" + report.getReportName()); } - return new CommandProcessingResultBuilder().withEntityId(reportId) - .build(); - } + this.reportRepository.delete(report); + this.permissionRepository.delete(permission); - /* - * Guaranteed to throw an exception no matter what the data integrity issue - * is. - */ - private void handleReportDataIntegrityIssues(final JsonCommand command, - DataIntegrityViolationException dve) { + return new CommandProcessingResultBuilder() // + .withEntityId(reportId) // + .build(); + } - Throwable realCause = dve.getMostSpecificCause(); - if (realCause.getMessage().contains("unq_report_name")) { - final String name = command - .stringValueOfParameterNamed("reportName"); - throw new PlatformDataIntegrityException( - "error.msg.report.duplicate.name", "A report with name '" - + name + "' already exists", "name", name); - } + /* + * Guaranteed to throw an exception no matter what the data integrity issue + * is. + */ + private void handleReportDataIntegrityIssues(final JsonCommand command, final DataIntegrityViolationException dve) { - logger.error(dve.getMessage(), dve); - throw new PlatformDataIntegrityException( - "error.msg.report.unknown.data.integrity.issue", - "Unknown data integrity issue with resource: " - + realCause.getMessage()); - } + Throwable realCause = dve.getMostSpecificCause(); + if (realCause.getMessage().contains("unq_report_name")) { + final String name = command.stringValueOfParameterNamed("reportName"); + throw new PlatformDataIntegrityException("error.msg.report.duplicate.name", "A report with name '" + name + "' already exists", + "name", name); + } - private Set assembleSetOfReportParameterUsages( - final Report report, final JsonCommand command) { - if (report == null) { - throw new PlatformDataIntegrityException("system.error", - "report parameter is null; "); - } + logger.error(dve.getMessage(), dve); + throw new PlatformDataIntegrityException("error.msg.report.unknown.data.integrity.issue", + "Unknown data integrity issue with resource: " + realCause.getMessage()); + } - if (command.parameterExists("reportParameters")) { - Set reportParameterUsages = new HashSet(); - JsonArray reportParametersArray = command - .arrayOfParameterNamed("reportParameters"); - if (reportParametersArray != null) { - for (int i = 0; i < reportParametersArray.size(); i++) { + private Set assembleSetOfReportParameterUsages(final Report report, final JsonCommand command) { - final JsonObject jsonObject = reportParametersArray.get(i) - .getAsJsonObject(); - Long id = null; - ReportParameter parameter = null; - String reportParameterName = null; + Set reportParameterUsages = null; - if (jsonObject.has("parameterId")) { - final Long parameterId = jsonObject.get("parameterId") - .getAsLong(); - parameter = this.reportParameterRepository - .findOne(parameterId); - if (parameter == null) { - throw new ReportParameterNotFoundException( - parameterId); - } - } else { - throw new PlatformDataIntegrityException( - "error.msg.parameter.id.mandatory.in.report.parameter", - "parameterId column is mandatory in Report Parameter Entry"); - } + if (command.parameterExists("reportParameters")) { + JsonArray reportParametersArray = command.arrayOfParameterNamed("reportParameters"); + if (reportParametersArray != null) { - if (jsonObject.has("reportParameterName")) { - reportParameterName = jsonObject.get( - "reportParameterName").getAsString(); - } + reportParameterUsages = new HashSet(); - if (jsonObject.has("id")) { - final String idStr = jsonObject.get("id").getAsString(); - if (!(idStr.equals(""))) - id = Long.parseLong(idStr); - } + for (int i = 0; i < reportParametersArray.size(); i++) { - ReportParameterUsage reportParameterUsage = null; - if (id == null) { - reportParameterUsage = new ReportParameterUsage(report, - parameter, reportParameterName); - } else { - ReportParameterUsage currentReportParameterUsage = rpuById( - report, id); + final JsonObject jsonObject = reportParametersArray.get(i).getAsJsonObject(); - if (existingEntryChanged(currentReportParameterUsage, - parameter.getId(), reportParameterName)) { - reportParameterUsage = new ReportParameterUsage( - currentReportParameterUsage, report, - parameter, reportParameterName); - } else { - reportParameterUsage = currentReportParameterUsage; - } - } + Long id = null; + ReportParameterUsage reportParameterUsageItem = null; + ReportParameter reportParameter = null; + String reportParameterName = null; - reportParameterUsages.add(reportParameterUsage); - } - return reportParameterUsages; - } else - return null; - } else - return null; - } + if (jsonObject.has("id")) { + final String idStr = jsonObject.get("id").getAsString(); + if (StringUtils.isNotBlank(idStr)) { + id = Long.parseLong(idStr); + } + } - private boolean existingEntryChanged( - final ReportParameterUsage currentReportParameterUsage, - final Long parameterId, final String reportParameterName) { + if (id != null) { + // existing report parameter usage + reportParameterUsageItem = this.reportParameterUsageRepository.findOne(id); + if (reportParameterUsageItem == null) { throw new ReportParameterNotFoundException(id); } - if (!(currentReportParameterUsage.getParameter().getId() - .equals(parameterId))) - return true; + // check parameter + if (jsonObject.has("parameterId")) { + final Long parameterId = jsonObject.get("parameterId").getAsLong(); + reportParameter = this.reportParameterRepository.findOne(parameterId); + if (reportParameter == null || !reportParameterUsageItem.hasParameterIdOf(parameterId)) { + // + throw new ReportParameterNotFoundException(parameterId); + } + } - if ((reportParameterName == null) - && (currentReportParameterUsage.getReportParameterName() == null)) - return false; + if (jsonObject.has("reportParameterName")) { + reportParameterName = jsonObject.get("reportParameterName").getAsString(); + reportParameterUsageItem.updateParameterName(reportParameterName); + } + } else { + // new report parameter usage + if (jsonObject.has("parameterId")) { + final Long parameterId = jsonObject.get("parameterId").getAsLong(); + reportParameter = this.reportParameterRepository.findOne(parameterId); + if (reportParameter == null) { throw new ReportParameterNotFoundException(parameterId); } + } else { + throw new PlatformDataIntegrityException("error.msg.parameter.id.mandatory.in.report.parameter", + "parameterId column is mandatory in Report Parameter Entry"); + } - if (!(currentReportParameterUsage.getReportParameterName() - .equals(reportParameterName))) - return true; + if (jsonObject.has("reportParameterName")) { + reportParameterName = jsonObject.get("reportParameterName").getAsString(); + } - return false; - } + reportParameterUsageItem = new ReportParameterUsage(report, reportParameter, reportParameterName); + } - private ReportParameterUsage rpuById(final Report report, final Long id) { + reportParameterUsages.add(reportParameterUsageItem); + } + } + } - for (ReportParameterUsage rpu : report.getReportParameterUsages()) { - if (rpu.getId().equals(id)) - return rpu; - } - - throw new PlatformDataIntegrityException( - "error.msg.supplied.parameter.id.doesnt.exist", - "Supplied parameterId column doesn't Exist", id); - } + return reportParameterUsages; + } } \ No newline at end of file