Commit 990b18fb authored by Piotr Gawron's avatar Piotr Gawron
Browse files

convert to image moved to controller

parent 9ad4bfce
package lcsb.mapviewer.api.convert;
import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
......@@ -17,9 +19,11 @@ import org.springframework.web.bind.annotation.*;
import lcsb.mapviewer.api.BaseController;
import lcsb.mapviewer.commands.CreateHierarchyCommand;
import lcsb.mapviewer.common.MinervaLoggerAppender;
import lcsb.mapviewer.common.Pair;
import lcsb.mapviewer.common.exception.InvalidStateException;
import lcsb.mapviewer.converter.*;
import lcsb.mapviewer.converter.graphics.DrawingException;
import lcsb.mapviewer.converter.ConverterParams;
import lcsb.mapviewer.converter.graphics.*;
import lcsb.mapviewer.model.map.InconsistentModelException;
import lcsb.mapviewer.model.map.layout.graphics.Layer;
import lcsb.mapviewer.model.map.model.Model;
......@@ -79,16 +83,80 @@ public class ConvertController extends BaseController {
public @ResponseBody ResponseEntity<byte[]> convertInputToImage(
@PathVariable(value = "fromFormat") String fromFormat,
@PathVariable(value = "toFormat") String toFormat,
@RequestBody byte[] body)
throws IOException, QueryException, SBMLException, InvalidInputDataExecption, ConverterException,
DrawingException {
ByteArrayOutputStream os = convertController.converToImage(fromFormat, toFormat, body);
@RequestBody byte[] body) throws QueryException, IOException, DrawingException {
Model original = null;
try {
original = getModelParserByNameOrClass(fromFormat).createModel(createConvertParams(body));
} catch (InvalidInputDataExecption | ConverterException e) {
throw new QueryException("Problem with input file", e);
}
try {
new CreateHierarchyCommand(original, 10, 1024).execute();
for (Layer layer : original.getLayers()) {
layer.setVisible(false);
}
} catch (Exception e) {
logger.error("Problem with creating pathways", e);
}
AbstractImageGenerator generator = getImageGenerator(toFormat, createImageParams(original));
ByteArrayOutputStream os = new ByteArrayOutputStream();
generator.saveToOutputStream(os);
return ResponseEntity.ok().contentLength(os.size())
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.header("Content-Disposition", "attachment; filename=model" + toFormat)
.body(os.toByteArray());
}
private AbstractImageGenerator getImageGenerator(String extOrClass, AbstractImageGenerator.Params params)
throws QueryException {
for (Pair<String, Class<? extends AbstractImageGenerator>> element : new ImageGenerators()
.getAvailableImageGenerators()) {
try {
Class<? extends AbstractImageGenerator> clazz = element.getRight();
Constructor<?> ctor = clazz.getConstructor(AbstractImageGenerator.Params.class);
AbstractImageGenerator generator = (AbstractImageGenerator) ctor.newInstance(params);
if (extOrClass.equals(clazz.getCanonicalName()) || extOrClass.equals(generator.getFileExtension())) {
return generator;
}
} catch (NoSuchMethodException | java.lang.SecurityException | InstantiationException
| IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
logger.error("Creation of image generator class for '" + element.getLeft() + "' failed.");
throw new QueryException("Issue with obtaining image generator for extension " + extOrClass + ".");
}
}
throw new QueryException("Image generator for extension " + extOrClass + " not available.");
}
private AbstractImageGenerator.Params createImageParams(Model model) {
double padding = 5.0;
double maxDim = 10000.0;
double w = model.getWidth();
double h = model.getHeight();
double targetHeight = Math.min(h, maxDim);
double targetWidth = Math.min(w, maxDim);
double scale = targetWidth / w;
if (h * scale > targetHeight) {
scale = targetHeight / h;
}
double wScaled = w * scale;
double hScaled = h * scale;
return new AbstractImageGenerator.Params().model(model).width(wScaled + padding).height(hScaled + padding)
.scale(1 / scale)
.x(0);
}
@PostMapping(value = "/merge/{fromFormat}:{toFormat}")
public @ResponseBody ResponseEntity<byte[]> mergeFilesToFile(
@PathVariable(value = "fromFormat") String fromFormat,
......
package lcsb.mapviewer.api.convert;
import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.sbml.jsbml.SBMLException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import lcsb.mapviewer.api.BaseRestImpl;
import lcsb.mapviewer.commands.*;
import lcsb.mapviewer.commands.CommandExecutionException;
import lcsb.mapviewer.commands.MergeCommand;
import lcsb.mapviewer.common.Pair;
import lcsb.mapviewer.common.exception.InvalidStateException;
import lcsb.mapviewer.converter.*;
import lcsb.mapviewer.converter.ConverterParams;
import lcsb.mapviewer.converter.graphics.*;
import lcsb.mapviewer.converter.graphics.AbstractImageGenerator;
import lcsb.mapviewer.converter.graphics.ImageGenerators;
import lcsb.mapviewer.model.map.InconsistentModelException;
import lcsb.mapviewer.model.map.layout.graphics.Layer;
import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.services.QueryException;
......@@ -30,39 +25,6 @@ import lcsb.mapviewer.services.QueryException;
@Service
public class ConvertRestImpl extends BaseRestImpl {
private Logger logger = LogManager.getLogger();
public ByteArrayOutputStream converToImage(String fromFormat, String toFormat, byte[] input,
Double targetWidth, Double targetHeight)
throws InvalidInputDataExecption, SBMLException, IOException, ConverterException, DrawingException,
QueryException {
Model original = getModelParserByNameOrClass(fromFormat).createModel(createConvertParams(input));
try {
new CreateHierarchyCommand(original, 10, 1024).execute();
for (Layer layer : original.getLayers()) {
layer.setVisible(false);
}
} catch (Exception e) {
logger.error("Problem with creating pathways", e);
}
AbstractImageGenerator generator = getImageGenerator(toFormat,
createImageParams(original, targetWidth, targetHeight));
ByteArrayOutputStream os = new ByteArrayOutputStream();
generator.saveToOutputStream(os);
return os;
}
public ByteArrayOutputStream converToImage(String fromFormat, String toFormat, byte[] input)
throws IOException, DrawingException, QueryException {
try {
return converToImage(fromFormat, toFormat, input, 0.0, 0.0);
} catch (InvalidInputDataExecption | ConverterException e) {
throw new QueryException("Input file is invalid", e);
}
}
public byte[] mergeFiles(String fromFormat, String toFormat, byte[] input)
throws IOException, QueryException, CommandExecutionException {
try {
......@@ -181,62 +143,4 @@ public class ConvertRestImpl extends BaseRestImpl {
throw new QueryException("Unknown parser name: " + name);
}
private AbstractImageGenerator getImageGenerator(String extOrClass, AbstractImageGenerator.Params params)
throws QueryException {
for (Pair<String, Class<? extends AbstractImageGenerator>> element : new ImageGenerators()
.getAvailableImageGenerators()) {
try {
Class<? extends AbstractImageGenerator> clazz = element.getRight();
Constructor<?> ctor = clazz.getConstructor(AbstractImageGenerator.Params.class);
AbstractImageGenerator generator = (AbstractImageGenerator) ctor.newInstance(params);
if (extOrClass.equals(clazz.getCanonicalName()) || extOrClass.equals(generator.getFileExtension())) {
return generator;
}
} catch (NoSuchMethodException | java.lang.SecurityException | InstantiationException
| IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
logger.error("Creation of image generator class for '" + element.getLeft() + "' failed.");
throw new QueryException("Issue with obtaining image generator for extension " + extOrClass + ".");
}
}
throw new QueryException("Image generator for extension " + extOrClass + " not available.");
}
private AbstractImageGenerator.Params createImageParams(Model model, Double targetWidth, Double targetHeight) {
Double padding = 5.0;
Double maxDim = 10000.0;
Double w = model.getWidth();
Double h = model.getHeight();
if (targetHeight == 0)
targetHeight = Math.min(h, maxDim);
if (targetWidth == 0)
targetWidth = Math.min(w, maxDim);
Double scale = targetWidth / w;
if (h * scale > targetHeight) {
scale = targetHeight / h;
}
Double wScaled = w * scale;
Double hScaled = h * scale;
return new AbstractImageGenerator.Params().model(model).width(wScaled + padding).height(hScaled + padding)
.scale(1 / scale)
.x(0);
}
private ConverterParams createConvertParams(byte[] input) {
ConverterParams params = new ConverterParams();
InputStream is = new ByteArrayInputStream(input);
params.inputStream(is);
return params;
}
}
package lcsb.mapviewer.api.convert;
import static org.junit.Assert.*;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.stream.XMLStreamException;
import java.util.List;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.*;
import org.sbml.jsbml.SBMLException;
import org.springframework.beans.factory.annotation.Autowired;
import lcsb.mapviewer.api.RestTestFunctions;
import lcsb.mapviewer.converter.ConverterException;
import lcsb.mapviewer.converter.InvalidInputDataExecption;
import lcsb.mapviewer.converter.graphics.DrawingException;
import lcsb.mapviewer.model.map.InconsistentModelException;
import lcsb.mapviewer.services.QueryException;
public class ConvertRestImplTest extends RestTestFunctions {
......@@ -71,29 +62,4 @@ public class ConvertRestImplTest extends RestTestFunctions {
assertTrue(outputs.size() > 0);
}
@Test
public void testCelDesigner2Svg() throws Exception {
String content = readFile("testFiles/convert/sample-cd.xml");
String result = convertRestImpl.converToImage("CellDesigner_SBML", "svg", content.getBytes()).toString();
assertTrue(result.contains("<rect"));
}
@Test
public void testScaling() throws Exception {
String content = readFile("testFiles/convert/sample-cd.xml");
String result1 = convertRestImpl.converToImage("CellDesigner_SBML", "svg", content.getBytes(), 50.0, 50.0).toString();
String result2 = convertRestImpl.converToImage("CellDesigner_SBML", "svg", content.getBytes(), 100.0, 100.0)
.toString();
Pattern pattern = Pattern.compile(".*scale\\(([0-9]+\\.[0-9]+),([0-9]+\\.[0-9]+)\\).*");
Matcher matcher1 = pattern.matcher(result1);
Matcher matcher2 = pattern.matcher(result2);
assertTrue(matcher1.find());
assertTrue(matcher2.find());
assertFalse(matcher1.group(1).toString().equals(matcher2.group(1).toString()));
}
}
......@@ -385,4 +385,12 @@ public class ConvertControllerIntegrationTest extends ControllerIntegrationTest
test2All(content, "SBGN-ML");
}
@Test
public void testCelDesigner2Svg() throws Exception {
String content = readFile("src/test/resources/convert/sample-cd.xml");
String result = convertToImage(content, "CellDesigner_SBML", "svg");
assertTrue(result.contains("<rect"));
}
}
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