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

PluginRestImpl removed

parent 51bd86b3
Pipeline #44227 failed with stage
in 16 minutes and 14 seconds
......@@ -35,19 +35,14 @@ import lcsb.mapviewer.services.interfaces.IUserService;
@RestController
@RequestMapping(value = "/api/plugins", produces = MediaType.APPLICATION_JSON_VALUE)
public class PluginController extends BaseController {
/**
* Default class logger.
*/
@SuppressWarnings("unused")
private Logger logger = LogManager.getLogger();
private PluginRestImpl pluginRest;
private IUserService userService;
private IPluginService pluginService;
@Autowired
public PluginController(PluginRestImpl pluginRest, IUserService userService, IPluginService pluginService) {
this.pluginRest = pluginRest;
public PluginController(IUserService userService, IPluginService pluginService) {
this.userService = userService;
this.pluginService = pluginService;
}
......@@ -89,14 +84,64 @@ public class PluginController extends BaseController {
return plugin;
}
static class UpdatePluginData {
public Boolean isPublic;
public Boolean isDefault;
public String version;
public String name;
public String hash;
public Integer id;
public List<String> urls = new ArrayList<>();
}
static class UpdateData {
public UpdatePluginData plugin;
}
@PreAuthorize("hasAuthority('IS_ADMIN')")
@PatchMapping(value = "/{hash}")
public Plugin updatePlugin(
@PathVariable(value = "hash") String hash,
@RequestBody String body) throws QueryException {
Map<String, Object> node = parseBody(body);
Map<String, Object> data = getData(node, "plugin");
return pluginRest.updatePlugin(hash, data);
@RequestBody UpdateData body) throws QueryException {
UpdatePluginData data = body.plugin;
Plugin plugin = pluginService.getByHash(hash);
if (plugin == null) {
throw new ObjectNotFoundException("Plugin doesn't exist");
}
if (data == null) {
throw new QueryException("plugin data is not defined");
}
if (data.isPublic != null) {
plugin.setPublic(data.isPublic);
}
if (data.isDefault != null) {
plugin.setDefault(data.isDefault);
}
if (data.name != null) {
plugin.setName(data.name);
}
if (data.version != null) {
plugin.setVersion(data.version);
}
if (data.hash != null) {
if (!data.hash.equals(hash)) {
throw new QueryException("plugin hash cannot be changed");
}
}
if (data.id != null) {
if (data.id != plugin.getId() && data.id != 0) {
throw new QueryException("plugin id cannot be changed");
}
}
if (data.urls.size() > 0) {
plugin.getUrls().clear();
for (String string : data.urls) {
plugin.getUrls().add(string.toString());
}
}
pluginService.update(plugin);
return pluginService.getByHash(plugin.getHash());
}
@GetMapping(value = "/")
......@@ -199,7 +244,6 @@ public class PluginController extends BaseController {
return entry;
}
private Object deletePluginDataEntry(String hash, User user, String key) throws QueryException {
Plugin plugin = pluginService.getByHash(hash);
if (plugin == null) {
......@@ -215,8 +259,8 @@ public class PluginController extends BaseController {
Map<String, Object> result = new HashMap<>();
result.put("status", "ok");
return result;
}
}
private PluginDataEntry createPluginDataEntry(String hash, User user, String key, String value)
throws QueryException {
Plugin plugin = pluginService.getByHash(hash);
......@@ -249,5 +293,5 @@ public class PluginController extends BaseController {
return entry;
}
}
\ No newline at end of file
package lcsb.mapviewer.api.plugins;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import java.util.Set;
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;
import lcsb.mapviewer.api.BaseRestImpl;
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.ObjectNotFoundException;
import lcsb.mapviewer.services.QueryException;
@Transactional
@Service
public class PluginRestImpl extends BaseRestImpl {
Logger logger = LogManager.getLogger();
private PluginDao pluginDao;
private PluginDataEntryDao pluginDataEntryDao;
@Autowired
public PluginRestImpl(PluginDao pluginDao, PluginDataEntryDao pluginDataEntryDao) {
this.pluginDao = pluginDao;
this.pluginDataEntryDao = pluginDataEntryDao;
}
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");
}
if (data == null) {
throw new QueryException("plugin data is not defined");
}
Set<String> fields = data.keySet();
for (String fieldName : fields) {
Object value = data.get(fieldName);
String stringValue = null;
Integer intValue = null;
Boolean boolValue = null;
if (value instanceof String) {
stringValue = (String) value;
}
if (value instanceof Integer) {
intValue = (Integer) value;
}
if (value instanceof Boolean) {
boolValue = (Boolean) value;
}
if (fieldName.equalsIgnoreCase("isPublic")) {
plugin.setPublic(boolValue);
} else if (fieldName.equalsIgnoreCase("isDefault")) {
plugin.setDefault(boolValue);
} else if (fieldName.equalsIgnoreCase("name")) {
plugin.setName(stringValue);
} else if (fieldName.equalsIgnoreCase("version")) {
plugin.setVersion(stringValue);
} else if (fieldName.equalsIgnoreCase("hash")) {
if (!stringValue.equals(hash)) {
throw new QueryException("plugin hash cannot be changed");
}
} else if (fieldName.equalsIgnoreCase("id")) {
if (intValue != 0 && intValue != plugin.getId()) {
throw new QueryException("plugin id cannot be changed");
}
} else if (fieldName.equalsIgnoreCase("urls")) {
if (value instanceof List) {
if (((List) value).size() > 0) {
plugin.getUrls().clear();
for (Object string : (List) value) {
plugin.getUrls().add(string.toString());
}
}
}
} else {
throw new QueryException("Unknown field: " + fieldName);
}
}
pluginDao.update(plugin);
Hibernate.initialize(plugin.getUrls());
return plugin;
}
}
......@@ -388,7 +388,7 @@ public class PluginControllerIntegrationTest extends ControllerIntegrationTest {
mockMvc.perform(request)
.andExpect(status().is2xxSuccessful());
assertNull(pluginService.getByHash(plugin.getHash()));
}
......@@ -421,19 +421,17 @@ public class PluginControllerIntegrationTest extends ControllerIntegrationTest {
String body = new Gson().toJson(bodyMap);
RequestBuilder request = patch("/api/plugins/" + plugin.getHash())
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.contentType(MediaType.APPLICATION_JSON)
.content(body)
.session(session);
mockMvc.perform(request)
.andExpect(status().is2xxSuccessful());
callInSeparateThreadNoReturn(() -> {
Plugin p = pluginDao.getByHash(plugin.getHash());
assertEquals(updatedData.getName(), p.getName());
assertEquals(updatedData.getVersion(), p.getVersion());
assertEquals(updatedData.isDefault(), p.isDefault());
});
Plugin p = pluginService.getByHash(plugin.getHash());
assertEquals(updatedData.getName(), p.getName());
assertEquals(updatedData.getVersion(), p.getVersion());
assertEquals(updatedData.isDefault(), p.isDefault());
}
}
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