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

old methods removed

parent 1ba34cc8
package lcsb.mapviewer.api;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
......@@ -37,15 +22,11 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.gson.Gson;
import lcsb.mapviewer.annotation.services.dapi.DapiConnectionException;
import lcsb.mapviewer.common.Configuration;
import lcsb.mapviewer.common.MimeType;
import lcsb.mapviewer.common.exception.InvalidStateException;
import lcsb.mapviewer.common.exception.InvalidXmlSchemaException;
import lcsb.mapviewer.model.cache.FileEntry;
import lcsb.mapviewer.services.ObjectExistsException;
import lcsb.mapviewer.services.ObjectNotFoundException;
......@@ -54,10 +35,6 @@ import lcsb.mapviewer.services.QueryException;
public abstract class BaseController {
private Logger logger = LogManager.getLogger();
private ObjectMapper mapper = new ObjectMapper();
private Transformer mathMlTransformer;
@ExceptionHandler({ Exception.class })
public ResponseEntity<Object> handleException(Exception e) {
if (e instanceof AccessDeniedException) {
......@@ -102,56 +79,6 @@ public abstract class BaseController {
return new ResponseEntity<Object>(new Gson().toJson(response), httpHeaders, status);
}
public Map parseBody(String body) throws QueryException {
try {
if (body == null || body.isEmpty()) {
return new TreeMap<>();
}
ObjectNode result = mapper.readValue(body, ObjectNode.class);
return mapper.convertValue(result, Map.class);
} catch (IOException e) {
throw new QueryException("Cannot parse body", e);
}
}
public Map[] extractListBody(String body) throws QueryException {
try {
if (body == null || body.isEmpty()) {
return new Map[0];
}
return mapper.readValue(body, Map[].class);
} catch (IOException e) {
throw new QueryException("Cannot parse body", e);
}
}
protected Map getData(Map<String, Object> node, String objectName) {
return (Map) node.get(objectName);
}
protected Map<String, String> parsePostBody(String body) {
Map<String, String> result = new TreeMap<>();
String[] parameters = body.split("&");
for (String string : parameters) {
int position = string.indexOf("=");
if (position < 0) {
position = string.length();
}
String key = string.substring(0, position);
String value = null;
if (position < string.length()) {
value = string.substring(position + 1);
try {
value = URLDecoder.decode(value, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new InvalidStateException("Cannot decode input", e);
}
}
result.put(key, value);
}
return result;
}
protected ResponseEntity<byte[]> sendAsZip(FileEntry originalFile) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos);
......@@ -168,62 +95,4 @@ public abstract class BaseController {
.header("Content-Disposition", "attachment; filename=" + originalFile.getOriginalFileName() + ".zip")
.body(baos.toByteArray());
}
public Integer getId(String id) {
if (id != null) {
try {
return Integer.parseInt(id);
} catch (NumberFormatException e) {
logger.debug("Invalid id: " + id);
return null;
}
}
return null;
}
protected Integer parseInteger(String value, String fieldName) throws QueryException {
if (value == null) {
return null;
} else if (value instanceof String) {
if (((String) value).equalsIgnoreCase("")) {
return null;
} else {
try {
return Integer.parseInt((String) value);
} catch (NumberFormatException e) {
throw new QueryException("Invalid " + fieldName + " value: " + value);
}
}
} else {
throw new QueryException("Invalid " + fieldName + " value: " + value);
}
}
protected String mathMLToPresentationML(String xmlContent)
throws IOException, InvalidXmlSchemaException, TransformerException {
Transformer transformer = getMathMLTransformer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
transformer.transform(new StreamSource(new ByteArrayInputStream(xmlContent.getBytes())), new StreamResult(baos));
String result = baos.toString("UTF-8");
if (result.startsWith("<?xml")) {
result = result.substring(result.indexOf(">") + 1);
}
return result;
}
private Transformer getMathMLTransformer()
throws TransformerFactoryConfigurationError, IOException, TransformerConfigurationException {
if (this.mathMlTransformer == null) {
TransformerFactory factory = TransformerFactory.newInstance();
Resource resource = new ClassPathResource("mathmlc2p.xsl");
InputStream styleInputStream = resource.getInputStream();
StreamSource stylesource = new StreamSource(styleInputStream);
this.mathMlTransformer = factory.newTransformer(stylesource);
}
return this.mathMlTransformer;
}
}
......@@ -68,7 +68,7 @@ public class BioEntitiesController extends BaseController {
types.add(str.toLowerCase());
}
}
int mapId = parseInteger(modelId, "modelId");
int mapId = Integer.valueOf(modelId);
return searchService.getClosestElements(projectId, mapId, pointCoordinates, count, perfectMatch, types);
} else if (query != null) {
......
package lcsb.mapviewer.api.projects.models.bioEntities.reactions;
import java.awt.geom.Line2D;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
......@@ -11,8 +10,6 @@ import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import javax.xml.transform.TransformerException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -24,8 +21,11 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import lcsb.mapviewer.api.BaseController;
import lcsb.mapviewer.common.exception.InvalidXmlSchemaException;
import lcsb.mapviewer.common.geometry.PointTransformation;
import lcsb.mapviewer.model.map.kinetics.SbmlFunction;
import lcsb.mapviewer.model.map.kinetics.SbmlKinetics;
......@@ -37,6 +37,7 @@ import lcsb.mapviewer.model.map.reaction.Product;
import lcsb.mapviewer.model.map.reaction.Reactant;
import lcsb.mapviewer.model.map.reaction.Reaction;
import lcsb.mapviewer.model.map.reaction.ReactionNode;
import lcsb.mapviewer.modelutils.serializer.MathMLSerializer;
import lcsb.mapviewer.services.QueryException;
import lcsb.mapviewer.services.interfaces.IReactionService;
......@@ -64,36 +65,36 @@ public class ReactionsController extends BaseController {
@RequestParam(value = "columns", defaultValue = "") String columns,
@RequestParam(value = "participantId", defaultValue = "") String participantId) throws QueryException {
Set<Integer> ids = new LinkedHashSet<>();
if (!id.equals("")) {
for (String str : id.split(",")) {
ids.add(parseInteger(str, "id"));
}
Set<Integer> ids = new LinkedHashSet<>();
if (!id.equals("")) {
for (String str : id.split(",")) {
ids.add(Integer.valueOf(str));
}
Set<Integer> participantIds = new LinkedHashSet<>();
if (!participantId.equals("")) {
for (String str : participantId.split(",")) {
participantIds.add(parseInteger(str, "participantId"));
}
}
Set<Integer> participantIds = new LinkedHashSet<>();
if (!participantId.equals("")) {
for (String str : participantId.split(",")) {
participantIds.add(Integer.valueOf(str));
}
}
Set<String> columnsSet = createReactionColumnSet(columns);
Set<String> columnsSet = createReactionColumnSet(columns);
List<Reaction> reactions = reactionService.getReactionById(projectId, modelId, ids, participantIds);
List<Reaction> reactions = reactionService.getReactionById(projectId, modelId, ids, participantIds);
List<Map<String, Object>> result = new ArrayList<>();
for (Reaction reaction : reactions) {
result.add(preparedReaction(reaction, columnsSet));
}
return result;
List<Map<String, Object>> result = new ArrayList<>();
for (Reaction reaction : reactions) {
result.add(preparedReaction(reaction, columnsSet));
}
return result;
}
private Map<String, Object> preparedReaction(Reaction reaction, Set<String> columnsSet) {
Map<String, Object> result = new TreeMap<>();
for (String string : columnsSet) {
String column = string.toLowerCase();
Object value;
switch (column) {
private Map<String, Object> preparedReaction(Reaction reaction, Set<String> columnsSet) {
Map<String, Object> result = new TreeMap<>();
for (String string : columnsSet) {
String column = string.toLowerCase();
Object value;
switch (column) {
case "id":
case "idobject":
value = reaction.getId();
......@@ -156,139 +157,145 @@ public class ReactionsController extends BaseController {
default:
value = "Unknown column";
break;
}
result.put(string, value);
}
return result;
result.put(string, value);
}
return result;
}
private List<Map<String, Object>> getLines(Reaction reaction) {
List<Map<String, Object>> result = new ArrayList<>();
for (Reactant reactant : reaction.getReactants()) {
result.addAll(getLines(reactant));
}
for (Product product : reaction.getProducts()) {
result.addAll(getLines(product));
}
for (Modifier modifier : reaction.getModifiers()) {
result.addAll(getLines(modifier));
}
for (NodeOperator operator : reaction.getOperators()) {
result.addAll(getLines(operator));
}
return result;
private List<Map<String, Object>> getLines(Reaction reaction) {
List<Map<String, Object>> result = new ArrayList<>();
for (Reactant reactant : reaction.getReactants()) {
result.addAll(getLines(reactant));
}
for (Product product : reaction.getProducts()) {
result.addAll(getLines(product));
}
for (Modifier modifier : reaction.getModifiers()) {
result.addAll(getLines(modifier));
}
for (NodeOperator operator : reaction.getOperators()) {
result.addAll(getLines(operator));
}
return result;
}
protected List<Map<String, Object>> getLines(AbstractNode reactant) {
// order in this method is to keep the order from previous version, it can be
// changed in the future, but I wanted to make that it's giving the same results
// when refactoring
List<Map<String, Object>> result = new ArrayList<>();
List<Line2D> startLines = new ArrayList<>();
List<Line2D> middleLines = new ArrayList<>();
List<Line2D> endLines = new ArrayList<>();
List<Line2D> lines = reactant.getLine().getLines();
protected List<Map<String, Object>> getLines(AbstractNode reactant) {
// order in this method is to keep the order from previous version, it can
// be
// changed in the future, but I wanted to make that it's giving the same
// results
// when refactoring
List<Map<String, Object>> result = new ArrayList<>();
List<Line2D> startLines = new ArrayList<>();
List<Line2D> middleLines = new ArrayList<>();
List<Line2D> endLines = new ArrayList<>();
if (reactant instanceof Reactant) {
startLines.add(lines.get(0));
for (int i = 1; i < lines.size(); i++) {
middleLines.add(lines.get(i));
}
} else if (reactant instanceof Product) {
endLines.add(lines.get(lines.size() - 1));
for (int i = 0; i < lines.size() - 1; i++) {
middleLines.add(lines.get(i));
}
} else if (reactant instanceof Modifier) {
middleLines.add(lines.get(lines.size() - 1));
for (int i = 0; i < lines.size() - 1; i++) {
middleLines.add(lines.get(i));
}
} else {
middleLines.addAll(lines);
}
List<Line2D> lines = reactant.getLine().getLines();
for (Line2D line2d : startLines) {
result.add(lineToMap(line2d, "START"));
if (reactant instanceof Reactant) {
startLines.add(lines.get(0));
for (int i = 1; i < lines.size(); i++) {
middleLines.add(lines.get(i));
}
for (Line2D line2d : endLines) {
result.add(lineToMap(line2d, "END"));
} else if (reactant instanceof Product) {
endLines.add(lines.get(lines.size() - 1));
for (int i = 0; i < lines.size() - 1; i++) {
middleLines.add(lines.get(i));
}
for (Line2D line2d : middleLines) {
result.add(lineToMap(line2d, "MIDDLE"));
} else if (reactant instanceof Modifier) {
middleLines.add(lines.get(lines.size() - 1));
for (int i = 0; i < lines.size() - 1; i++) {
middleLines.add(lines.get(i));
}
return result;
} else {
middleLines.addAll(lines);
}
private Map<String, Object> lineToMap(Line2D line2d, String type) {
Map<String, Object> result = new LinkedHashMap<>();
result.put("start", line2d.getP1());
result.put("end", line2d.getP2());
result.put("type", type);
return result;
for (Line2D line2d : startLines) {
result.add(lineToMap(line2d, "START"));
}
private Map<String, Object> createReactionNode(ReactionNode node) {
Map<String, Object> result = new TreeMap<>();
result.put("aliasId", node.getElement().getId());
result.put("stoichiometry", node.getStoichiometry());
return result;
for (Line2D line2d : endLines) {
result.add(lineToMap(line2d, "END"));
}
for (Line2D line2d : middleLines) {
result.add(lineToMap(line2d, "MIDDLE"));
}
private Map<String, Object> kineticsToMap(SbmlKinetics kinetics) {
if (kinetics == null) {
return null;
}
Map<String, Object> result = new TreeMap<>();
result.put("definition", kinetics.getDefinition());
try {
result.put("mathMlPresentation", super.mathMLToPresentationML(kinetics.getDefinition()));
} catch (IOException | InvalidXmlSchemaException | TransformerException e) {
logger.error("Problems with transforming kinetics", e);
}
return result;
}
List<Integer> functionIds = new ArrayList<>();
for (SbmlFunction function : kinetics.getFunctions()) {
functionIds.add(function.getId());
}
result.put("functionIds", functionIds);
List<Integer> parameterIds = new ArrayList<>();
for (SbmlParameter parameter : kinetics.getParameters()) {
parameterIds.add(parameter.getId());
}
result.put("parameterIds", parameterIds);
return result;
private Map<String, Object> lineToMap(Line2D line2d, String type) {
Map<String, Object> result = new LinkedHashMap<>();
result.put("start", line2d.getP1());
result.put("end", line2d.getP2());
result.put("type", type);
return result;
}
private Map<String, Object> createReactionNode(ReactionNode node) {
Map<String, Object> result = new TreeMap<>();
result.put("aliasId", node.getElement().getId());
result.put("stoichiometry", node.getStoichiometry());
return result;
}
private Map<String, Object> kineticsToMap(SbmlKinetics kinetics) {
if (kinetics == null) {
return null;
}
Map<String, Object> result = new TreeMap<>();
result.put("definition", kinetics.getDefinition());
try {
ObjectMapper objectMapper = new ObjectMapper();
SimpleModule s = new SimpleModule();
s.addSerializer(String.class, new MathMLSerializer());
objectMapper.registerModule(s);
public List<String> getAvailableReactionColumns() {
List<String> result = new ArrayList<>();
result.add("id");
result.add("reactionId");
result.add("modelId");
result.add("type");
result.add("lines");
result.add("kineticLaw");
result.add("centerPoint");
result.add("products");
result.add("reactants");
result.add("modifiers");
result.add("hierarchyVisibilityLevel");
result.add("references");
result.add("notes");
return result;
result.put("mathMlPresentation", objectMapper.writeValueAsString(kinetics.getDefinition()));
} catch (JsonProcessingException e) {
logger.error("Problems with transforming kinetics", e);
}
private Set<String> createReactionColumnSet(String columns) {
Set<String> columnsSet = new LinkedHashSet<>();
if (columns.equals("")) {
columnsSet.addAll(getAvailableReactionColumns());
} else {
Collections.addAll(columnsSet, columns.split(","));
}
return columnsSet;
List<Integer> functionIds = new ArrayList<>();
for (SbmlFunction function : kinetics.getFunctions()) {
functionIds.add(function.getId());
}
result.put("functionIds", functionIds);
List<Integer> parameterIds = new ArrayList<>();
for (SbmlParameter parameter : kinetics.getParameters()) {
parameterIds.add(parameter.getId());
}
result.put("parameterIds", parameterIds);
return result;
}
public List<String> getAvailableReactionColumns() {
List<String> result = new ArrayList<>();
result.add("id");
result.add("reactionId");
result.add("modelId");
result.add("type");
result.add("lines");
result.add("kineticLaw");
result.add("centerPoint");
result.add("products");
result.add("reactants");
result.add("modifiers");
result.add("hierarchyVisibilityLevel");
result.add("references");
result.add("notes");
return result;
}
private Set<String> createReactionColumnSet(String columns) {
Set<String> columnsSet = new LinkedHashSet<>();
if (columns.equals("")) {
columnsSet.addAll(getAvailableReactionColumns());
} else {
Collections.addAll(columnsSet, columns.split(","));
}
return columnsSet;
}
}
\ No newline at end of file
......@@ -39,8 +39,7 @@ public class ParametersController extends BaseController {
public Map<String, Object> getParameter(
@PathVariable(value = "projectId") String projectId,
@PathVariable(value = "modelId") String modelId,
@PathVariable(value = "parameterId") String parameterId) throws QueryException {
int id = parseInteger(parameterId, "parameterId");
@PathVariable(value = "parameterId") Integer id) throws QueryException {
for (SbmlParameter parameter : getGlobalParametersFromProject(projectId, modelId)) {
if (parameter.getId() == id) {
return parameterToMap(parameter, true);
......