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

API allows to merge files

parent 7f20994d
Pipeline #24112 passed with stage
in 53 minutes and 11 seconds
minerva (15.0.0~beta.2) stable; urgency=medium
* Improvement: API allows to merge files (#1208)
* Small Improvement: SBO annotation support added (#1194)
* Small Improvement: additional annotation uri qualifiers added: hasProperty,
isPropertyOf (#1193)
......
......@@ -55,6 +55,19 @@ public class ConvertController extends BaseController {
.body(os.toByteArray());
}
@PostMapping(value = "/merge/{fromFormat}:{toFormat}")
public @ResponseBody ResponseEntity<byte[]> mergeFilesToFile(
@PathVariable(value = "fromFormat") String fromFormat,
@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);
}
@RequestMapping(value = "/", method = { RequestMethod.GET, RequestMethod.POST })
public Map<String, Object> getInformation() {
return convertController.getInformation();
......
......@@ -5,6 +5,8 @@ import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.StandardCharsets;
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;
......@@ -15,6 +17,8 @@ import org.springframework.transaction.annotation.Transactional;
import lcsb.mapviewer.api.BaseRestImpl;
import lcsb.mapviewer.api.QueryException;
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.*;
......@@ -60,6 +64,58 @@ 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, Object> getInformation() {
Map<String, Object> info = new LinkedHashMap<>();
......
package lcsb.mapviewer.web;
import static org.junit.Assert.assertEquals;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.nio.file.Files;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.RequestBuilder;
import lcsb.mapviewer.converter.ConverterParams;
import lcsb.mapviewer.converter.model.celldesigner.CellDesignerXmlParser;
import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.model.map.species.Element;
@RunWith(SpringJUnit4ClassRunner.class)
public class ConvertControllerIntegrationTest extends ControllerIntegrationTest {
......@@ -34,4 +44,37 @@ public class ConvertControllerIntegrationTest extends ControllerIntegrationTest
.andExpect(status().isBadRequest());
}
@Test
public void testMergeCellDesignerMaps() throws Exception {
byte[] body = Files.readAllBytes(new File("src/test/resources/convert/cd-maps.zip").toPath());
RequestBuilder request = post("/convert/merge/CellDesigner_SBML:CellDesigner_SBML")
.content(body);
byte[] content = mockMvc.perform(request)
.andExpect(status().is2xxSuccessful())
.andReturn().getResponse().getContentAsByteArray();
CellDesignerXmlParser parser = new CellDesignerXmlParser();
Model model = parser.createModel(new ConverterParams().inputStream(new ByteArrayInputStream(content)));
int s1 = 0;
int s2 = 0;
int s3 = 0;
for (Element element : model.getElements()) {
if (element.getName().equals("s1")) {
s1++;
}
if (element.getName().equals("s2")) {
s2++;
}
if (element.getName().equals("s3")) {
s3++;
}
}
assertEquals(1, s1);
assertEquals(2, s2);
assertEquals(1, s3);
}
}
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