Skip to content

Commit

Permalink
process params: DateRange widget type support
Browse files Browse the repository at this point in the history
  • Loading branch information
teosarca committed Sep 15, 2017
1 parent 86afc9c commit e5370a5
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import de.metas.ui.web.process.descriptor.ProcessDescriptor.ProcessDescriptorType;
import de.metas.ui.web.process.descriptor.ProcessLayout;
import de.metas.ui.web.process.descriptor.WebuiRelatedProcessDescriptor;
import de.metas.ui.web.window.datatypes.DateRangeValue;
import de.metas.ui.web.window.datatypes.DocumentType;
import de.metas.ui.web.window.datatypes.LookupValue;
import de.metas.ui.web.window.descriptor.DocumentEntityDataBindingDescriptor;
Expand Down Expand Up @@ -194,7 +195,6 @@ private DocumentFieldDescriptor.Builder createProcessParaDescriptor(final WebuiP
{
final IModelTranslationMap adProcessParaTrlsMap = InterfaceWrapperHelper.getModelTranslationMap(adProcessParam);
final String parameterName = adProcessParam.getColumnName();
final boolean isParameterTo = false; // TODO: implement range parameters support

//
// Ask the provider if it has some custom lookup descriptor
Expand All @@ -212,7 +212,7 @@ private DocumentFieldDescriptor.Builder createProcessParaDescriptor(final WebuiP
//
final LookupDescriptor lookupDescriptor = lookupDescriptorProvider.provideForScope(LookupDescriptorProvider.LookupScope.DocumentField);

final DocumentFieldWidgetType widgetType = DescriptorsFactoryHelper.extractWidgetType(parameterName, adProcessParam.getAD_Reference_ID(), lookupDescriptor);
final DocumentFieldWidgetType widgetType = extractWidgetType(parameterName, adProcessParam.getAD_Reference_ID(), lookupDescriptor, adProcessParam.isRange());
final Class<?> valueClass = DescriptorsFactoryHelper.getValueClass(widgetType, lookupDescriptor);
final boolean allowShowPassword = widgetType == DocumentFieldWidgetType.Password ? true : false; // process parameters shall always allow displaying the password

Expand Down Expand Up @@ -249,31 +249,28 @@ private DocumentFieldDescriptor.Builder createProcessParaDescriptor(final WebuiP
;

// Add a callout to forward process parameter value (UI) to current process instance
if (webuiProcesClassInfo.isForwardValueToJavaProcessInstance(parameterName, isParameterTo))
if (webuiProcesClassInfo.isForwardValueToJavaProcessInstance(parameterName))
{
paramDescriptor.addCallout(calloutField -> forwardValueToCurrentProcessInstance(calloutField, isParameterTo));
paramDescriptor.addCallout(ProcessParametersCallout::forwardValueToCurrentProcessInstance);
}

return paramDescriptor;
}

private static final void forwardValueToCurrentProcessInstance(final ICalloutField calloutField, final boolean isParameterTo)
private static DocumentFieldWidgetType extractWidgetType(final String parameterName, final int adReferenceId, final LookupDescriptor lookupDescriptor, final boolean isRange)
{
final JavaProcess processInstance = JavaProcess.currentInstance();
final DocumentFieldWidgetType widgetType = DescriptorsFactoryHelper.extractWidgetType(parameterName, adReferenceId, lookupDescriptor);

final String parameterName = calloutField.getColumnName();

//
// Build up our value source
Object parameterValue = calloutField.getValue();
if (parameterValue instanceof LookupValue)
// Date range:
if (isRange && widgetType == DocumentFieldWidgetType.Date)
{
parameterValue = ((LookupValue)parameterValue).getId();
return DocumentFieldWidgetType.DateRange;
}
// Others
else
{
return widgetType;
}
final IRangeAwareParams source = ProcessParams.ofValue(parameterName, parameterValue);

// Ask the instance to load the parameter
processInstance.loadParameterValueNoFail(parameterName, isParameterTo, source);
}

private static final ProcessDescriptorType extractType(final I_AD_Process adProcess)
Expand Down Expand Up @@ -316,6 +313,38 @@ private static final String extractClassnameOrNull(final I_AD_Process adProcess)
return null;
}

private static final class ProcessParametersCallout
{
private static final void forwardValueToCurrentProcessInstance(final ICalloutField calloutField)
{
final JavaProcess processInstance = JavaProcess.currentInstance();

final String parameterName = calloutField.getColumnName();

//
// Build up our value source
final IRangeAwareParams source;
final Object fieldValue = calloutField.getValue();
if (fieldValue instanceof LookupValue)
{
final Object idObj = ((LookupValue)fieldValue).getId();
source = ProcessParams.ofValueObject(parameterName, idObj);
}
else if (fieldValue instanceof DateRangeValue)
{
final DateRangeValue dateRange = (DateRangeValue)fieldValue;
source = ProcessParams.of(parameterName, dateRange.getFrom(), dateRange.getTo());
}
else
{
source = ProcessParams.ofValueObject(parameterName, fieldValue);
}

// Ask the instance to load the parameter
processInstance.loadParameterValueNoFail(parameterName, source);
}
}

private static final class ProcessParametersDataBindingDescriptorBuilder implements DocumentEntityDataBindingDescriptorBuilder
{
public static final transient ProcessParametersDataBindingDescriptorBuilder instance = new ProcessParametersDataBindingDescriptorBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import de.metas.process.ProcessInfoParameter;
import de.metas.ui.web.exceptions.EntityNotFoundException;
import de.metas.ui.web.window.WindowConstants;
import de.metas.ui.web.window.datatypes.DateRangeValue;
import de.metas.ui.web.window.datatypes.DocumentId;
import de.metas.ui.web.window.datatypes.LookupValue;
import de.metas.ui.web.window.descriptor.DocumentEntityDescriptor;
Expand Down Expand Up @@ -184,21 +185,33 @@ private static ProcessInfoParameter createProcessInfoParameter(final IDocumentFi

final Object parameter;
final String info;
final Object parameterTo;
final String infoTo;
if (fieldValue instanceof LookupValue)
{
final LookupValue lookupValue = (LookupValue)fieldValue;
parameter = lookupValue.getId();
info = lookupValue.getDisplayName();
parameterTo = null;
infoTo = null;
}
else if (fieldValue instanceof DateRangeValue)
{
final DateRangeValue dateRange = (DateRangeValue)fieldValue;
parameter = dateRange.getFrom();
info = parameter == null ? null : parameter.toString();
parameterTo = dateRange.getTo();
infoTo = parameterTo == null ? null : parameterTo.toString();
}
else
{
parameter = fieldValue;
info = fieldValue == null ? null : fieldValue.toString();
parameterTo = null;
infoTo = null;
}

final Object parameter_To = null;
final String info_To = null;
return new ProcessInfoParameter(parameterName, parameter, parameter_To, info, info_To);
return new ProcessInfoParameter(parameterName, parameter, parameterTo, info, infoTo);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ public LookupDescriptorProvider getLookupDescriptorProviderOrNull(final String p
return paramLookupValuesProviders.get(parameterName);
}

public boolean isForwardValueToJavaProcessInstance(final String parameterName, final boolean isParameterTo)
public boolean isForwardValueToJavaProcessInstance(final String parameterName)
{
return !processClassInfo.getParameterInfos(parameterName, isParameterTo).isEmpty();
return !processClassInfo.getParameterInfos(parameterName).isEmpty();
}

//
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/de/metas/ui/web/window/datatypes/DateRangeValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package de.metas.ui.web.window.datatypes;

import java.util.Date;

import lombok.Value;

/*
* #%L
* metasfresh-webui-api
* %%
* Copyright (C) 2017 metas GmbH
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/

@Value(staticConstructor = "of")
public final class DateRangeValue
{
private final Date from;
private final Date to;
}
6 changes: 6 additions & 0 deletions src/main/java/de/metas/ui/web/window/datatypes/Values.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import de.metas.ui.web.window.datatypes.json.JSONDate;
import de.metas.ui.web.window.datatypes.json.JSONLookupValue;
import de.metas.ui.web.window.datatypes.json.JSONRange;

/*
* #%L
Expand Down Expand Up @@ -64,6 +65,11 @@ else if (value instanceof java.util.Date)
final java.util.Date valueDate = (java.util.Date)value;
return JSONDate.toJson(valueDate);
}
else if(value instanceof DateRangeValue)
{
final DateRangeValue dateRange = (DateRangeValue)value;
return JSONRange.of(dateRange);
}
else if (value instanceof LookupValue)
{
final LookupValue lookupValue = (LookupValue)value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public enum JSONLayoutWidgetType
{
Text, LongText, Link, Password

, Date, Time, DateTime
, Date, Time, DateTime, DateRange

, Integer, Number, Amount, Quantity, CostPrice

Expand All @@ -50,7 +50,7 @@ public enum JSONLayoutWidgetType
//
;

public static JSONLayoutWidgetType fromNullable(DocumentFieldWidgetType widgetType)
public static JSONLayoutWidgetType fromNullable(final DocumentFieldWidgetType widgetType)
{
if (widgetType == null)
{
Expand All @@ -72,6 +72,7 @@ public static JSONLayoutWidgetType fromNullable(DocumentFieldWidgetType widgetTy
.put(DocumentFieldWidgetType.Date, JSONLayoutWidgetType.Date)
.put(DocumentFieldWidgetType.Time, JSONLayoutWidgetType.Time)
.put(DocumentFieldWidgetType.DateTime, JSONLayoutWidgetType.DateTime)
.put(DocumentFieldWidgetType.DateRange, JSONLayoutWidgetType.DateRange)
.put(DocumentFieldWidgetType.Integer, JSONLayoutWidgetType.Integer)
.put(DocumentFieldWidgetType.Number, JSONLayoutWidgetType.Number)
.put(DocumentFieldWidgetType.Amount, JSONLayoutWidgetType.Amount)
Expand Down
77 changes: 77 additions & 0 deletions src/main/java/de/metas/ui/web/window/datatypes/json/JSONRange.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package de.metas.ui.web.window.datatypes.json;

import java.util.Date;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import de.metas.printing.esb.base.util.Check;
import de.metas.ui.web.window.datatypes.DateRangeValue;
import de.metas.ui.web.window.descriptor.DocumentFieldWidgetType;
import lombok.Builder;
import lombok.Value;

/*
* #%L
* metasfresh-webui-api
* %%
* Copyright (C) 2017 metas GmbH
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/

@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, isGetterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
@Value
public class JSONRange
{
public static final JSONRange of(final DateRangeValue range)
{
final Date from = range.getFrom();
final String jsonFrom = from != null ? JSONDate.toJson(from) : null;

final Date to = range.getTo();
final String jsonTo = to != null ? JSONDate.toJson(to) : null;

return new JSONRange(jsonFrom, jsonTo);
}

public static DateRangeValue dateRangeFromJSONMap(final Map<String, String> map)
{
final String jsonFrom = map.get("value");
final Date from = Check.isEmpty(jsonFrom) ? null : JSONDate.fromJson(jsonFrom, DocumentFieldWidgetType.Date);

final String jsonTo = map.get("valueTo");
final Date to = Check.isEmpty(jsonTo) ? null : JSONDate.fromJson(jsonTo, DocumentFieldWidgetType.Date);

return DateRangeValue.of(from, to);
}

@JsonProperty("value")
private final String value;
@JsonProperty("valueTo")
private final String valueTo;

@JsonCreator
@Builder
private JSONRange(@JsonProperty("value") final String value, @JsonProperty("valueTo") final String valueTo)
{
this.value = value;
this.valueTo = valueTo;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@
import de.metas.i18n.ImmutableTranslatableString;
import de.metas.logging.LogManager;
import de.metas.ui.web.window.WindowConstants;
import de.metas.ui.web.window.datatypes.DateRangeValue;
import de.metas.ui.web.window.datatypes.LookupValue;
import de.metas.ui.web.window.datatypes.LookupValue.IntegerLookupValue;
import de.metas.ui.web.window.datatypes.LookupValue.StringLookupValue;
import de.metas.ui.web.window.datatypes.json.JSONDate;
import de.metas.ui.web.window.datatypes.json.JSONLookupValue;
import de.metas.ui.web.window.datatypes.json.JSONRange;
import de.metas.ui.web.window.descriptor.DocumentFieldDependencyMap.DependencyType;
import de.metas.ui.web.window.descriptor.DocumentLayoutElementFieldDescriptor.LookupSource;
import de.metas.ui.web.window.descriptor.LookupDescriptorProvider.LookupScope;
Expand Down Expand Up @@ -636,6 +638,18 @@ else if (IntegerLookupValue.class == fromType)
return valueConv;
}
}
else if(DateRangeValue.class == targetType)
{
if (Map.class.isAssignableFrom(fromType))
{
@SuppressWarnings("unchecked")
final Map<String, String> map = (Map<String, String>)value;
final DateRangeValue dateRange = JSONRange.dateRangeFromJSONMap(map);
@SuppressWarnings("unchecked")
final T valueConv = (T)dateRange;
return valueConv;
}
}
}
catch (final Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;

import de.metas.ui.web.window.datatypes.DateRangeValue;
import de.metas.ui.web.window.datatypes.LookupValue.IntegerLookupValue;

/*
Expand Down Expand Up @@ -46,6 +47,7 @@ public enum DocumentFieldWidgetType
, Date(LayoutAlign.Right, java.util.Date.class, DisplayType.Date) //
, Time(LayoutAlign.Right, java.util.Date.class, DisplayType.Time) //
, DateTime(LayoutAlign.Right, java.util.Date.class, DisplayType.DateTime) //
, DateRange(LayoutAlign.Left, DateRangeValue.class, -1) //

// Numbers, Amounts, Prices
, Integer(LayoutAlign.Right, Integer.class, DisplayType.Integer) //
Expand Down

0 comments on commit e5370a5

Please sign in to comment.