Commit 5f895d68 by Tuomas Riihimäki

Fix error when saving empty t-shirt size

1 parent 7f5bb682
Pipeline #67 passed
in 0 seconds
......@@ -21,6 +21,7 @@ package fi.codecrew.moya.model;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.json.JsonValue;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
......@@ -66,10 +67,28 @@ public class GenericEntity extends EntityEquals implements ModelInterface, Entit
@Transient
public String getMetaStringValue(String key) {
try {
return getMeta().getString(key);
JsonObject m = getMeta();
if(m != null && m.containsKey(key)) {
return getMeta().getString(key);
}
} catch (NullPointerException e) {
// this is normal if key is not found etc.
return "";
}
return "";
}
@Transient
public void deleteMetaKey(String key){
final JsonObject m = getMeta();
if(m != null){
JsonObjectBuilder metaBuilder = Json.createObjectBuilder();
getMeta()
.entrySet()
.stream()
.filter(e->!e.getKey().equals(key))
.forEach(e -> metaBuilder.add(e.getKey(), e.getValue()));
setMeta(metaBuilder.build());
}
}
......@@ -79,15 +98,10 @@ public class GenericEntity extends EntityEquals implements ModelInterface, Entit
JsonObjectBuilder metaBuilder = Json.createObjectBuilder();
if (getMeta() != null) {
for (String valKey : getMeta().keySet()) {
if (!valKey.equals(key)) {
metaBuilder.add(valKey, getMeta().get(valKey));
}
}
getMeta().entrySet().forEach(e -> metaBuilder.add(e.getKey(), e.getValue()));
}
metaBuilder.add(key, value);
setMeta(metaBuilder.build());
}
......
......@@ -58,6 +58,7 @@ public class User extends GenericEntity implements IUser {
public static final String ANONYMOUS_LOGINNAME = "anonymous";
private static final long serialVersionUID = -1632200627103418206L;
private static final String SHIRT_SIZE_METAKEY = "shirtSize";
@Column(name = "created", nullable = false, updatable = false)
@Temporal(TemporalType.TIMESTAMP)
......@@ -433,12 +434,16 @@ public class User extends GenericEntity implements IUser {
@Transient
public String getShirtSize() {
return getMetaStringValue("shirtSize");
return getMetaStringValue(SHIRT_SIZE_METAKEY);
}
@Transient
public void setShirtSize(String size) {
setMetaStringValue("shirtSize", size);
if(size == null || size.isEmpty()){
deleteMetaKey(SHIRT_SIZE_METAKEY);
}else {
setMetaStringValue(SHIRT_SIZE_METAKEY, size);
}
}
public String getLocale() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!