Commit 51bd86b3 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

create plugin data entry moved to service

parent 89a494d5
package lcsb.mapviewer.api.plugins;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
......@@ -142,7 +143,7 @@ public class PluginController extends BaseController {
@PathVariable(value = "key") String key,
@RequestParam(value = "value", defaultValue = "") String value) throws QueryException {
User user = userService.getUserByLogin(authentication.getName());
return pluginRest.createPluginDataEntry(hash, user, key, value);
return createPluginDataEntry(hash, user, key, value);
}
@DeleteMapping(value = "/{hash}/data/users/{key}")
......@@ -159,7 +160,7 @@ public class PluginController extends BaseController {
@PathVariable(value = "hash") String hash,
@PathVariable(value = "key") String key,
@RequestParam(value = "value", defaultValue = "") String value) throws QueryException {
return pluginRest.createPluginDataEntry(hash, null, key, value);
return createPluginDataEntry(hash, null, key, value);
}
@DeleteMapping(value = "/{hash}/data/global/{key}")
......@@ -215,4 +216,38 @@ public class PluginController extends BaseController {
result.put("status", "ok");
return result;
}
private PluginDataEntry createPluginDataEntry(String hash, User user, String key, String value)
throws QueryException {
Plugin plugin = pluginService.getByHash(hash);
if (plugin == null) {
throw new ObjectNotFoundException("Plugin doesn't exist");
}
int length = 0;
if (value != null) {
try {
length = value.getBytes("UTF-8").length;
} catch (UnsupportedEncodingException e) {
logger.error(e, e);
}
}
if (length >= 1024 * 1024) {
throw new QueryException("Data entry value too big (" + length + "; max length = " + 1024 * 1024 + ")");
}
PluginDataEntry entry = pluginService.getEntryByKey(plugin, key, user);
if (entry == null) {
entry = new PluginDataEntry();
entry.setKey(key);
entry.setPlugin(plugin);
entry.setUser(user);
entry.setValue(value);
pluginService.add(entry);
} else {
entry.setValue(value);
pluginService.update(entry);
}
return entry;
}
}
\ No newline at end of file
......@@ -39,39 +39,6 @@ public class PluginRestImpl extends BaseRestImpl {
this.pluginDataEntryDao = pluginDataEntryDao;
}
public PluginDataEntry createPluginDataEntry(String hash, User user, String key, String value)
throws QueryException {
Plugin plugin = pluginDao.getByHash(hash);
if (plugin == null) {
throw new ObjectNotFoundException("Plugin doesn't exist");
}
int length = 0;
if (value != null) {
try {
length = value.getBytes("UTF-8").length;
} catch (UnsupportedEncodingException e) {
logger.error(e, e);
}
}
if (length >= 1024 * 1024) {
throw new QueryException("Data entry value too big (" + length + "; max length = " + 1024 * 1024 + ")");
}
PluginDataEntry entry = pluginDataEntryDao.getByKey(plugin, key, user);
if (entry == null) {
entry = new PluginDataEntry();
entry.setKey(key);
entry.setPlugin(plugin);
entry.setUser(user);
entry.setValue(value);
pluginDataEntryDao.add(entry);
} else {
entry.setValue(value);
pluginDataEntryDao.update(entry);
}
return entry;
}
public Plugin updatePlugin(String hash, Map<String, Object> data) throws QueryException {
Plugin plugin = pluginDao.getByHash(hash);
if (plugin == null) {
......
......@@ -57,7 +57,9 @@ public class PluginService implements IPluginService {
@Override
public PluginDataEntry getEntryByKey(Plugin plugin, String key, User user) {
PluginDataEntry result = pluginDataEntryDao.getByKey(plugin, key, user);
Hibernate.initialize(result.getUser());
if (result != null) {
Hibernate.initialize(result.getUser());
}
return result;
}
......@@ -75,4 +77,14 @@ public class PluginService implements IPluginService {
public void add(Plugin plugin) {
pluginDao.add(plugin);
}
@Override
public void add(PluginDataEntry entry) {
pluginDataEntryDao.add(entry);
}
@Override
public void update(PluginDataEntry entry) {
pluginDataEntryDao.add(entry);
}
}
......@@ -22,4 +22,8 @@ public interface IPluginService {
void add(Plugin plugin);
void add(PluginDataEntry entry);
void update(PluginDataEntry entry);
}
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