Commit b22dfcc7 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

PluginDataEntrySerializer implemented

parent 6f92f050
......@@ -2,9 +2,18 @@ package lcsb.mapviewer.model.plugin;
import java.io.Serializable;
import javax.persistence.*;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lcsb.mapviewer.model.user.User;
import lcsb.mapviewer.modelutils.serializer.model.plugin.PluginDataEntrySerializer;
/**
* Single entry of data stored by the plugin.
......@@ -13,6 +22,7 @@ import lcsb.mapviewer.model.user.User;
*
*/
@Entity
@JsonSerialize(using = PluginDataEntrySerializer.class)
public class PluginDataEntry implements Serializable {
/**
......
package lcsb.mapviewer.modelutils.serializer.model.plugin;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import lcsb.mapviewer.model.plugin.PluginDataEntry;
public class PluginDataEntrySerializer extends JsonSerializer<PluginDataEntry> {
@Override
public void serialize(final PluginDataEntry entry, final JsonGenerator gen,
final SerializerProvider serializers)
throws IOException {
gen.writeStartObject();
gen.writeStringField("key", entry.getKey());
gen.writeStringField("value", entry.getValue());
if (entry.getUser() != null) {
gen.writeStringField("user", entry.getUser().getLogin());
}
gen.writeEndObject();
}
}
\ No newline at end of file
......@@ -11,16 +11,16 @@ import lcsb.mapviewer.model.plugin.Plugin;
public class PluginSerializer extends JsonSerializer<Plugin> {
@Override
public void serialize(final Plugin fileEntry, final JsonGenerator gen,
public void serialize(final Plugin plugin, final JsonGenerator gen,
final SerializerProvider serializers)
throws IOException {
gen.writeStartObject();
gen.writeStringField("hash", fileEntry.getHash());
gen.writeStringField("name", fileEntry.getName());
gen.writeStringField("version", fileEntry.getVersion());
gen.writeBooleanField("isPublic", fileEntry.isPublic());
gen.writeBooleanField("isDefault", fileEntry.isDefault());
gen.writeObjectField("urls", fileEntry.getUrls());
gen.writeStringField("hash", plugin.getHash());
gen.writeStringField("name", plugin.getName());
gen.writeStringField("version", plugin.getVersion());
gen.writeBooleanField("isPublic", plugin.isPublic());
gen.writeBooleanField("isDefault", plugin.isDefault());
gen.writeObjectField("urls", plugin.getUrls());
gen.writeEndObject();
}
}
\ No newline at end of file
......@@ -30,11 +30,7 @@ public class PluginDataEntryDao extends BaseDao<PluginDataEntry> {
List<Pair<String, Object>> params = new ArrayList<>();
params.add(new Pair<>("plugin_id", plugin.getId()));
params.add(new Pair<>("key", key));
if (user == null) {
params.add(new Pair<>("user_id", null));
} else {
params.add(new Pair<>("user_id", user.getId()));
}
params.add(new Pair<>("user", user));
List<PluginDataEntry> entries = getElementsByParameters(params);
if (entries.size() > 0) {
return entries.get(0);
......
......@@ -23,6 +23,7 @@ import org.springframework.web.bind.annotation.RestController;
import lcsb.mapviewer.api.BaseController;
import lcsb.mapviewer.model.plugin.Plugin;
import lcsb.mapviewer.model.plugin.PluginDataEntry;
import lcsb.mapviewer.model.user.User;
import lcsb.mapviewer.services.ObjectNotFoundException;
import lcsb.mapviewer.services.QueryException;
......@@ -109,7 +110,7 @@ public class PluginController extends BaseController {
}
@PostMapping(value = "/{hash}/data/users/{key}")
public Map<String, Object> createPluginDataEntry(
public PluginDataEntry createPluginDataEntry(
Authentication authentication,
@PathVariable(value = "hash") String hash,
@PathVariable(value = "key") String key,
......@@ -131,7 +132,7 @@ public class PluginController extends BaseController {
}
@PostMapping(value = "/{hash}/data/global/{key}")
public Map<String, Object> createPluginDataEntry(
public PluginDataEntry createPluginDataEntry(
@PathVariable(value = "hash") String hash,
@PathVariable(value = "key") String key,
@RequestParam(value = "value", defaultValue = "") String value) throws QueryException {
......@@ -149,19 +150,32 @@ public class PluginController extends BaseController {
}
@GetMapping(value = "/{hash}/data/users/{key}")
public Map<String, Object> getPluginDataEntry(
public PluginDataEntry getPluginDataEntry(
Authentication authentication,
@PathVariable(value = "hash") String hash,
@PathVariable(value = "key") String key) throws ObjectNotFoundException {
User user = userService.getUserByLogin(authentication.getName());
return pluginRest.getPluginDataEntry(hash, user, key);
return getPluginDataEntry(hash, user, key);
}
@GetMapping(value = "/{hash}/data/global/{key}")
public Map<String, Object> getPluginDataEntry(
public PluginDataEntry getPluginDataEntry(
@PathVariable(value = "hash") String hash,
@PathVariable(value = "key") String key) throws ObjectNotFoundException {
return pluginRest.getPluginDataEntry(hash, null, key);
return getPluginDataEntry(hash, null, key);
}
public PluginDataEntry getPluginDataEntry(String hash, User user, String key) throws ObjectNotFoundException {
Plugin plugin = pluginService.getByHash(hash);
if (plugin == null) {
throw new ObjectNotFoundException("Plugin doesn't exist");
}
PluginDataEntry entry = pluginService.getEntryByKey(plugin, key, user);
if (entry == null) {
throw new ObjectNotFoundException("Entry doesn't exist");
}
return entry;
}
}
\ No newline at end of file
......@@ -69,7 +69,7 @@ public class PluginRestImpl extends BaseRestImpl {
return plugin;
}
public Map<String, Object> createPluginDataEntry(String hash, User user, String key, String value)
public PluginDataEntry createPluginDataEntry(String hash, User user, String key, String value)
throws QueryException {
Plugin plugin = pluginDao.getByHash(hash);
if (plugin == null) {
......@@ -99,7 +99,7 @@ public class PluginRestImpl extends BaseRestImpl {
pluginDataEntryDao.update(entry);
}
return pluginEntryToMap(entry);
return entry;
}
public void deletePluginDataEntry(String hash, User user, String key) throws QueryException {
......@@ -116,29 +116,6 @@ public class PluginRestImpl extends BaseRestImpl {
}
}
private Map<String, Object> pluginEntryToMap(PluginDataEntry entry) {
Map<String, Object> result = new TreeMap<>();
result.put("key", entry.getKey());
result.put("value", entry.getValue());
if (entry.getUser() != null) {
result.put("user", entry.getUser().getLogin());
}
return result;
}
public Map<String, Object> getPluginDataEntry(String hash, User user, String key) throws ObjectNotFoundException {
Plugin plugin = pluginDao.getByHash(hash);
if (plugin == null) {
throw new ObjectNotFoundException("Plugin doesn't exist");
}
PluginDataEntry entry = pluginDataEntryDao.getByKey(plugin, key, user);
if (entry == null) {
throw new ObjectNotFoundException("Entry doesn't exist");
}
return pluginEntryToMap(entry);
}
public Plugin updatePlugin(String hash, Map<String, Object> data) throws QueryException {
Plugin plugin = pluginDao.getByHash(hash);
if (plugin == null) {
......
......@@ -9,6 +9,7 @@ import org.springframework.transaction.annotation.Transactional;
import lcsb.mapviewer.model.plugin.Plugin;
import lcsb.mapviewer.model.plugin.PluginDataEntry;
import lcsb.mapviewer.model.user.User;
import lcsb.mapviewer.persist.dao.plugin.PluginDao;
import lcsb.mapviewer.persist.dao.plugin.PluginDataEntryDao;
import lcsb.mapviewer.services.interfaces.IPluginService;
......@@ -52,4 +53,11 @@ public class PluginService implements IPluginService {
}
return result;
}
@Override
public PluginDataEntry getEntryByKey(Plugin plugin, String key, User user) {
PluginDataEntry result = pluginDataEntryDao.getByKey(plugin, key, user);
Hibernate.initialize(result.getUser());
return result;
}
}
......@@ -3,6 +3,8 @@ package lcsb.mapviewer.services.interfaces;
import java.util.List;
import lcsb.mapviewer.model.plugin.Plugin;
import lcsb.mapviewer.model.plugin.PluginDataEntry;
import lcsb.mapviewer.model.user.User;
public interface IPluginService {
......@@ -12,4 +14,6 @@ public interface IPluginService {
List<Plugin> getAll();
PluginDataEntry getEntryByKey(Plugin plugin, String key, User user);
}
Markdown is supported
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