Skip to content
Snippets Groups Projects
Commit 16236f7b authored by Daniel Gerhardt's avatar Daniel Gerhardt
Browse files

Add Jackson Converter for type field of Entities

This decouples type identifiers used in CouchDB from Java type names.
parent c2757a21
Branches
1 merge request!68Migration to Ektorp and refactoring of the database layer
......@@ -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();
}
}
......@@ -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();
}
/*
* 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);
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment