Commit 6d0b2e08 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

ConverterConvtroller separated from service

parent 990b18fb
......@@ -4,8 +4,9 @@ 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;
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;
......@@ -18,6 +19,7 @@ import org.springframework.web.bind.annotation.*;
import lcsb.mapviewer.api.BaseController;
import lcsb.mapviewer.commands.CreateHierarchyCommand;
import lcsb.mapviewer.commands.MergeCommand;
import lcsb.mapviewer.common.MinervaLoggerAppender;
import lcsb.mapviewer.common.Pair;
import lcsb.mapviewer.common.exception.InvalidStateException;
......@@ -36,12 +38,10 @@ public class ConvertController extends BaseController {
private Logger logger = LogManager.getLogger();
private ConvertRestImpl convertController;
private List<Converter> modelConverters;
@Autowired
public ConvertController(ConvertRestImpl convertController, List<Converter> modelConverters) {
this.convertController = convertController;
public ConvertController(List<Converter> modelConverters) {
this.modelConverters = modelConverters;
}
......@@ -163,21 +163,101 @@ public class ConvertController extends BaseController {
@PathVariable(value = "toFormat") String toFormat,
@RequestBody byte[] body)
throws Exception {
byte[] bytea = convertController.mergeFiles(fromFormat, toFormat, body);
return ResponseEntity.ok().contentLength(bytea.length)
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.header("Content-Disposition", "attachment; filename=model" + toFormat)
.body(bytea);
try {
ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(body));
ZipEntry zipEntry = zis.getNextEntry();
List<Pair<String, Model>> modelsWithFilename = new ArrayList<>();
while (zipEntry != null) {
String filename = zipEntry.getName();
if (!zipEntry.isDirectory() && filename.toLowerCase().startsWith("maps/")) {
Model model = getModelParserByNameOrClass(fromFormat)
.createModel(new ConverterParams().inputStream(convertZipInputStreamToInputStream(zis, zipEntry, "UTF-8"),
filename));
modelsWithFilename.add(new Pair<>(filename, model));
}
zipEntry = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
modelsWithFilename.sort(new Comparator<Pair<String, Model>>() {
@Override
public int compare(Pair<String, Model> o1, Pair<String, Model> o2) {
return o1.getLeft().compareTo(o2.getLeft());
}
});
List<Model> models = new ArrayList<>();
for (Pair<String, Model> pair : modelsWithFilename) {
models.add(pair.getRight());
}
Model result = new MergeCommand(models).execute();
Converter exporter = getModelParserByNameOrClass(toFormat);
byte[] bytea = IOUtils.toByteArray(exporter.model2InputStream(result));
return ResponseEntity.ok().contentLength(bytea.length)
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.header("Content-Disposition", "attachment; filename=model" + toFormat)
.body(bytea);
} catch (InvalidInputDataExecption | ConverterException | InconsistentModelException e) {
throw new QueryException("Input file is invalid", e);
}
}
private InputStream convertZipInputStreamToInputStream(ZipInputStream in, ZipEntry entry, String encoding)
throws IOException {
final int BUFFER = 2048;
int count = 0;
byte data[] = new byte[BUFFER];
ByteArrayOutputStream out = new ByteArrayOutputStream();
while ((count = in.read(data)) != -1) {
out.write(data, 0, count);
}
return new ByteArrayInputStream(out.toByteArray());
}
@RequestMapping(value = "/", method = { RequestMethod.GET, RequestMethod.POST })
public Map<String, ?> getInformation() {
return convertController.getInformation();
public Map<String, List<Map<String, List<String>>>> getInformation() {
Map<String, List<Map<String, List<String>>>> info = new LinkedHashMap<>();
List<Map<String, List<String>>> converters = new ArrayList<>();
for (Converter converter : modelConverters) {
List<String> names = new ArrayList<>();
names.add(removeWhiteSpaces(converter.getCommonName()));
names.add(converter.getClass().getCanonicalName());
Map<String, List<String>> names_item = new LinkedHashMap<>();
names_item.put("available_names", names);
converters.add(names_item);
}
info.put("inputs", converters);
info.put("outputs", converters);
return info;
}
@RequestMapping(value = "/image/", method = { RequestMethod.GET, RequestMethod.POST })
public Map<String, ?> getInformationImage() {
return convertController.getInformationImage();
Map<String, List<Map<String, List<String>>>> info = getInformation();
List<Map<String, List<String>>> generators = new ArrayList<>();
ImageGenerators igs = new ImageGenerators();
for (Pair<String, Class<? extends AbstractImageGenerator>> generator : igs.getAvailableImageGenerators()) {
List<String> names = new ArrayList<>();
names.add(igs.getExtension(generator.getRight()));
names.add(generator.getRight().getCanonicalName());
Map<String, List<String>> names_item = new LinkedHashMap<>();
names_item.put("available_names", names);
generators.add(names_item);
}
info.remove("outputs");
info.put("outputs", generators);
return info;
}
private ConverterParams createConvertParams(byte[] input) {
......
package lcsb.mapviewer.api.convert;
import java.io.*;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import lcsb.mapviewer.api.BaseRestImpl;
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.graphics.AbstractImageGenerator;
import lcsb.mapviewer.converter.graphics.ImageGenerators;
import lcsb.mapviewer.model.map.InconsistentModelException;
import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.services.QueryException;
@Transactional
@Service
public class ConvertRestImpl extends BaseRestImpl {
public byte[] mergeFiles(String fromFormat, String toFormat, byte[] input)
throws IOException, QueryException, CommandExecutionException {
try {
ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(input));
ZipEntry zipEntry = zis.getNextEntry();
List<Pair<String, Model>> modelsWithFilename = new ArrayList<>();
while (zipEntry != null) {
String filename = zipEntry.getName();
if (!zipEntry.isDirectory() && filename.toLowerCase().startsWith("maps/")) {
Model model = getModelParserByNameOrClass(fromFormat)
.createModel(new ConverterParams().inputStream(convertZipInputStreamToInputStream(zis, zipEntry, "UTF-8"),
filename));
modelsWithFilename.add(new Pair<>(filename, model));
}
zipEntry = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
modelsWithFilename.sort(new Comparator<Pair<String, Model>>() {
@Override
public int compare(Pair<String, Model> o1, Pair<String, Model> o2) {
return o1.getLeft().compareTo(o2.getLeft());
}
});
List<Model> models = new ArrayList<>();
for (Pair<String, Model> pair : modelsWithFilename) {
models.add(pair.getRight());
}
Model result = new MergeCommand(models).execute();
Converter exporter = getModelParserByNameOrClass(toFormat);
return IOUtils.toByteArray(exporter.model2InputStream(result));
} catch (InvalidInputDataExecption | ConverterException | InconsistentModelException e) {
throw new QueryException("Input file is invalid", e);
}
}
private InputStream convertZipInputStreamToInputStream(ZipInputStream in, ZipEntry entry, String encoding)
throws IOException {
final int BUFFER = 2048;
int count = 0;
byte data[] = new byte[BUFFER];
ByteArrayOutputStream out = new ByteArrayOutputStream();
while ((count = in.read(data)) != -1) {
out.write(data, 0, count);
}
return new ByteArrayInputStream(out.toByteArray());
}
public Map<String, List<Map<String, List<String>>>> getInformation() {
Map<String, List<Map<String, List<String>>>> info = new LinkedHashMap<>();
List<Map<String, List<String>>> converters = new ArrayList<>();
for (Converter converter : getModelConverters()) {
List<String> names = new ArrayList<>();
names.add(removeWhiteSpaces(converter.getCommonName()));
names.add(converter.getClass().getCanonicalName());
Map<String, List<String>> names_item = new LinkedHashMap<>();
names_item.put("available_names", names);
converters.add(names_item);
}
info.put("inputs", converters);
info.put("outputs", converters);
return info;
}
public Map<String, List<Map<String, List<String>>>> getInformationImage() {
Map<String, List<Map<String, List<String>>>> info = getInformation();
List<Map<String, List<String>>> generators = new ArrayList<>();
ImageGenerators igs = new ImageGenerators();
for (Pair<String, Class<? extends AbstractImageGenerator>> generator : igs.getAvailableImageGenerators()) {
List<String> names = new ArrayList<>();
names.add(igs.getExtension(generator.getRight()));
names.add(generator.getRight().getCanonicalName());
Map<String, List<String>> names_item = new LinkedHashMap<>();
names_item.put("available_names", names);
generators.add(names_item);
}
info.remove("outputs");
info.put("outputs", generators);
return info;
}
private Converter getModelParserByNameOrClass(String id) throws QueryException {
try {
return getModelParserByName(id);
} catch (QueryException e) {
return getModelParser(id);
}
}
private String removeWhiteSpaces(String str) {
return str.replace(' ', '_');
}
private Converter getModelParserByName(String name) throws QueryException {
for (Converter converter : getModelConverters()) {
if (removeWhiteSpaces(converter.getCommonName()).equals(name)) {
try {
return converter.getClass().newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new InvalidStateException(e);
}
}
}
throw new QueryException("Unknown parser name: " + name);
}
}
......@@ -4,7 +4,6 @@ import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import lcsb.mapviewer.api.convert.AllConvertTests;
import lcsb.mapviewer.api.files.AllFileTests;
import lcsb.mapviewer.api.genomics.AllGenomicsTests;
import lcsb.mapviewer.api.mesh.AllMeshTests;
......@@ -13,7 +12,6 @@ import lcsb.mapviewer.api.users.AllUserTests;
@RunWith(Suite.class)
@SuiteClasses({
AllConvertTests.class,
AllFileTests.class,
AllGenomicsTests.class,
AllMeshTests.class,
......
package lcsb.mapviewer.api.convert;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({ ConvertRestImplTest.class })
public class AllConvertTests {
}
package lcsb.mapviewer.api.convert;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
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.springframework.beans.factory.annotation.Autowired;
import lcsb.mapviewer.api.RestTestFunctions;
public class ConvertRestImplTest extends RestTestFunctions {
@Autowired
public ConvertRestImpl convertRestImpl;
@SuppressWarnings("unused")
private Logger logger = LogManager.getLogger();
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@SuppressWarnings("unchecked")
@Test
public void testGetInformation() throws Exception {
Map<String, ?> result = convertRestImpl.getInformationImage();
assertTrue(result.keySet().size() == 2);
assertNotNull(result.get("inputs"));
assertNotNull(result.get("outputs"));
List<Object> inputs = (List<Object>) result.get("inputs");
List<Object> outputs = (List<Object>) result.get("outputs");
assertTrue(inputs.size() > 0);
assertTrue(outputs.size() > 0);
}
@SuppressWarnings("unchecked")
@Test
public void testGetInformationImage() throws Exception {
Map<String, ?> result = convertRestImpl.getInformationImage();
assertTrue(result.keySet().size() == 2);
assertNotNull(result.get("inputs"));
assertNotNull(result.get("outputs"));
List<Object> inputs = (List<Object>) result.get("inputs");
List<Object> outputs = (List<Object>) result.get("outputs");
assertTrue(inputs.size() > 0);
assertTrue(outputs.size() > 0);
}
}
......@@ -72,9 +72,8 @@ public class UserRestImplTest extends RestTestFunctions {
private Map<String, Object> deserialize(Map<String, Object> data)
throws JsonParseException, JsonMappingException, IOException {
String body = new Gson().toJson(data);
ObjectMapper mapper = new ObjectMapper();
ObjectNode result = mapper.readValue(body, ObjectNode.class);
ObjectNode result = mapper.readValue(mapper.writeValueAsString(data), ObjectNode.class);
return mapper.convertValue(result, Map.class);
}
......
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<sbgn xmlns="http://sbgn.org/libsbgn/0.2">
<map language="process description">
<glyph id="nwtN_f034af08-f2b3-4912-aaa8-58b4f55f43b9" class="macromolecule">
<label text="dffgdf"/>
<bbox x="707" y="292" w="60" h="30"/>
</glyph>
</map>
</sbgn>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<sbml xmlns="http://www.sbml.org/sbml/level2/version4" xmlns:celldesigner="http://www.sbml.org/2001/ns/celldesigner" level="2" version="4">
<model metaid="untitled" id="untitled">
<annotation>
<celldesigner:extension>
<celldesigner:modelVersion>4.0</celldesigner:modelVersion>
<celldesigner:modelDisplay sizeX="1308" sizeY="600"/>
<celldesigner:listOfIncludedSpecies>
<celldesigner:species id="s14" name="s14">
<celldesigner:notes>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title/>
</head>
<body/>
</html>
</celldesigner:notes>
<celldesigner:annotation>
<celldesigner:complexSpecies>s12</celldesigner:complexSpecies>
<celldesigner:speciesIdentity>
<celldesigner:class>PROTEIN</celldesigner:class>
<celldesigner:proteinReference>pr5</celldesigner:proteinReference>
</celldesigner:speciesIdentity>
</celldesigner:annotation>
</celldesigner:species>
</celldesigner:listOfIncludedSpecies>
<celldesigner:listOfCompartmentAliases/>
<celldesigner:listOfComplexSpeciesAliases>
<celldesigner:complexSpeciesAlias id="csa1" species="s12">
<celldesigner:activity>inactive</celldesigner:activity>
<celldesigner:bounds x="271.0" y="207.0" w="101.0" h="164.0"/>
<celldesigner:font size="12"/>
<celldesigner:view state="usual"/>
<celldesigner:backupSize w="0.0" h="0.0"/>
<celldesigner:backupView state="none"/>
<celldesigner:usualView>
<celldesigner:innerPosition x="0.0" y="0.0"/>
<celldesigner:boxSize width="101.0" height="164.0"/>
<celldesigner:singleLine width="2.0"/>
<celldesigner:paint color="fff7f7f7" scheme="Color"/>
</celldesigner:usualView>
<celldesigner:briefView>
<celldesigner:innerPosition x="0.0" y="0.0"/>
<celldesigner:boxSize width="80.0" height="60.0"/>
<celldesigner:singleLine width="2.0"/>
<celldesigner:paint color="fff7f7f7" scheme="Color"/>
</celldesigner:briefView>
<celldesigner:info state="empty" angle="-1.5707963267948966"/>
</celldesigner:complexSpeciesAlias>
</celldesigner:listOfComplexSpeciesAliases>
<celldesigner:listOfSpeciesAliases>
<celldesigner:speciesAlias id="sa1" species="s1">
<celldesigner:activity>inactive</celldesigner:activity>
<celldesigner:bounds x="12.0" y="6.0" w="80.0" h="40.0"/>
<celldesigner:font size="12"/>
<celldesigner:view state="usual"/>
<celldesigner:usualView>
<celldesigner:innerPosition x="0.0" y="0.0"/>
<celldesigner:boxSize width="80.0" height="40.0"/>
<celldesigner:singleLine width="1.0"/>
<celldesigner:paint color="ffffffff" scheme="Color"/>
</celldesigner:usualView>
<celldesigner:briefView>
<celldesigner:innerPosition x="0.0" y="0.0"/>
<celldesigner:boxSize width="80.0" height="60.0"/>
<celldesigner:singleLine width="1.0"/>
<celldesigner:paint color="3fff0000" scheme="Color"/>
</celldesigner:briefView>
<celldesigner:info state="empty" angle="-1.5707963267948966"/>
</celldesigner:speciesAlias>
<celldesigner:speciesAlias id="sa2" species="s2">
<celldesigner:activity>inactive</celldesigner:activity>
<celldesigner:bounds x="165.0" y="43.0" w="80.0" h="50.0"/>
<celldesigner:font size="12"/>
<celldesigner:view state="usual"/>
<celldesigner:usualView>
<celldesigner:innerPosition x="0.0" y="0.0"/>
<celldesigner:boxSize width="80.0" height="50.0"/>
<celldesigner:singleLine width="1.0"/>
<celldesigner:paint color="ffffffcc" scheme="Color"/>
</celldesigner:usualView>
<celldesigner:briefView>
<celldesigner:innerPosition x="0.0" y="0.0"/>
<celldesigner:boxSize width="80.0" height="60.0"/>
<celldesigner:singleLine width="1.0"/>
<celldesigner:paint color="3fff0000" scheme="Color"/>
</celldesigner:briefView>
<celldesigner:info state="empty" angle="-1.5707963267948966"/>
</celldesigner:speciesAlias>
<celldesigner:speciesAlias id="sa3" species="s3">
<celldesigner:activity>inactive</celldesigner:activity>
<celldesigner:bounds x="289.0" y="39.0" w="80.0" h="40.0"/>
<celldesigner:font size="12"/>
<celldesigner:view state="usual"/>
<celldesigner:usualView>
<celldesigner:innerPosition x="0.0" y="0.0"/>
<celldesigner:boxSize width="80.0" height="40.0"/>
<celldesigner:singleLine width="1.0"/>
<celldesigner:paint color="ffccffff" scheme="Color"/>
</celldesigner:usualView>
<celldesigner:briefView>
<celldesigner:innerPosition x="0.0" y="0.0"/>
<celldesigner:boxSize width="80.0" height="60.0"/>
<celldesigner:singleLine width="1.0"/>
<celldesigner:paint color="3fff0000" scheme="Color"/>
</celldesigner:briefView>
<celldesigner:info state="empty" angle="-1.5707963267948966"/>
</celldesigner:speciesAlias>
<celldesigner:speciesAlias id="sa4" species="s4">
<celldesigner:activity>inactive</celldesigner:activity>
<celldesigner:bounds x="419.0" y="45.0" w="80.0" h="40.0"/>
<celldesigner:font size="12"/>
<celldesigner:view state="usual"/>
<celldesigner:usualView>
<celldesigner:innerPosition x="0.0" y="0.0"/>
<celldesigner:boxSize width="80.0" height="40.0"/>
<celldesigner:singleLine width="1.0"/>
<celldesigner:paint color="ffffcccc" scheme="Color"/>
</celldesigner:usualView>
<celldesigner:briefView>
<celldesigner:innerPosition x="0.0" y="0.0"/>
<celldesigner:boxSize width="80.0" height="60.0"/>
<celldesigner:singleLine width="1.0"/>
<celldesigner:paint color="3fff0000" scheme="Color"/>
</celldesigner:briefView>
<celldesigner:info state="empty" angle="-1.5707963267948966"/>
</celldesigner:speciesAlias>
<celldesigner:speciesAlias id="sa5" species="s5">
<celldesigner:activity>inactive</celldesigner:activity>
<celldesigner:bounds x="0.0" y="118.5" w="90.0" h="25.0"/>
<celldesigner:font size="12"/>
<celldesigner:view state="usual"/>
<celldesigner:usualView>
<celldesigner:innerPosition x="0.0" y="0.0"/>
<celldesigner:boxSize width="90.0" height="25.0"/>
<celldesigner:singleLine width="1.0"/>
<celldesigner:paint color="ff66ff66" scheme="Color"/>
</celldesigner:usualView>
<celldesigner:briefView>
<celldesigner:innerPosition x="0.0" y="0.0"/>
<celldesigner:boxSize width="80.0" height="60.0"/>
<celldesigner:singleLine width="1.0"/>
<celldesigner:paint color="3fff0000" scheme="Color"/>
</celldesigner:briefView>
<celldesigner:info state="empty" angle="-1.5707963267948966"/>
</celldesigner:speciesAlias>
<celldesigner:speciesAlias id="sa6" species="s6">
<celldesigner:activity>inactive</celldesigner:activity>
<celldesigner:bounds x="101.0" y="129.5" w="90.0" h="25.0"/>
<celldesigner:font size="12"/>
<celldesigner:view state="usual"/>
<celldesigner:usualView>
<celldesigner:innerPosition x="0.0" y="0.0"/>
<celldesigner:boxSize width="90.0" height="25.0"/>
<celldesigner:singleLine width="1.0"/>
<celldesigner:paint color="ffff6666" scheme="Color"/>
</celldesigner:usualView>
<celldesigner:briefView>
<celldesigner:innerPosition x="0.0" y="0.0"/>
<celldesigner:boxSize width="80.0" height="60.0"/>
<celldesigner:singleLine width="1.0"/>
<celldesigner:paint color="3fff0000" scheme="Color"/>
</celldesigner:briefView>
<celldesigner:info state="empty" angle="-1.5707963267948966"/>