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

Add Jackson converter for v3 Entity type

parent 1a3fcbd6
Branches
No related merge requests found
......@@ -28,6 +28,6 @@ public class CouchDbDocumentModule extends SimpleModule {
@Override
public void setupModule(SetupContext context) {
context.setMixInAnnotations(Entity.class, CouchDbDocumentMixIn.class);
context.setMixInAnnotations(de.thm.arsnova.entities.migration.v2.Entity.class, CouchDbDocumentMixIn.class);
context.setMixInAnnotations(de.thm.arsnova.entities.migration.v2.Entity.class, CouchDbDocumentV2MixIn.class);
}
}
/*
* 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.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 CouchDbDocumentV2MixIn {
@JsonProperty("_id")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
abstract String getId();
@JsonProperty("_id") abstract void setId(String id);
@JsonProperty("_rev")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
abstract String getRevision();
@JsonProperty("_rev") abstract String setRevision(String rev);
@JsonSerialize(converter = CouchDbTypeFieldV2Converter.class)
abstract Class<? extends Entity> getType();
}
......@@ -20,36 +20,13 @@ 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.migration.v2.Answer;
import de.thm.arsnova.entities.migration.v2.Comment;
import de.thm.arsnova.entities.migration.v2.Content;
import de.thm.arsnova.entities.migration.v2.DbUser;
import de.thm.arsnova.entities.migration.v2.Entity;
import de.thm.arsnova.entities.migration.v2.LogEntry;
import de.thm.arsnova.entities.migration.v2.Motd;
import de.thm.arsnova.entities.migration.v2.MotdList;
import de.thm.arsnova.entities.migration.v2.Room;
import java.util.HashMap;
import java.util.Map;
import de.thm.arsnova.entities.Entity;
public class CouchDbTypeFieldConverter implements Converter<Class<? extends Entity>, String> {
private static final Map<Class<? extends Entity>, String> typeMapping = new HashMap<>();
{
typeMapping.put(LogEntry.class, "log");
typeMapping.put(DbUser.class, "userdetails");
typeMapping.put(Motd.class, "motd");
typeMapping.put(MotdList.class, "motdlist");
typeMapping.put(Room.class, "session");
typeMapping.put(Comment.class, "interposed_question");
typeMapping.put(Content.class, "skill_question");
typeMapping.put(Answer.class, "skill_question_answer");
}
@Override
public String convert(Class<? extends Entity> aClass) {
return typeMapping.get(aClass);
return aClass.getSimpleName();
}
@Override
......
/*
* 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.migration.v2.Answer;
import de.thm.arsnova.entities.migration.v2.Comment;
import de.thm.arsnova.entities.migration.v2.Content;
import de.thm.arsnova.entities.migration.v2.DbUser;
import de.thm.arsnova.entities.migration.v2.Entity;
import de.thm.arsnova.entities.migration.v2.LogEntry;
import de.thm.arsnova.entities.migration.v2.Motd;
import de.thm.arsnova.entities.migration.v2.MotdList;
import de.thm.arsnova.entities.migration.v2.Room;
import java.util.HashMap;
import java.util.Map;
public class CouchDbTypeFieldV2Converter implements Converter<Class<? extends Entity>, String> {
private static final Map<Class<? extends Entity>, String> typeMapping = new HashMap<>();
{
typeMapping.put(LogEntry.class, "log");
typeMapping.put(DbUser.class, "userdetails");
typeMapping.put(Motd.class, "motd");
typeMapping.put(MotdList.class, "motdlist");
typeMapping.put(Room.class, "session");
typeMapping.put(Comment.class, "interposed_question");
typeMapping.put(Content.class, "skill_question");
typeMapping.put(Answer.class, "skill_question_answer");
}
@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