diff --git a/src/main/java/de/thm/arsnova/entities/Entity.java b/src/main/java/de/thm/arsnova/entities/Entity.java index 23c3103aee634a7152484da30f20f9a5b95af198..4de04ab4b7d1923cbfce3a50649e1af520db468f 100644 --- a/src/main/java/de/thm/arsnova/entities/Entity.java +++ b/src/main/java/de/thm/arsnova/entities/Entity.java @@ -25,7 +25,7 @@ public interface Entity { void setId(String id); @JsonView(View.Persistence.class) - default String getType() { - return getClass().getSimpleName(); + default Class<? extends Entity> getType() { + return getClass(); } } diff --git a/src/main/java/de/thm/arsnova/entities/serialization/CouchDbDocumentMixIn.java b/src/main/java/de/thm/arsnova/entities/serialization/CouchDbDocumentMixIn.java index bd9eac28218364c76150719592300e4e97943f0d..6928dad0589ea1452f69ec7f1c7ea9fe383639f2 100644 --- a/src/main/java/de/thm/arsnova/entities/serialization/CouchDbDocumentMixIn.java +++ b/src/main/java/de/thm/arsnova/entities/serialization/CouchDbDocumentMixIn.java @@ -20,6 +20,8 @@ package de.thm.arsnova.entities.serialization; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import de.thm.arsnova.entities.Entity; @JsonIgnoreProperties(value = {"type"}, allowGetters = true) public abstract class CouchDbDocumentMixIn { @@ -34,4 +36,7 @@ public abstract class CouchDbDocumentMixIn { abstract String getRevision(); @JsonProperty("_rev") abstract String setRevision(String rev); + + @JsonSerialize(converter = CouchDbTypeFieldConverter.class) + abstract Class<? extends Entity> getType(); } diff --git a/src/main/java/de/thm/arsnova/entities/serialization/CouchDbTypeFieldConverter.java b/src/main/java/de/thm/arsnova/entities/serialization/CouchDbTypeFieldConverter.java new file mode 100644 index 0000000000000000000000000000000000000000..186124630542c4bc7fd52ffb099421230bd70fc0 --- /dev/null +++ b/src/main/java/de/thm/arsnova/entities/serialization/CouchDbTypeFieldConverter.java @@ -0,0 +1,47 @@ +/* + * This file is part of ARSnova Backend. + * Copyright (C) 2012-2017 The ARSnova Team + * + * ARSnova Backend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * ARSnova Backend is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +package de.thm.arsnova.entities.serialization; + +import com.fasterxml.jackson.databind.JavaType; +import com.fasterxml.jackson.databind.type.TypeFactory; +import com.fasterxml.jackson.databind.util.Converter; +import de.thm.arsnova.entities.Entity; +import java.util.HashMap; +import java.util.Map; + +public class CouchDbTypeFieldConverter implements Converter<Class<? extends Entity>, String> { + private static final Map<Class<? extends Entity>, String> typeMapping = new HashMap<>(); + + { + } + + @Override + public String convert(Class<? extends Entity> aClass) { + return typeMapping.get(aClass); + } + + @Override + public JavaType getInputType(TypeFactory typeFactory) { + return typeFactory.constructGeneralizedType(typeFactory.constructType(Class.class), Entity.class); + } + + @Override + public JavaType getOutputType(TypeFactory typeFactory) { + return typeFactory.constructType(String.class); + } +}