Commit 49e87a6e authored by Piotr Gawron's avatar Piotr Gawron
Browse files

ConfigurationOptionSerializaer implemented

parent 41c1f1ba
......@@ -4,6 +4,10 @@ import java.io.Serializable;
import javax.persistence.*;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lcsb.mapviewer.modelutils.serializer.ConfigurationOptionSerializer;
/**
* This class represents one configurable parameter of the system.
*
......@@ -11,6 +15,7 @@ import javax.persistence.*;
*
*/
@Entity
@JsonSerialize(using = ConfigurationOptionSerializer.class)
public class ConfigurationOption implements Serializable {
/**
......
package lcsb.mapviewer.modelutils.serializer;
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.user.ConfigurationElementEditType;
import lcsb.mapviewer.model.user.ConfigurationOption;
public class ConfigurationOptionSerializer extends JsonSerializer<ConfigurationOption> {
@Override
public void serialize(final ConfigurationOption option, final JsonGenerator gen, final SerializerProvider serializers)
throws IOException {
gen.writeStartObject();
gen.writeNumberField("idObject", option.getId());
gen.writeStringField("type", option.getType().name());
gen.writeStringField("valueType", option.getType().getEditType().name());
gen.writeStringField("commonName", option.getType().getCommonName());
gen.writeBooleanField("isServerSide", option.getType().isServerSide());
// don't send password over API
if (!option.getType().getEditType().equals(ConfigurationElementEditType.PASSWORD)) {
gen.writeStringField("value", option.getValue());
}
if (option.getType().getGroup() != null) {
gen.writeStringField("group", option.getType().getGroup().getCommonName());
}
gen.writeEndObject();
}
}
\ No newline at end of file
......@@ -19,6 +19,7 @@ import lcsb.mapviewer.annotation.services.dapi.DapiConnectionException;
import lcsb.mapviewer.annotation.services.dapi.dto.UserDto;
import lcsb.mapviewer.api.BaseController;
import lcsb.mapviewer.model.security.PrivilegeType;
import lcsb.mapviewer.model.user.ConfigurationOption;
import lcsb.mapviewer.services.QueryException;
import lcsb.mapviewer.services.interfaces.IConfigurationService;
......@@ -65,11 +66,11 @@ public class ConfigurationController extends BaseController {
}
@GetMapping(value = "/options/")
public List<Map<String, Object>> getOptions(Authentication authentication) {
public List<ConfigurationOption> getOptions(Authentication authentication) {
boolean isAdmin = authentication.getAuthorities()
.contains(new SimpleGrantedAuthority(PrivilegeType.IS_ADMIN.toString()));
return configurationRestImpl.getAllValues().stream()
.filter(option -> !((Boolean) option.get("isServerSide")) || isAdmin)
.filter(option -> !option.getType().isServerSide() || isAdmin)
.collect(Collectors.toList());
}
......@@ -103,7 +104,7 @@ public class ConfigurationController extends BaseController {
@PreAuthorize("hasAuthority('IS_ADMIN')")
@PatchMapping(value = "/options/{option}")
public Map<String, Object> updateOption(@RequestBody String body, @PathVariable(value = "option") String option)
public ConfigurationOption updateOption(@RequestBody String body, @PathVariable(value = "option") String option)
throws IOException, QueryException {
Map<String, Object> node = parseBody(body);
Map<String, Object> data = getData(node, "option");
......
......@@ -30,7 +30,8 @@ import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.model.map.species.field.ModificationState;
import lcsb.mapviewer.model.overlay.DataOverlayType;
import lcsb.mapviewer.model.security.PrivilegeType;
import lcsb.mapviewer.model.user.*;
import lcsb.mapviewer.model.user.ConfigurationElementType;
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;
......@@ -60,29 +61,8 @@ public class ConfigurationRestImpl extends BaseRestImpl {
this.dapiConnector = dapiConnector;
}
public List<Map<String, Object>> getAllValues() {
List<Map<String, Object>> result = new ArrayList<>();
for (ConfigurationOption option : configurationService.getAllValues()) {
result.add(optionToMap(option));
}
return result;
}
private Map<String, Object> optionToMap(ConfigurationOption option) {
Map<String, Object> result = new TreeMap<>();
result.put("idObject", option.getId());
result.put("type", option.getType());
result.put("valueType", option.getType().getEditType());
result.put("commonName", option.getType().getCommonName());
result.put("isServerSide", option.getType().isServerSide());
// don't send password over API
if (!option.getType().getEditType().equals(ConfigurationElementEditType.PASSWORD)) {
result.put("value", option.getValue());
}
if (option.getType().getGroup() != null) {
result.put("group", option.getType().getGroup().getCommonName());
}
return result;
public List<ConfigurationOption> getAllValues() {
return configurationService.getAllValues();
}
/**
......@@ -269,7 +249,7 @@ public class ConfigurationRestImpl extends BaseRestImpl {
return result;
}
public Map<String, Object> updateOption(String option, Map<String, Object> data) throws QueryException {
public ConfigurationOption updateOption(String option, Map<String, Object> data) throws QueryException {
ConfigurationElementType type = ConfigurationElementType.valueOf(option);
String value = (String) data.get("value");
try {
......@@ -277,7 +257,7 @@ public class ConfigurationRestImpl extends BaseRestImpl {
} catch (InvalidArgumentException e) {
throw new QueryException(e.getMessage(), e);
}
return optionToMap(configurationService.getValue(type));
return configurationService.getValue(type);
}
public List<Map<String, Object>> getUnitTypes() {
......
......@@ -14,6 +14,7 @@ import lcsb.mapviewer.model.map.MiriamType;
import lcsb.mapviewer.model.map.reaction.type.StateTransitionReaction;
import lcsb.mapviewer.model.map.species.GenericProtein;
import lcsb.mapviewer.model.user.ConfigurationElementType;
import lcsb.mapviewer.model.user.ConfigurationOption;
import lcsb.mapviewer.model.user.annotator.BioEntityField;
import lcsb.mapviewer.services.QueryException;
......@@ -38,7 +39,7 @@ public class ConfigurationRestImplTest extends RestTestFunctions {
@Test
public void testGetAllParams() throws Exception {
List<Map<String, Object>> list = configurationRestImpl.getAllValues();
List<ConfigurationOption> list = configurationRestImpl.getAllValues();
assertTrue(list.size() > 0);
}
......@@ -53,7 +54,7 @@ public class ConfigurationRestImplTest extends RestTestFunctions {
public void testSetSmtpPortToValid() throws Exception {
Map<String, Object> data = new HashMap<>();
data.put("value", "255");
Map<String, Object> result = configurationRestImpl.updateOption(
ConfigurationOption result = configurationRestImpl.updateOption(
ConfigurationElementType.EMAIL_SMTP_PORT.name(), data);
assertNotNull(result);
}
......
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