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