From 4d183f839af636439bc8cc9ab03076255abe23e3 Mon Sep 17 00:00:00 2001 From: Viorel Chihai Date: Mon, 3 Jan 2022 16:32:38 +0200 Subject: [PATCH] TOBACCO-800: Add industry responses variables --- .../edit/dataset/EditTetsimDatasetPage.html | 1 + .../edit/dataset/EditTetsimDatasetPage.java | 44 +++++++-- .../dataset/EditTetsimDatasetPage.properties | 4 +- .../dataset/TetsimIndustryResponsesPanel.html | 23 +++++ .../dataset/TetsimIndustryResponsesPanel.java | 96 +++++++++++++++++++ .../TetsimIndustryResponsesPanel.properties | 6 ++ .../dataset/TetsimPriceAnalysisPanel.java | 93 +++++++----------- 7 files changed, 200 insertions(+), 67 deletions(-) create mode 100644 forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/edit/dataset/TetsimIndustryResponsesPanel.html create mode 100644 forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/edit/dataset/TetsimIndustryResponsesPanel.java create mode 100644 forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/edit/dataset/TetsimIndustryResponsesPanel.properties diff --git a/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/edit/dataset/EditTetsimDatasetPage.html b/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/edit/dataset/EditTetsimDatasetPage.html index 310e030c..5cebebdc 100644 --- a/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/edit/dataset/EditTetsimDatasetPage.html +++ b/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/edit/dataset/EditTetsimDatasetPage.html @@ -13,6 +13,7 @@
+
diff --git a/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/edit/dataset/EditTetsimDatasetPage.java b/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/edit/dataset/EditTetsimDatasetPage.java index 1f759546..96a66adf 100644 --- a/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/edit/dataset/EditTetsimDatasetPage.java +++ b/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/edit/dataset/EditTetsimDatasetPage.java @@ -1,26 +1,23 @@ package org.devgateway.toolkit.forms.wicket.page.edit.dataset; -import org.apache.wicket.Component; -import org.apache.wicket.markup.html.basic.Label; -import org.apache.wicket.markup.repeater.RepeatingView; -import org.apache.wicket.model.Model; -import org.apache.wicket.model.PropertyModel; -import org.apache.wicket.model.StringResourceModel; +import org.apache.wicket.markup.html.form.Form; +import org.apache.wicket.markup.html.form.FormComponent; +import org.apache.wicket.markup.html.form.validation.AbstractFormValidator; import org.apache.wicket.request.mapper.parameter.PageParameters; import org.apache.wicket.spring.injection.annot.SpringBean; -import org.devgateway.toolkit.forms.validators.PositiveBigDecimalValidator; import org.devgateway.toolkit.forms.wicket.components.form.TextFieldBootstrapFormComponent; import org.devgateway.toolkit.forms.wicket.page.edit.AbstractEditPage; import org.devgateway.toolkit.forms.wicket.page.lists.dataset.ListTetsimDatasetPage; -import org.devgateway.toolkit.persistence.dao.categories.TobaccoProduct; import org.devgateway.toolkit.persistence.dao.data.DatasetStatus; import org.devgateway.toolkit.persistence.dao.data.TetsimDataset; import org.devgateway.toolkit.persistence.dao.data.TetsimPriceVariable; -import org.devgateway.toolkit.persistence.service.category.TobaccoProductService; +import org.devgateway.toolkit.persistence.dao.data.TetsimTobaccoProductValue; import org.devgateway.toolkit.persistence.service.data.TetsimDatasetService; import org.wicketstuff.annotation.mount.MountPath; -import java.util.List; +import java.math.BigDecimal; + +import static org.devgateway.toolkit.forms.WebConstants.MAXIMUM_PERCENTAGE; /** * @author vchihai @@ -50,6 +47,9 @@ protected void onInitialize() { editForm.add(getYear()); editForm.add(getBaseLineNumbers()); editForm.add(getPriceAnalysisNumbers()); + editForm.add(getIndustryResponsesNumbers()); + + editForm.add(new TetsimMarketSharePercentageValidator()); } private TextFieldBootstrapFormComponent getYear() { @@ -66,4 +66,28 @@ private TetsimBaselineNumbersPanel getBaseLineNumbers() { private TetsimPriceAnalysisPanel getPriceAnalysisNumbers() { return new TetsimPriceAnalysisPanel("priceAnalysisNumbers", editForm.getModel()); } + + private TetsimIndustryResponsesPanel getIndustryResponsesNumbers() { + return new TetsimIndustryResponsesPanel("industryResponsesNumbers", editForm.getModel()); + } + + private class TetsimMarketSharePercentageValidator extends AbstractFormValidator { + + @Override + public FormComponent[] getDependentFormComponents() { + return new FormComponent[0]; + } + + @Override + public void validate(final Form form) { + TetsimPriceVariable marketShare = editForm.getModelObject().getMarketShare(); + BigDecimal sum = marketShare.getValues().stream() + .map(TetsimTobaccoProductValue::getValue) + .reduce(BigDecimal.ZERO, BigDecimal::add); + + if (sum.intValue() > MAXIMUM_PERCENTAGE) { + editForm.error(getString("error.form.validation.marketShare.percentage")); + } + } + } } diff --git a/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/edit/dataset/EditTetsimDatasetPage.properties b/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/edit/dataset/EditTetsimDatasetPage.properties index 78cbc216..b83fab3f 100644 --- a/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/edit/dataset/EditTetsimDatasetPage.properties +++ b/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/edit/dataset/EditTetsimDatasetPage.properties @@ -1,3 +1,5 @@ page.title.add=Add TETSIM Dataset page.title.edit=Edit TETSIM Dataset -year.label=Year \ No newline at end of file +year.label=Year + +error.form.validation.marketShare.percentage=The total of all the market share values should add up to 100 \ No newline at end of file diff --git a/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/edit/dataset/TetsimIndustryResponsesPanel.html b/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/edit/dataset/TetsimIndustryResponsesPanel.html new file mode 100644 index 00000000..1903c0a1 --- /dev/null +++ b/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/edit/dataset/TetsimIndustryResponsesPanel.html @@ -0,0 +1,23 @@ + + + + +
+

Industry responses to the change in taxation:

+
+ +
+ + + + + + + + +
+
+
+
+ + diff --git a/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/edit/dataset/TetsimIndustryResponsesPanel.java b/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/edit/dataset/TetsimIndustryResponsesPanel.java new file mode 100644 index 00000000..35dcc273 --- /dev/null +++ b/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/edit/dataset/TetsimIndustryResponsesPanel.java @@ -0,0 +1,96 @@ +package org.devgateway.toolkit.forms.wicket.page.edit.dataset; + +import org.apache.wicket.Component; +import org.apache.wicket.markup.html.basic.Label; +import org.apache.wicket.markup.html.panel.Panel; +import org.apache.wicket.markup.repeater.RepeatingView; +import org.apache.wicket.model.IModel; +import org.apache.wicket.model.Model; +import org.apache.wicket.model.PropertyModel; +import org.apache.wicket.model.StringResourceModel; +import org.apache.wicket.spring.injection.annot.SpringBean; +import org.devgateway.toolkit.persistence.dao.categories.TobaccoProduct; +import org.devgateway.toolkit.persistence.dao.data.TetsimDataset; +import org.devgateway.toolkit.persistence.service.category.TobaccoProductService; + +import java.util.List; + +/** + * @author Viorel Chihai + */ +public class TetsimIndustryResponsesPanel extends Panel { + + protected final IModel tetsimDatasetIModel; + + @SpringBean + protected TobaccoProductService tobaccoProductService; + + public TetsimIndustryResponsesPanel(final String id, final IModel tetsimDatasetIModel) { + super(id); + this.tetsimDatasetIModel = tetsimDatasetIModel; + } + + @Override + protected void onInitialize() { + super.onInitialize(); + add(getIndustryResponsesHeaders()); + add(getIndustryResponsesVariables()); + } + + private Component getIndustryResponsesHeaders() { + RepeatingView analysisHeaders = new RepeatingView("industryResponsesHeaders"); + analysisHeaders.add(new Label(analysisHeaders.newChildId(), + Model.of("Industry responses to the change in taxation"))); + analysisHeaders.add(new Label(analysisHeaders.newChildId(), Model.of("Unit"))); + + List tobaccoProducts = tobaccoProductService.findAllSorted(); + for (TobaccoProduct tobaccoProduct : tobaccoProducts) { + analysisHeaders.add(new Label(analysisHeaders.newChildId(), + new PropertyModel<>(tobaccoProduct, "label"))); + } + + return analysisHeaders; + } + + private Component getIndustryResponsesVariables() { + RepeatingView variables = new RepeatingView("industryResponsesVariables"); + + variables.add(getOverShifting(variables.newChildId())); + variables.add(getUnderShifting(variables.newChildId())); + + return variables; + } + + /** + * Get overshifting variable panel with tobacco product inputs. + * Mandatory. Numerical fields with decimals. + * + * @param id + * @return TetsimTobaccoProductsVariable + */ + private TetsimTobaccoProductsVariable getOverShifting(final String id) { + return new TetsimTobaccoProductsVariable(id, + new StringResourceModel("overShifting.label"), + new StringResourceModel("overShifting.unit"), + new PropertyModel<>(tetsimDatasetIModel, "overShifting"), true) { + }; + } + + /** + * Get undershifting variable panel with tobacco product inputs. + * Mandatory. Numerical fields with decimals. + * + * @param id + * @return TetsimTobaccoProductsVariable + */ + private TetsimTobaccoProductsVariable getUnderShifting(final String id) { + return new TetsimTobaccoProductsVariable(id, + new StringResourceModel("underShifting.label"), + new StringResourceModel("underShifting.unit"), + new PropertyModel<>(tetsimDatasetIModel, "underShifting"), true) { + }; + } + +} + + diff --git a/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/edit/dataset/TetsimIndustryResponsesPanel.properties b/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/edit/dataset/TetsimIndustryResponsesPanel.properties new file mode 100644 index 00000000..c4f11b14 --- /dev/null +++ b/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/edit/dataset/TetsimIndustryResponsesPanel.properties @@ -0,0 +1,6 @@ +overShifting.label=Overshifting (% change above excise tax increase) +overShifting.unit=Percentage change above excise tax increase + +underShifting.label=Change in illicit NOT (proportion of change in discount price): sensitivity factor +underShifting.unit=Undershifting (% change below excise tax increase) + diff --git a/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/edit/dataset/TetsimPriceAnalysisPanel.java b/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/edit/dataset/TetsimPriceAnalysisPanel.java index 2f08b1e9..cc450ff8 100644 --- a/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/edit/dataset/TetsimPriceAnalysisPanel.java +++ b/forms/src/main/java/org/devgateway/toolkit/forms/wicket/page/edit/dataset/TetsimPriceAnalysisPanel.java @@ -27,6 +27,7 @@ public class TetsimPriceAnalysisPanel extends Panel { protected final IModel tetsimDatasetIModel; + @SpringBean protected TobaccoProductService tobaccoProductService; @@ -56,7 +57,7 @@ private Component getPriceAnalysisHeaders() { return analysisHeaders; } - private Component getPriceAnalysisVariables() { + private RepeatingView getPriceAnalysisVariables() { RepeatingView variables = new RepeatingView("analysisVariables"); variables.add(getRetailPriceVariable(variables.newChildId())); @@ -80,19 +81,17 @@ private Component getPriceAnalysisVariables() { * @return TetsimTobaccoProductsVariable */ private TetsimTobaccoProductsVariable getRetailPriceVariable(final String id) { - TetsimTobaccoProductsVariable variable = new TetsimTobaccoProductsVariable(id, + return new TetsimTobaccoProductsVariable(id, new StringResourceModel("retailPrice.label"), new StringResourceModel("retailPrice.unit"), new PropertyModel<>(tetsimDatasetIModel, "retailPrice")) { @Override - protected void addBehavioursToTobaccoProductVariable(final TextFieldBootstrapFormComponent variable) { - super.addBehavioursToTobaccoProductVariable(variable); - variable.getField().add(new PositiveBigDecimalValidator()); + protected void addBehavioursToTobaccoProductVariable(final TextFieldBootstrapFormComponent variable1) { + super.addBehavioursToTobaccoProductVariable(variable1); + variable1.getField().add(new PositiveBigDecimalValidator()); } }; - - return variable; } /** @@ -104,22 +103,18 @@ protected void addBehavioursToTobaccoProductVariable(final TextFieldBootstrapFor * @return TetsimTobaccoProductsVariable */ private TetsimTobaccoProductsVariable getMarketShareVariable(final String id) { - TetsimTobaccoProductsVariable variable = new TetsimTobaccoProductsVariable(id, + return new TetsimTobaccoProductsVariable(id, new StringResourceModel("marketShare.label"), new StringResourceModel("marketShare.unit"), new PropertyModel<>(tetsimDatasetIModel, "marketShare")) { @Override - protected void addBehavioursToTobaccoProductVariable(final TextFieldBootstrapFormComponent variable) { - super.addBehavioursToTobaccoProductVariable(variable); - variable.getField().add(new PositiveBigDecimalValidator(true)); - variable.getField().add(RangeValidator.maximum(BigDecimal.valueOf(MAXIMUM_PERCENTAGE))); + protected void addBehavioursToTobaccoProductVariable(final TextFieldBootstrapFormComponent variable1) { + super.addBehavioursToTobaccoProductVariable(variable1); + variable1.getField().add(new PositiveBigDecimalValidator(true)); + variable1.getField().add(RangeValidator.maximum(BigDecimal.valueOf(MAXIMUM_PERCENTAGE))); } }; - - //TODO add sum of all percentages validator based on inputs values - - return variable; } /** @@ -130,19 +125,17 @@ protected void addBehavioursToTobaccoProductVariable(final TextFieldBootstrapFor * @return TetsimTobaccoProductsVariable */ private TetsimTobaccoProductsVariable getCifVariable(final String id) { - TetsimTobaccoProductsVariable variable = new TetsimTobaccoProductsVariable(id, + return new TetsimTobaccoProductsVariable(id, new StringResourceModel("cif.label"), new StringResourceModel("cif.unit"), new PropertyModel<>(tetsimDatasetIModel, "cif")) { @Override - protected void addBehavioursToTobaccoProductVariable(final TextFieldBootstrapFormComponent variable) { - super.addBehavioursToTobaccoProductVariable(variable); - variable.getField().add(new PositiveBigDecimalValidator(true)); + protected void addBehavioursToTobaccoProductVariable(final TextFieldBootstrapFormComponent variable1) { + super.addBehavioursToTobaccoProductVariable(variable1); + variable1.getField().add(new PositiveBigDecimalValidator(true)); } }; - - return variable; } /** @@ -153,20 +146,18 @@ protected void addBehavioursToTobaccoProductVariable(final TextFieldBootstrapFor * @return TetsimTobaccoProductsVariable */ private TetsimTobaccoProductsVariable getTobaccoLevy(final String id) { - TetsimTobaccoProductsVariable variable = new TetsimTobaccoProductsVariable(id, + return new TetsimTobaccoProductsVariable(id, new StringResourceModel("tobaccoLevy.label"), new StringResourceModel("tobaccoLevy.unit"), new PropertyModel<>(tetsimDatasetIModel, "tobaccoLevy")) { @Override - protected void addBehavioursToTobaccoProductVariable(final TextFieldBootstrapFormComponent variable) { - super.addBehavioursToTobaccoProductVariable(variable); - variable.getField().add(new PositiveBigDecimalValidator(true)); - variable.getField().add(RangeValidator.maximum(BigDecimal.valueOf(MAXIMUM_PERCENTAGE))); + protected void addBehavioursToTobaccoProductVariable(final TextFieldBootstrapFormComponent variable1) { + super.addBehavioursToTobaccoProductVariable(variable1); + variable1.getField().add(new PositiveBigDecimalValidator(true)); + variable1.getField().add(RangeValidator.maximum(BigDecimal.valueOf(MAXIMUM_PERCENTAGE))); } }; - - return variable; } /** @@ -177,19 +168,17 @@ protected void addBehavioursToTobaccoProductVariable(final TextFieldBootstrapFor * @return TetsimTobaccoProductsVariable */ private TetsimTobaccoProductsVariable getExciseTax(final String id) { - TetsimTobaccoProductsVariable variable = new TetsimTobaccoProductsVariable(id, + return new TetsimTobaccoProductsVariable(id, new StringResourceModel("exciseTax.label"), new StringResourceModel("exciseTax.unit"), new PropertyModel<>(tetsimDatasetIModel, "exciseTax")) { @Override - protected void addBehavioursToTobaccoProductVariable(final TextFieldBootstrapFormComponent variable) { - super.addBehavioursToTobaccoProductVariable(variable); - variable.getField().add(new PositiveBigDecimalValidator(true)); + protected void addBehavioursToTobaccoProductVariable(final TextFieldBootstrapFormComponent variable1) { + super.addBehavioursToTobaccoProductVariable(variable1); + variable1.getField().add(new PositiveBigDecimalValidator(true)); } }; - - return variable; } /** @@ -200,19 +189,17 @@ protected void addBehavioursToTobaccoProductVariable(final TextFieldBootstrapFor * @return TetsimTobaccoProductsVariable */ private TetsimTobaccoProductsVariable getCustomsDuty(final String id) { - TetsimTobaccoProductsVariable variable = new TetsimTobaccoProductsVariable(id, + return new TetsimTobaccoProductsVariable(id, new StringResourceModel("customsDuty.label"), new StringResourceModel("customsDuty.unit"), new PropertyModel<>(tetsimDatasetIModel, "customsDuty"), false) { @Override - protected void addBehavioursToTobaccoProductVariable(final TextFieldBootstrapFormComponent variable) { - super.addBehavioursToTobaccoProductVariable(variable); - variable.getField().add(new PositiveBigDecimalValidator(true)); + protected void addBehavioursToTobaccoProductVariable(final TextFieldBootstrapFormComponent variable1) { + super.addBehavioursToTobaccoProductVariable(variable1); + variable1.getField().add(new PositiveBigDecimalValidator(true)); } }; - - return variable; } /** @@ -223,19 +210,17 @@ protected void addBehavioursToTobaccoProductVariable(final TextFieldBootstrapFor * @return TetsimTobaccoProductsVariable */ private TetsimTobaccoProductsVariable getElasticityOfDemandPrice(final String id) { - TetsimTobaccoProductsVariable variable = new TetsimTobaccoProductsVariable(id, + return new TetsimTobaccoProductsVariable(id, new StringResourceModel("elasticityOfDemandPrice.label"), new StringResourceModel("elasticityOfDemandPrice.unit"), new PropertyModel<>(tetsimDatasetIModel, "elasticityOfDemandPrice")) { @Override - protected void addBehavioursToTobaccoProductVariable(final TextFieldBootstrapFormComponent variable) { - super.addBehavioursToTobaccoProductVariable(variable); - variable.getField().add(RangeValidator.range(BigDecimal.valueOf(-1), BigDecimal.ONE)); + protected void addBehavioursToTobaccoProductVariable(final TextFieldBootstrapFormComponent variable1) { + super.addBehavioursToTobaccoProductVariable(variable1); + variable1.getField().add(RangeValidator.range(BigDecimal.valueOf(-1), BigDecimal.ONE)); } }; - - return variable; } /** @@ -246,19 +231,17 @@ protected void addBehavioursToTobaccoProductVariable(final TextFieldBootstrapFor * @return TetsimTobaccoProductsVariable */ private TetsimTobaccoProductsVariable getElasticityOfDemandCrossPrice(final String id) { - TetsimTobaccoProductsVariable variable = new TetsimTobaccoProductsVariable(id, + return new TetsimTobaccoProductsVariable(id, new StringResourceModel("elasticityOfDemandCrossPrice.label"), new StringResourceModel("elasticityOfDemandCrossPrice.unit"), new PropertyModel<>(tetsimDatasetIModel, "elasticityOfDemandPrice")) { @Override - protected void addBehavioursToTobaccoProductVariable(final TextFieldBootstrapFormComponent variable) { - super.addBehavioursToTobaccoProductVariable(variable); - variable.getField().add(RangeValidator.range(BigDecimal.valueOf(-1), BigDecimal.ONE)); + protected void addBehavioursToTobaccoProductVariable(final TextFieldBootstrapFormComponent variable1) { + super.addBehavioursToTobaccoProductVariable(variable1); + variable1.getField().add(RangeValidator.range(BigDecimal.valueOf(-1), BigDecimal.ONE)); } }; - - return variable; } /** @@ -270,13 +253,11 @@ protected void addBehavioursToTobaccoProductVariable(final TextFieldBootstrapFor * @return TetsimTobaccoProductsVariable */ private TetsimTobaccoProductsVariable getChangeInIllicitNot(final String id) { - TetsimTobaccoProductsVariable variable = new TetsimTobaccoProductsVariable(id, + return new TetsimTobaccoProductsVariable(id, new StringResourceModel("changeInIllicitNot.label"), new StringResourceModel("changeInIllicitNot.unit"), new PropertyModel<>(tetsimDatasetIModel, "changeInIllicitNot"), false, true) { }; - - return variable; } }