From 2912fd80dc3faedd820c4a756137cfd588441d8e Mon Sep 17 00:00:00 2001 From: Teo Sarca Date: Thu, 9 Nov 2017 10:25:23 +0200 Subject: [PATCH] JSONLookupValue: use only the new encoding https://github.com/metasfresh/metasfresh-webui-api/issues/666 --- .../datatypes/json/JSONLookupValue.java | 63 +++++++------------ 1 file changed, 24 insertions(+), 39 deletions(-) diff --git a/src/main/java/de/metas/ui/web/window/datatypes/json/JSONLookupValue.java b/src/main/java/de/metas/ui/web/window/datatypes/json/JSONLookupValue.java index 5a67ecae5..e4d865b4c 100644 --- a/src/main/java/de/metas/ui/web/window/datatypes/json/JSONLookupValue.java +++ b/src/main/java/de/metas/ui/web/window/datatypes/json/JSONLookupValue.java @@ -1,13 +1,12 @@ package de.metas.ui.web.window.datatypes.json; -import java.io.Serializable; import java.util.Map; -import java.util.Set; import org.compiere.util.Env; import org.compiere.util.NamePair; -import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonInclude; @@ -21,6 +20,7 @@ import de.metas.ui.web.window.datatypes.LookupValue.IntegerLookupValue; import de.metas.ui.web.window.datatypes.LookupValue.StringLookupValue; import io.swagger.annotations.ApiModel; +import lombok.EqualsAndHashCode; import lombok.NonNull; /* @@ -46,8 +46,9 @@ */ @ApiModel(value = "lookup-value", description = "pair of { field : value}") -@SuppressWarnings("serial") -public final class JSONLookupValue implements Serializable +@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, isGetterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE) +@EqualsAndHashCode +public final class JSONLookupValue { public static final JSONLookupValue of(final String key, final String caption) { @@ -79,32 +80,31 @@ public static final JSONLookupValue ofNamePair(final NamePair namePair) public static final IntegerLookupValue integerLookupValueFromJsonMap(final Map map) { - final Set> entrySet = map.entrySet(); - final Map.Entry firstEntry = entrySet.iterator().next(); - - String idStr = firstEntry.getKey(); - if (idStr == null) + final Object keyObj = map.get(PROPERTY_Key); + if (keyObj == null) { return null; } - idStr = idStr.trim(); - if (idStr.isEmpty()) + final String keyStr = keyObj.toString().trim(); + if (keyStr.isEmpty()) { return null; } + final int keyInt = Integer.parseInt(keyStr); - final int id = Integer.parseInt(idStr); - final ITranslatableString displayName = ImmutableTranslatableString.anyLanguage(firstEntry.getValue().toString()); + final Object captionObj = map.get(PROPERTY_Caption); + final String caption = captionObj != null ? captionObj.toString() : ""; + final ITranslatableString displayName = ImmutableTranslatableString.anyLanguage(caption); @SuppressWarnings("unchecked") final Map attributes = (Map)map.get(PROPERTY_Attributes); if (attributes == null || attributes.isEmpty()) { - return IntegerLookupValue.of(id, displayName); + return IntegerLookupValue.of(keyInt, displayName); } return IntegerLookupValue.builder() - .id(id) + .id(keyInt) .displayName(displayName) .attributes(attributes) .build(); @@ -112,35 +112,32 @@ public static final IntegerLookupValue integerLookupValueFromJsonMap(final Map map) { - final Set> entrySet = map.entrySet(); - final Map.Entry firstEntry = entrySet.iterator().next(); + final Object keyObj = map.get(PROPERTY_Key); + final String key = keyObj != null ? keyObj.toString() : null; - final String id = firstEntry.getKey(); - final ITranslatableString displayName = ImmutableTranslatableString.anyLanguage(firstEntry.getValue().toString()); + final Object captionObj = map.get(PROPERTY_Caption); + final String caption = captionObj != null ? captionObj.toString() : ""; + final ITranslatableString displayName = ImmutableTranslatableString.anyLanguage(caption); @SuppressWarnings("unchecked") final Map attributes = (Map)map.get(PROPERTY_Attributes); if (attributes == null || attributes.isEmpty()) { - return StringLookupValue.of(id, displayName); + return StringLookupValue.of(key, displayName); } return StringLookupValue.builder() - .id(id) + .id(key) .displayName(displayName) .attributes(attributes) .build(); } - // NOTE: this shall be exported as first entry in form of "key:name". - @Deprecated - private final Map keyAndCaptionMap; - private static final String PROPERTY_Key = "key"; @JsonProperty(PROPERTY_Key) private final String key; @JsonIgnore - private Integer keyAsInt = null; // lazy + private transient Integer keyAsInt = null; // lazy private static final String PROPERTY_Caption = "caption"; @JsonProperty(PROPERTY_Caption) @@ -160,8 +157,6 @@ private JSONLookupValue( this.key = key; this.caption = caption; this.attributes = attributes != null && !attributes.isEmpty() ? ImmutableMap.copyOf(attributes) : ImmutableMap.of(); - - keyAndCaptionMap = ImmutableMap.of(key, caption); } @Override @@ -174,19 +169,11 @@ public String toString() .toString(); } - @JsonAnyGetter - public Map getKeyAndCaptionMap() - { - return keyAndCaptionMap; - } - - @JsonIgnore public String getKey() { return key; } - @JsonIgnore public int getKeyAsInt() { if (keyAsInt == null) @@ -197,13 +184,11 @@ public int getKeyAsInt() return keyAsInt; } - @JsonIgnore public String getCaption() { return caption; } - @JsonIgnore public Map getAttributes() { return attributes;