Commit 75ecf0a9 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

configuration controller separated from service

parent cd8024b9
......@@ -5,6 +5,7 @@ import java.util.stream.Collectors;
import javax.servlet.ServletContext;
import org.apache.http.HttpStatus;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -17,6 +18,8 @@ import org.springframework.web.bind.annotation.*;
import lcsb.mapviewer.annotation.services.ModelAnnotator;
import lcsb.mapviewer.annotation.services.annotators.ElementAnnotator;
import lcsb.mapviewer.annotation.services.dapi.DapiConnectionException;
import lcsb.mapviewer.annotation.services.dapi.DapiConnector;
import lcsb.mapviewer.annotation.services.dapi.dto.ReleaseDto;
import lcsb.mapviewer.annotation.services.dapi.dto.UserDto;
import lcsb.mapviewer.api.BaseController;
import lcsb.mapviewer.common.Pair;
......@@ -38,6 +41,7 @@ import lcsb.mapviewer.model.user.ConfigurationOption;
import lcsb.mapviewer.model.user.annotator.BioEntityField;
import lcsb.mapviewer.modelutils.map.ClassTreeNode;
import lcsb.mapviewer.modelutils.map.ElementUtils;
import lcsb.mapviewer.services.ObjectExistsException;
import lcsb.mapviewer.services.QueryException;
import lcsb.mapviewer.services.interfaces.IConfigurationService;
......@@ -47,19 +51,19 @@ public class ConfigurationController extends BaseController {
Logger logger = LogManager.getLogger();
private ConfigurationRestImpl configurationRestImpl;
private DapiConnector dapiConnector;
private IConfigurationService configurationService;
private ServletContext context;
private ModelAnnotator modelAnnotator;
private List<Converter> modelConverters;
@Autowired
public ConfigurationController(ConfigurationRestImpl configurationController,
public ConfigurationController(DapiConnector dapiConnector,
IConfigurationService configurationService,
ServletContext context,
List<Converter> modelConverters,
ModelAnnotator modelAnnotator) {
this.configurationRestImpl = configurationController;
this.dapiConnector = dapiConnector;
this.configurationService = configurationService;
this.modelConverters = modelConverters;
this.context = context;
......@@ -100,30 +104,65 @@ public class ConfigurationController extends BaseController {
@GetMapping(value = "/dapi/")
public Map<String, Object> getDapi() {
return configurationRestImpl.getDapiConfig();
Map<String, Object> result = new HashMap<>();
result.put("validConnection", dapiConnector.isValidConnection());
return result;
}
@PreAuthorize("hasAuthority('IS_ADMIN')")
@PostMapping(value = "/dapi:registerUser")
public void registerDapiUser(@RequestBody UserDto user) throws DapiConnectionException, QueryException {
configurationRestImpl.registerDapiUser(user);
try {
dapiConnector.registerUser(user);
} catch (DapiConnectionException e) {
if (e.getStatusCode() == HttpStatus.SC_CONFLICT) {
throw new ObjectExistsException("User with given login/email exists", e);
} else if (e.getStatusCode() == HttpStatus.SC_BAD_REQUEST) {
throw new QueryException("Invalid input data", e);
}
}
configurationService.setConfigurationValue(ConfigurationElementType.DAPI_LOGIN, user.getLogin());
configurationService.setConfigurationValue(ConfigurationElementType.DAPI_PASSWORD, user.getPassword());
}
@GetMapping(value = "/dapi/database/")
public List<Map<String, Object>> getDapiDatabases() {
return configurationRestImpl.getDapiDatabases();
List<Map<String, Object>> result = new ArrayList<>();
try {
for (String string : dapiConnector.getDatabases()) {
Map<String, Object> entry = new HashMap<>();
entry.put("name", string);
result.add(entry);
}
} catch (DapiConnectionException e) {
logger.error("Problem with dapi", e);
}
return result;
}
@GetMapping(value = "/dapi/database/{database}/release/")
public List<Map<String, Object>> getDapiReleases(@PathVariable(value = "database") String database) {
return configurationRestImpl.getDapiDatabaseReleases(database);
List<Map<String, Object>> result = new ArrayList<>();
try {
for (ReleaseDto release : dapiConnector.getReleases(database)) {
Map<String, Object> entry = new HashMap<>();
entry.put("name", release.getName());
entry.put("licenseUrl", release.getLicense().getUrl());
entry.put("licenseContent", release.getLicense().getContent());
entry.put("accepted", dapiConnector.isReleaseAccepted(database, release));
result.add(entry);
}
} catch (DapiConnectionException e) {
logger.error("Problem with dapi", e);
}
return result;
}
@PreAuthorize("hasAuthority('IS_ADMIN')")
@PostMapping(value = "/dapi/database/{database}/release/{release:.+}:acceptLicense")
public void acceptReleasesLicense(@PathVariable(value = "database") String database,
@PathVariable(value = "release") String release) throws DapiConnectionException {
configurationRestImpl.acceptDapiRelease(database, release);
dapiConnector.acceptRelease(database, release);
}
static class OptionUpdateData {
......
package lcsb.mapviewer.api.configuration;
import java.util.*;
import org.apache.http.HttpStatus;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import lcsb.mapviewer.annotation.services.ModelAnnotator;
import lcsb.mapviewer.annotation.services.dapi.DapiConnectionException;
import lcsb.mapviewer.annotation.services.dapi.DapiConnector;
import lcsb.mapviewer.annotation.services.dapi.dto.ReleaseDto;
import lcsb.mapviewer.annotation.services.dapi.dto.UserDto;
import lcsb.mapviewer.api.BaseRestImpl;
import lcsb.mapviewer.common.exception.InvalidArgumentException;
import lcsb.mapviewer.model.user.ConfigurationElementType;
import lcsb.mapviewer.model.user.ConfigurationOption;
import lcsb.mapviewer.services.ObjectExistsException;
import lcsb.mapviewer.services.QueryException;
import lcsb.mapviewer.services.interfaces.IConfigurationService;
@Transactional
@Service
public class ConfigurationRestImpl extends BaseRestImpl {
/**
* Default class logger.
*/
private Logger logger = LogManager.getLogger();
private IConfigurationService configurationService;
private ModelAnnotator modelAnnotator;
private DapiConnector dapiConnector;
@Autowired
public ConfigurationRestImpl(IConfigurationService configurationService, ModelAnnotator modelAnnotator,
DapiConnector dapiConnector) {
this.configurationService = configurationService;
this.modelAnnotator = modelAnnotator;
this.dapiConnector = dapiConnector;
}
/**
* @return the configurationService
* @see #configurationService
*/
public IConfigurationService getConfigurationService() {
return configurationService;
}
/**
* @param configurationService
* the configurationService to set
* @see #configurationService
*/
public void setConfigurationService(IConfigurationService configurationService) {
this.configurationService = configurationService;
}
public Map<String, Object> getDapiConfig() {
Map<String, Object> result = new HashMap<>();
result.put("validConnection", dapiConnector.isValidConnection());
return result;
}
public List<Map<String, Object>> getDapiDatabases() {
List<Map<String, Object>> result = new ArrayList<>();
try {
for (String string : dapiConnector.getDatabases()) {
Map<String, Object> entry = new HashMap<>();
entry.put("name", string);
result.add(entry);
}
} catch (DapiConnectionException e) {
logger.error("Problem with dapi", e);
}
return result;
}
public List<Map<String, Object>> getDapiDatabaseReleases(String database) {
List<Map<String, Object>> result = new ArrayList<>();
try {
for (ReleaseDto release : dapiConnector.getReleases(database)) {
Map<String, Object> entry = new HashMap<>();
entry.put("name", release.getName());
entry.put("licenseUrl", release.getLicense().getUrl());
entry.put("licenseContent", release.getLicense().getContent());
entry.put("accepted", dapiConnector.isReleaseAccepted(database, release));
result.add(entry);
}
} catch (DapiConnectionException e) {
logger.error("Problem with dapi", e);
}
return result;
}
public void acceptDapiRelease(String database, String release) throws DapiConnectionException {
dapiConnector.acceptRelease(database, release);
}
public void registerDapiUser(UserDto user) throws DapiConnectionException, QueryException {
try {
dapiConnector.registerUser(user);
} catch (DapiConnectionException e) {
if (e.getStatusCode() == HttpStatus.SC_CONFLICT) {
throw new ObjectExistsException("User with given login/email exists", e);
} else if (e.getStatusCode() == HttpStatus.SC_BAD_REQUEST) {
throw new QueryException("Invalid input data", e);
}
}
configurationService.setConfigurationValue(ConfigurationElementType.DAPI_LOGIN, user.getLogin());
configurationService.setConfigurationValue(ConfigurationElementType.DAPI_PASSWORD, user.getPassword());
}
}
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