Skip to content

Commit

Permalink
JSONLookupValue: use only the new encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
teosarca committed Nov 9, 2017
1 parent 9393a77 commit 2912fd8
Showing 1 changed file with 24 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

/*
Expand All @@ -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)
{
Expand Down Expand Up @@ -79,68 +80,64 @@ public static final JSONLookupValue ofNamePair(final NamePair namePair)

public static final IntegerLookupValue integerLookupValueFromJsonMap(final Map<String, Object> map)
{
final Set<Map.Entry<String, Object>> entrySet = map.entrySet();
final Map.Entry<String, Object> 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<String, Object> attributes = (Map<String, Object>)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();
}

public static final StringLookupValue stringLookupValueFromJsonMap(final Map<String, Object> map)
{
final Set<Map.Entry<String, Object>> entrySet = map.entrySet();
final Map.Entry<String, Object> 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<String, Object> attributes = (Map<String, Object>)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<String, String> 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)
Expand All @@ -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
Expand All @@ -174,19 +169,11 @@ public String toString()
.toString();
}

@JsonAnyGetter
public Map<String, String> getKeyAndCaptionMap()
{
return keyAndCaptionMap;
}

@JsonIgnore
public String getKey()
{
return key;
}

@JsonIgnore
public int getKeyAsInt()
{
if (keyAsInt == null)
Expand All @@ -197,13 +184,11 @@ public int getKeyAsInt()
return keyAsInt;
}

@JsonIgnore
public String getCaption()
{
return caption;
}

@JsonIgnore
public Map<String, Object> getAttributes()
{
return attributes;
Expand Down

0 comments on commit 2912fd8

Please sign in to comment.