Commit 6f92f050 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

plugin serializer implemented

parent 91a43bf5
package lcsb.mapviewer.model.plugin;
import java.io.Serializable;
import java.util.*;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OrderBy;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lcsb.mapviewer.common.comparator.IntegerComparator;
import lcsb.mapviewer.modelutils.serializer.model.plugin.PluginSerializer;
/**
* Meta data of the plugin used in the system.
......@@ -14,6 +27,7 @@ import lcsb.mapviewer.common.comparator.IntegerComparator;
*
*/
@Entity
@JsonSerialize(using = PluginSerializer.class)
public class Plugin implements Serializable {
public static final Comparator<? super Plugin> ID_COMPARATOR = new Comparator<Plugin>() {
......
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.Plugin;
public class PluginSerializer extends JsonSerializer<Plugin> {
@Override
public void serialize(final Plugin fileEntry, 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.writeEndObject();
}
}
\ No newline at end of file
package lcsb.mapviewer.api.plugins;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
......@@ -50,7 +51,7 @@ public class PluginController extends BaseController {
@PreAuthorize("(not #isPublic and #isDefault!=true) or hasAuthority('IS_ADMIN')")
@PostMapping(value = "/")
public Map<String, Object> createPlugin(
public Plugin createPlugin(
@RequestParam(value = "hash") String hash,
@RequestParam(value = "name") String name,
@RequestParam(value = "version") String version,
......@@ -62,7 +63,7 @@ public class PluginController extends BaseController {
@PreAuthorize("hasAuthority('IS_ADMIN')")
@PatchMapping(value = "/{hash}")
public Map<String, Object> updatePlugin(
public Plugin updatePlugin(
@PathVariable(value = "hash") String hash,
@RequestBody String body) throws QueryException {
Map<String, Object> node = parseBody(body);
......@@ -71,14 +72,29 @@ public class PluginController extends BaseController {
}
@GetMapping(value = "/")
public List<Map<String, Object>> getPlugins(
public List<Plugin> getPlugins(
@RequestParam(value = "onlyPublic", defaultValue = "false") String onlyPublic) {
return pluginRest.getPlugins(onlyPublic.equalsIgnoreCase("true"));
boolean onlyPublicBool = onlyPublic.equalsIgnoreCase("true");
List<Plugin> plugins = pluginService.getAll();
plugins.sort(Plugin.ID_COMPARATOR);
List<Plugin> result = new ArrayList<>();
for (Plugin plugin : plugins) {
if (!onlyPublicBool || plugin.isPublic()) {
result.add(plugin);
}
}
return result;
}
@GetMapping(value = "/{hash}")
public Map<String, Object> getPlugin(@PathVariable(value = "hash") String hash) throws ObjectNotFoundException {
return pluginRest.getPlugin(hash);
public Plugin getPlugin(@PathVariable(value = "hash") String hash) throws ObjectNotFoundException {
Plugin plugin = pluginService.getByHash(hash);
if (plugin == null) {
throw new ObjectNotFoundException("Plugin doesn't exist");
}
return plugin;
}
@PreAuthorize("hasAuthority('IS_ADMIN')")
......
package lcsb.mapviewer.api.plugins;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
......@@ -10,6 +9,7 @@ import java.util.TreeMap;
import org.apache.commons.validator.routines.UrlValidator;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -39,7 +39,7 @@ public class PluginRestImpl extends BaseRestImpl {
this.pluginDataEntryDao = pluginDataEntryDao;
}
public Map<String, Object> createPlugin(String hash, String name, String version, String url, boolean isPublic,
public Plugin createPlugin(String hash, String name, String version, String url, boolean isPublic,
Boolean isDefault) throws QueryException {
if (!new UrlValidator(UrlValidator.ALLOW_LOCAL_URLS).isValid(url)) {
throw new QueryException("Invalid url: " + url);
......@@ -66,27 +66,7 @@ public class PluginRestImpl extends BaseRestImpl {
}
pluginDao.add(plugin);
}
return pluginToMap(plugin);
}
private Map<String, Object> pluginToMap(Plugin plugin) {
Map<String, Object> result = new TreeMap<>();
result.put("hash", plugin.getHash());
result.put("name", plugin.getName());
result.put("version", plugin.getVersion());
result.put("isPublic", plugin.isPublic());
result.put("isDefault", plugin.isDefault());
plugin.getUrls().contains("");
result.put("urls", plugin.getUrls());
return result;
}
public Map<String, Object> getPlugin(String hash) throws ObjectNotFoundException {
Plugin plugin = pluginDao.getByHash(hash);
if (plugin == null) {
throw new ObjectNotFoundException("Plugin doesn't exist");
}
return pluginToMap(plugin);
return plugin;
}
public Map<String, Object> createPluginDataEntry(String hash, User user, String key, String value)
......@@ -159,20 +139,7 @@ public class PluginRestImpl extends BaseRestImpl {
return pluginEntryToMap(entry);
}
public List<Map<String, Object>> getPlugins(boolean onlyPublic) {
List<Plugin> plugins = pluginDao.getAll();
plugins.sort(Plugin.ID_COMPARATOR);
List<Map<String, Object>> result = new ArrayList<>();
for (Plugin plugin : plugins) {
if (!onlyPublic || plugin.isPublic()) {
result.add(pluginToMap(plugin));
}
}
return result;
}
public Map<String, Object> updatePlugin(String hash, Map<String, Object> data) throws QueryException {
public Plugin updatePlugin(String hash, Map<String, Object> data) throws QueryException {
Plugin plugin = pluginDao.getByHash(hash);
if (plugin == null) {
throw new ObjectNotFoundException("Plugin doesn't exist");
......@@ -227,6 +194,7 @@ public class PluginRestImpl extends BaseRestImpl {
}
pluginDao.update(plugin);
return getPlugin(hash);
Hibernate.initialize(plugin.getUrls());
return plugin;
}
}
......@@ -2,6 +2,7 @@ package lcsb.mapviewer.services.impl;
import java.util.List;
import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -27,7 +28,11 @@ public class PluginService implements IPluginService {
@Override
public Plugin getByHash(String hash) {
return pluginDao.getByHash(hash);
Plugin result = pluginDao.getByHash(hash);
if (result != null) {
Hibernate.initialize(result.getUrls());
}
return result;
}
@Override
......@@ -38,4 +43,13 @@ public class PluginService implements IPluginService {
}
pluginDao.delete(plugin);
}
@Override
public List<Plugin> getAll() {
List<Plugin> result = pluginDao.getAll();
for (Plugin plugin : result) {
Hibernate.initialize(plugin.getUrls());
}
return result;
}
}
package lcsb.mapviewer.services.interfaces;
import java.util.List;
import lcsb.mapviewer.model.plugin.Plugin;
public interface IPluginService {
......@@ -8,4 +10,6 @@ public interface IPluginService {
void delete(Plugin plugin);
List<Plugin> getAll();
}
......@@ -390,7 +390,6 @@ public class PluginControllerIntegrationTest extends ControllerIntegrationTest {
.andExpect(status().is2xxSuccessful());
assertNull(pluginService.getByHash(plugin.getHash()));
}
private Plugin createPlugin() throws Exception {
......
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