Skip to content

Commit

Permalink
Change the JSONLookupValue format
Browse files Browse the repository at this point in the history
#666
(cherry picked from commit 715dd07)
  • Loading branch information
teosarca authored and metas-ts committed Nov 10, 2017
1 parent 6e46227 commit a959ad6
Show file tree
Hide file tree
Showing 10 changed files with 245 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ else if (widgetType.isLookup())
}
else if (value instanceof JSONLookupValue)
{
return ImmutableTranslatableString.constant(((JSONLookupValue)value).getName().trim());
return ImmutableTranslatableString.constant(((JSONLookupValue)value).getCaption().trim());
}
}
else if (widgetType.isBoolean())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ public int getM_Product_ID()
public String getM_Product_DisplayName()
{
final JSONLookupValue productLV = getProduct();
return productLV == null ? null : productLV.getName();
return productLV == null ? null : productLV.getCaption();
}

public I_M_Product getM_Product()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ private void changeLetter(final WebuiLetter letter, final WebuiLetter.WebuiLette
else if (PATCH_FIELD_TemplateId.equals(fieldName))
{
@SuppressWarnings("unchecked")
final LookupValue templateId = JSONLookupValue.integerLookupValueFromJsonMap((Map<String, String>)event.getValue());
final LookupValue templateId = JSONLookupValue.integerLookupValueFromJsonMap((Map<String, Object>)event.getValue());
applyTemplate(letter, newLetterBuilder, templateId);
}
else
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/de/metas/ui/web/mail/MailRestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ private void changeEmail(final WebuiEmail email, final WebuiEmailBuilder newEmai

@SuppressWarnings("unchecked")
final LookupValuesList to = jsonTo.stream()
.map(mapObj -> (Map<String, String>)mapObj)
.map(mapObj -> (Map<String, Object>)mapObj)
.map(map -> JSONLookupValue.integerLookupValueFromJsonMap(map))
.collect(LookupValuesList.collect());

Expand All @@ -335,7 +335,7 @@ else if (PATCH_FIELD_Attachments.equals(fieldName))

@SuppressWarnings("unchecked")
final LookupValuesList attachments = jsonAttachments.stream()
.map(mapObj -> (Map<String, String>)mapObj)
.map(mapObj -> (Map<String, Object>)mapObj)
.map(map -> JSONLookupValue.stringLookupValueFromJsonMap(map))
.collect(LookupValuesList.collect());

Expand All @@ -344,7 +344,7 @@ else if (PATCH_FIELD_Attachments.equals(fieldName))
else if (PATCH_FIELD_TemplateId.equals(fieldName))
{
@SuppressWarnings("unchecked")
final LookupValue templateId = JSONLookupValue.integerLookupValueFromJsonMap((Map<String, String>)event.getValue());
final LookupValue templateId = JSONLookupValue.integerLookupValueFromJsonMap((Map<String, Object>)event.getValue());
applyTemplate(email, newEmailBuilder, templateId);
}
else
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/de/metas/ui/web/view/ViewExcelExporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public CellValue getValueAt(final int rowIndex, final int columnIndex)
}
else if (value instanceof JSONLookupValue)
{
final String valueStr = ((JSONLookupValue)value).getName();
final String valueStr = ((JSONLookupValue)value).getCaption();
return CellValues.toCellValue(valueStr, widgetType.getDisplayType());
}
else
Expand Down
148 changes: 129 additions & 19 deletions src/main/java/de/metas/ui/web/window/datatypes/LookupValue.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
package de.metas.ui.web.window.datatypes;

import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;

import javax.annotation.Nullable;

import org.adempiere.exceptions.AdempiereException;
import org.compiere.util.KeyNamePair;
import org.compiere.util.NamePair;
import org.compiere.util.ValueNamePair;

import com.google.common.base.MoreObjects;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;

import de.metas.i18n.ITranslatableString;
import de.metas.i18n.ImmutableTranslatableString;
import de.metas.process.IProcessDefaultParametersProvider;
import de.metas.process.JavaProcess;
import de.metas.ui.web.process.descriptor.ProcessParamLookupValuesProvider;
import lombok.Builder;
import lombok.NonNull;
import lombok.Singular;

/*
* #%L
Expand Down Expand Up @@ -85,11 +97,13 @@ public static final LookupValue fromObject(final Object id, final String display
}
if (id instanceof Integer)
{
return new IntegerLookupValue((int)id, ImmutableTranslatableString.constant(displayName));
final Map<String, Object> attributes = null;
return new IntegerLookupValue((int)id, ImmutableTranslatableString.constant(displayName), attributes);
}
else
{
return new StringLookupValue(id.toString(), ImmutableTranslatableString.constant(displayName));
final Map<String, Object> attributes = null;
return new StringLookupValue(id.toString(), ImmutableTranslatableString.constant(displayName), attributes);
}
}

Expand Down Expand Up @@ -142,24 +156,23 @@ else if (namePair instanceof KeyNamePair)

protected final Object id;
protected final ITranslatableString displayName;
private final ImmutableMap<String, Object> additionalAttributes;

private LookupValue(final Object id, final ITranslatableString displayName)
private LookupValue(@NonNull final Object id, @Nullable final ITranslatableString displayName, final Map<String, Object> additionalAttributes)
{
super();
if (id == null)
{
throw new NullPointerException("id");
}
this.id = id;
this.displayName = displayName == null ? ImmutableTranslatableString.empty() : displayName;
this.additionalAttributes = additionalAttributes != null && !additionalAttributes.isEmpty() ? ImmutableMap.copyOf(additionalAttributes) : null;
}

@Override
public String toString()
{
return MoreObjects.toStringHelper(this)
.omitNullValues()
.add("id", id)
.add("displayName", displayName)
.add("additionalAttributes", additionalAttributes)
.toString();
}

Expand All @@ -180,12 +193,15 @@ public boolean equals(final Object obj)
}

final LookupValue other = (LookupValue)obj;

// NOTE: only the ID is considered on hashCode and equals
return DataTypes.equals(id, other.id);
}

@Override
public int hashCode()
{
// NOTE: only the ID is considered on hashCode and equals
return Objects.hashCode(id);
}

Expand Down Expand Up @@ -221,28 +237,114 @@ public final <T> T transform(final Function<LookupValue, T> transformation)
return transformation.apply(this);
}

public Map<String, Object> getAttributes()
{
return additionalAttributes != null ? additionalAttributes : ImmutableMap.of();
}

public <T> T getAttribute(final String name)
{
if (additionalAttributes == null)
{
return null;
}
@SuppressWarnings("unchecked")
final T value = (T)additionalAttributes.get(name);
return value;
}

public int getAttributeAsInt(final String name, final int defaultValue)
{
final Object valueObj = getAttribute(name);
if (valueObj == null)
{
return defaultValue;
}
else if (valueObj instanceof Number)
{
return ((Number)valueObj).intValue();
}
else
{
final String valueStr = valueObj.toString().trim();
if (valueStr.isEmpty())
{
return defaultValue;
}

return Integer.parseInt(valueStr);
}
}

public Set<Integer> getAttributeAsIntSet(final String name)
{
final Object valueObj = getAttribute(name);
if (valueObj == null)
{
return null;
}
else if (valueObj instanceof Collection)
{
@SuppressWarnings("unchecked")
final Collection<Integer> coll = (Collection<Integer>)valueObj;
return ImmutableSet.copyOf(coll);
}
else
{
final String valueStr = valueObj.toString().trim();
if (valueStr.isEmpty())
{
return ImmutableSet.of();
}

try
{
return Splitter.on(",").omitEmptyStrings()
.trimResults()
.splitToList(valueStr).stream()
.map(Integer::parseInt)
.collect(ImmutableSet.toImmutableSet());
}
catch (final Exception ex)
{
throw new AdempiereException("Cannot convert attribute value to integer Set", ex)
.setParameter("attributeName", name)
.setParameter("attributeValue", valueStr)
.setParameter("lookupValue", this)
.appendParametersToMessage();
}
}
}

public static final class StringLookupValue extends LookupValue
{
public static final StringLookupValue of(final String value, final String displayName)
{
return new StringLookupValue(value, ImmutableTranslatableString.constant(displayName));
final Map<String, Object> attributes = null;
return new StringLookupValue(value, ImmutableTranslatableString.constant(displayName), attributes);
}

public static final StringLookupValue of(final String value, final ITranslatableString displayName)
{
return new StringLookupValue(value, displayName);
final Map<String, Object> attributes = null;
return new StringLookupValue(value, displayName, attributes);
}

public static final StringLookupValue unknown(final String value)
{
return new StringLookupValue(value, ImmutableTranslatableString.constant("<" + value + ">"));
final Map<String, Object> attributes = null;
return new StringLookupValue(value, ImmutableTranslatableString.constant("<" + value + ">"), attributes);
}

private Integer idInt; // lazy

private StringLookupValue(final String id, final ITranslatableString displayName)
@Builder
private StringLookupValue(
@NonNull final String id,
@Nullable final ITranslatableString displayName,
@Singular final Map<String, Object> attributes)
{
super(id, displayName);
super(id, displayName, attributes);
}

@Override
Expand All @@ -268,12 +370,14 @@ public static final class IntegerLookupValue extends LookupValue
*/
public static final IntegerLookupValue of(final int id, final String displayName)
{
return new IntegerLookupValue(id, ImmutableTranslatableString.anyLanguage(displayName));
final Map<String, Object> attributes = null;
return new IntegerLookupValue(id, ImmutableTranslatableString.anyLanguage(displayName), attributes);
}

public static final IntegerLookupValue of(final int id, final ITranslatableString displayName)
{
return new IntegerLookupValue(id, displayName);
final Map<String, Object> attributes = null;
return new IntegerLookupValue(id, displayName, attributes);
}

public static final IntegerLookupValue of(final StringLookupValue stringLookupValue)
Expand All @@ -282,17 +386,23 @@ public static final IntegerLookupValue of(final StringLookupValue stringLookupVa
{
return null;
}
return new IntegerLookupValue(stringLookupValue.getIdAsInt(), stringLookupValue.displayName);
final Map<String, Object> attributes = null;
return new IntegerLookupValue(stringLookupValue.getIdAsInt(), stringLookupValue.displayName, attributes);
}

public static final IntegerLookupValue unknown(final int id)
{
return new IntegerLookupValue(id, ImmutableTranslatableString.constant("<" + id + ">"));
final Map<String, Object> attributes = null;
return new IntegerLookupValue(id, ImmutableTranslatableString.constant("<" + id + ">"), attributes);
}

private IntegerLookupValue(final int id, final ITranslatableString displayName)
@Builder
private IntegerLookupValue(
final int id,
final ITranslatableString displayName,
@Singular final Map<String, Object> attributes)
{
super(id, displayName);
super(id, displayName, attributes);
}

@Override
Expand Down
Loading

0 comments on commit a959ad6

Please sign in to comment.