From 2fbdfd088421e705b43e7ddaf14d8f071ba1b700 Mon Sep 17 00:00:00 2001 From: "giancarlo.panichi" Date: Thu, 3 Sep 2015 16:32:07 +0000 Subject: [PATCH] 69: Create new Accounting Portlet Task-Url: https://support.d4science.org/issues/69 Added time restrictions git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/admin/accounting-manager@118896 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../client/filters/AccountingPeriodPanel.java | 185 ++++++++++++++---- 1 file changed, 148 insertions(+), 37 deletions(-) diff --git a/src/main/java/org/gcube/portlets/admin/accountingmanager/client/filters/AccountingPeriodPanel.java b/src/main/java/org/gcube/portlets/admin/accountingmanager/client/filters/AccountingPeriodPanel.java index aa0a030..ba68676 100644 --- a/src/main/java/org/gcube/portlets/admin/accountingmanager/client/filters/AccountingPeriodPanel.java +++ b/src/main/java/org/gcube/portlets/admin/accountingmanager/client/filters/AccountingPeriodPanel.java @@ -17,6 +17,7 @@ import com.google.gwt.event.logical.shared.ValueChangeHandler; import com.google.gwt.event.shared.EventBus; import com.google.gwt.i18n.client.DateTimeFormat; import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat; +import com.google.gwt.user.datepicker.client.CalendarUtil; import com.sencha.gxt.cell.core.client.form.ComboBoxCell.TriggerAction; import com.sencha.gxt.core.client.util.Margins; import com.sencha.gxt.data.shared.LabelProvider; @@ -31,7 +32,6 @@ import com.sencha.gxt.widget.core.client.form.DateField; import com.sencha.gxt.widget.core.client.form.FieldLabel; import com.sencha.gxt.widget.core.client.form.FieldSet; import com.sencha.gxt.widget.core.client.form.validator.EmptyValidator; -import com.sencha.gxt.widget.core.client.form.validator.MaxDateValidator; /** * @@ -62,7 +62,7 @@ public class AccountingPeriodPanel extends SimpleContainer { } private void create() { - + // Aggreagation Mode AccountingPeriodModePropertiesCombo props = GWT .create(AccountingPeriodModePropertiesCombo.class); @@ -83,10 +83,9 @@ public class AccountingPeriodPanel extends SimpleContainer { FieldLabel periodModeLabel = new FieldLabel(comboPeriodMode, "Aggregation"); - + // startDate = new DateField(); - startDate.addValidator(new MaxDateValidator(new Date())); startDate.addValidator(new EmptyValidator()); startDate.addParseErrorHandler(new ParseErrorEvent.ParseErrorHandler() { @@ -103,12 +102,13 @@ public class AccountingPeriodPanel extends SimpleContainer { String v = event.getValue() == null ? "Nothing" : DateTimeFormat.getFormat(PredefinedFormat.DATE_SHORT) .format(event.getValue()); - Log.debug("Start Date Selected "+ v); + Log.debug("Start Date Selected " + v); } }); FieldLabel startDateLabel = new FieldLabel(startDate, "Start Date"); + // endDate = new DateField(); endDate.addValidator(new EmptyValidator()); endDate.addParseErrorHandler(new ParseErrorEvent.ParseErrorHandler() { @@ -124,18 +124,17 @@ public class AccountingPeriodPanel extends SimpleContainer { String v = event.getValue() == null ? "Nothing" : DateTimeFormat.getFormat(PredefinedFormat.DATE_SHORT) .format(event.getValue()); - Log.debug("End Date Selected "+ v); + Log.debug("End Date Selected " + v); } }); FieldLabel endDateLabel = new FieldLabel(endDate, "End Date"); - VerticalLayoutContainer vlc = new VerticalLayoutContainer(); vlc.add(periodModeLabel, new VerticalLayoutData(1, -1, new Margins(0))); vlc.add(startDateLabel, new VerticalLayoutData(1, -1, new Margins(0))); vlc.add(endDateLabel, new VerticalLayoutData(1, -1, new Margins(0))); - + FieldSet fieldSet = new FieldSet(); fieldSet.setHeadingHtml("Temporal Constraint"); fieldSet.setCollapsible(false); @@ -167,12 +166,63 @@ public class AccountingPeriodPanel extends SimpleContainer { SelectionEvent event) { Log.debug("ComboPeriodMode selected: " + event.getSelectedItem()); - + updateTimeInterval(event.getSelectedItem()); } }); } + private void updateTimeInterval(AccountingPeriodMode accountingPeriodMode) { + if (accountingPeriodMode == null) { + startDate.disable(); + endDate.disable(); + startDate.setValue(new Date()); + endDate.setValue(new Date()); + } + + switch (accountingPeriodMode) { + case MINUTELY: + case HOURLY: + startDate.disable(); + endDate.disable(); + startDate.setValue(new Date()); + endDate.setValue(new Date()); + break; + case DAILY: + Date lastMonth = new Date(); + CalendarUtil.addMonthsToDate(lastMonth, -1); + startDate.enable(); + endDate.enable(); + startDate.setValue(lastMonth); + endDate.setValue(new Date()); + break; + case MONTHLY: + startDate.enable(); + endDate.enable(); + Date lastYear = new Date(); + CalendarUtil.addMonthsToDate(lastYear, -12); + startDate.setValue(lastYear); + endDate.setValue(new Date()); + break; + case YEARLY: + startDate.enable(); + endDate.enable(); + Date last3Year = new Date(); + CalendarUtil.addMonthsToDate(last3Year, -36); + startDate.setValue(last3Year); + endDate.setValue(new Date()); + break; + default: + startDate.disable(); + endDate.disable(); + startDate.setValue(new Date()); + endDate.setValue(new Date()); + break; + + } + + } + private void doStateChangeCommand(StateChangeEvent event) { if (event.getStateChangeType() == null) { return; @@ -213,48 +263,109 @@ public class AccountingPeriodPanel extends SimpleContainer { public AccountingPeriod getAccountingPeriod() { try { - - if (startDate.validate() && endDate.validate()) { - if (endDate.getCurrentValue().compareTo(new Date()) <= 0) { - Date startD = startDate.getCurrentValue(); - Date endD = endDate.getCurrentValue(); - if (startD.compareTo(endD) <= 0) { - if (comboPeriodMode.validate() - && comboPeriodMode.getCurrentValue() != null) { - AccountingPeriod accountingPeriod = new AccountingPeriod( - startD, endD, comboPeriodMode.getCurrentValue()); - return accountingPeriod; + if (startDate.validate() && endDate.validate()) { + if (startDate.getCurrentValue().compareTo(new Date()) <= 0) { + if (endDate.getCurrentValue().compareTo(new Date()) <= 0) { + + Date startD = startDate.getCurrentValue(); + Date endD = endDate.getCurrentValue(); + + if (startD.compareTo(endD) <= 0) { + if (comboPeriodMode.validate() + && comboPeriodMode.getCurrentValue() != null) { + + switch (comboPeriodMode.getCurrentValue()) { + case HOURLY: + case MINUTELY: + break; + case DAILY: + Date maximumDistantDay = new Date(); + CalendarUtil.addMonthsToDate( + maximumDistantDay, -1); + CalendarUtil.addDaysToDate( + maximumDistantDay, -1); + if (maximumDistantDay.compareTo(startD) >= 0) { + UtilsGXT3 + .alert("Attention", + "Invalid Start Date (Daily: the max interval should the last month)!"); + return null; + } + + if (maximumDistantDay.compareTo(endD) >= 0) { + UtilsGXT3 + .alert("Attention", + "Invalid End Date (Daily: the max interval should the last month)!"); + + return null; + } + break; + case MONTHLY: + Date maximumDistantMonth = new Date(); + CalendarUtil.addMonthsToDate( + maximumDistantMonth, -60); + CalendarUtil.addDaysToDate( + maximumDistantMonth, -1); + if (maximumDistantMonth.compareTo(startD) >= 0) { + UtilsGXT3 + .alert("Attention", + "Invalid Start Date (Monthly: the max interval should in the last 5 years)!"); + return null; + } + + if (maximumDistantMonth.compareTo(endD) >= 0) { + UtilsGXT3 + .alert("Attention", + "Invalid End Date (Monthly: the max interval should in the last 5 years)!"); + return null; + } + break; + case YEARLY: + break; + default: + break; + + } + + AccountingPeriod accountingPeriod = new AccountingPeriod( + startD, endD, + comboPeriodMode.getCurrentValue()); + return accountingPeriod; + } else { + UtilsGXT3.alert("Attention", + "Select a valid aggregation mode!"); + return null; + } + } else { + UtilsGXT3 + .alert("Attention", + "The start date must be less than or equal to the end date!"); + return null; + } + } else { - UtilsGXT3.alert("Attention", - "Select a valid aggregation mode!"); + String endD = DateTimeFormat.getFormat( + PredefinedFormat.DATE_SHORT).format(new Date()); + UtilsGXT3.alert("Attention", "The end date must be " + + endD + " or earlier!"); return null; } } else { - UtilsGXT3 - .alert("Attention", - "The start date must be less than or equal to the end date!"); + String startD = DateTimeFormat.getFormat( + PredefinedFormat.DATE_SHORT).format(new Date()); + UtilsGXT3.alert("Attention", "The start date must be " + + startD + " or earlier!"); return null; } } else { - String endD=DateTimeFormat.getFormat(PredefinedFormat.DATE_SHORT).format(new Date()); - UtilsGXT3 - .alert("Attention", - "The end date must be "+endD+" or earlier!"); return null; } - } else { - return null; - } - - } catch(Throwable e){ + } catch (Throwable e) { Log.error(e.getLocalizedMessage()); e.printStackTrace(); - UtilsGXT3 - .alert("Attention", - e.getLocalizedMessage()); + UtilsGXT3.alert("Attention", e.getLocalizedMessage()); return null; } }