Commit 6122571d authored by Piotr Gawron's avatar Piotr Gawron
Browse files

Merge branch '1540-export-error' into 'devel_16.0.x'

detach fetched map so the changes won't be reflected in db

See merge request !1334
parents 0b1a1a4d 47f661c1
Pipeline #46513 failed with stage
in 64 minutes and 5 seconds
minerva (16.0.0~beta.2) stable; urgency=medium
* Bug fix: problem with export map with submaps fixed (#1540)
minerva (16.0.0~beta.1) stable; urgency=medium
* Backward incompatibility: layout parameter in minerva url is not supported
anymore
......
package lcsb.mapviewer.commands;
import java.util.*;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Objects;
import java.util.Queue;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import lcsb.mapviewer.common.exception.*;
import lcsb.mapviewer.common.exception.InvalidArgumentException;
import lcsb.mapviewer.common.exception.InvalidClassException;
import lcsb.mapviewer.common.exception.InvalidStateException;
import lcsb.mapviewer.model.Project;
import lcsb.mapviewer.model.graphics.PolylineData;
import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.compartment.Compartment;
import lcsb.mapviewer.model.map.kinetics.*;
import lcsb.mapviewer.model.map.kinetics.SbmlArgument;
import lcsb.mapviewer.model.map.kinetics.SbmlFunction;
import lcsb.mapviewer.model.map.kinetics.SbmlKinetics;
import lcsb.mapviewer.model.map.kinetics.SbmlParameter;
import lcsb.mapviewer.model.map.kinetics.SbmlUnit;
import lcsb.mapviewer.model.map.layout.graphics.Layer;
import lcsb.mapviewer.model.map.model.*;
import lcsb.mapviewer.model.map.reaction.*;
import lcsb.mapviewer.model.map.species.*;
import lcsb.mapviewer.model.map.model.Author;
import lcsb.mapviewer.model.map.model.ElementSubmodelConnection;
import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.model.map.model.ModelData;
import lcsb.mapviewer.model.map.model.ModelFullIndexed;
import lcsb.mapviewer.model.map.model.ModelSubmodelConnection;
import lcsb.mapviewer.model.map.reaction.AbstractNode;
import lcsb.mapviewer.model.map.reaction.NodeOperator;
import lcsb.mapviewer.model.map.reaction.Reaction;
import lcsb.mapviewer.model.map.reaction.ReactionNode;
import lcsb.mapviewer.model.map.species.Complex;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.model.map.species.Species;
/**
* Command that creates a new instance of the model with the same data.
......@@ -42,7 +65,7 @@ public class CopyCommand extends NewModelCommand {
* @return copy of the model
*/
public Model execute() {
Map<Model, Model> copies = new HashMap<>();
Map<ModelData, ModelData> copies = new HashMap<>();
Model model = getModel();
......@@ -59,16 +82,30 @@ public class CopyCommand extends NewModelCommand {
throw new InvalidStateException("Problem with copying submodel of a model");
}
Model result = createNotNestedCopy(model);
Queue<ModelData> children = new LinkedList<>();
children.add(model.getModelData());
copies.put(model, result);
while (!children.isEmpty()) {
ModelData child = children.poll();
if (copies.get(child) == null) {
for (ModelSubmodelConnection submodelConnection : model.getSubmodelConnections()) {
Model submodel = submodelConnection.getSubmodel().getModel();
Model submodelCopy = createNotNestedCopy(submodel);
copies.put(submodel, submodelCopy);
Model copy = createNotNestedCopy(child);
copies.put(child, copy.getModelData());
for (ModelSubmodelConnection submodelConnection : child.getSubmodels()) {
children.add(submodelConnection.getSubmodel());
}
for (Element element : child.getElements()) {
if (element.getSubmodel() != null) {
children.add(element.getSubmodel().getSubmodel());
}
}
}
}
for (Model modelCopy : copies.values()) {
Model result = copies.get(model.getModelData()).getModel();
for (ModelData modelCopy : copies.values()) {
try {
result.getProject().addModel(modelCopy);
assignModelCopies(modelCopy, copies);
......@@ -90,11 +127,11 @@ public class CopyCommand extends NewModelCommand {
* @throws InvalidModelException
* when there is inconsistency in data
*/
private void assignModelCopies(Model modelCopy, Map<Model, Model> copies) throws InvalidModelException {
for (ModelSubmodelConnection connection : modelCopy.getSubmodelConnections()) {
private void assignModelCopies(ModelData modelCopy, Map<ModelData, ModelData> copies) throws InvalidModelException {
for (ModelSubmodelConnection connection : modelCopy.getSubmodels()) {
// copy connection reference
Model original = connection.getSubmodel().getModel();
Model copy = copies.get(original);
ModelData original = connection.getSubmodel();
ModelData copy = copies.get(original);
if (copy == null) {
throw new InvalidModelException("Original model contain reference to model that wasn't copied");
}
......@@ -102,16 +139,12 @@ public class CopyCommand extends NewModelCommand {
// copy connection parent reference
if (connection.getParentModel() != null) {
original = connection.getParentModel().getModel();
if (original != null) {
copy = copies.get(original);
if (copy == null) {
if (!copies.values().contains(original)) {
throw new InvalidModelException("Original model contain reference to model that wasn't copied");
}
} else {
connection.setParentModel(copy);
}
original = connection.getParentModel();
copy = copies.get(original);
if (copy == null) {
throw new InvalidModelException("Original model contain reference to model that wasn't copied");
} else {
connection.setParentModel(copy);
}
}
}
......@@ -120,20 +153,21 @@ public class CopyCommand extends NewModelCommand {
if (alias.getSubmodel() != null) {
ElementSubmodelConnection connection = alias.getSubmodel();
// copy information about submodel
Model original = connection.getSubmodel().getModel();
Model copy = copies.get(original);
ModelData original = connection.getSubmodel();
ModelData copy = copies.get(original);
if (copy == null) {
throw new InvalidModelException("Original model contain reference to model that wasn't copied");
}
connection.setSubmodel(copy);
// copy information about original alias
// copy information about original element
if (connection.getFromElement() != null) {
connection.setFromElement(modelCopy.getElementByElementId(connection.getFromElement().getElementId()));
connection.setFromElement(modelCopy.getModel().getElementByElementId(connection.getFromElement().getElementId()));
}
// copy information about reference alias
// copy information about reference element
if (connection.getToElement() != null) {
connection.setToElement(modelCopy.getElementByElementId(connection.getToElement().getElementId()));
connection.setToElement(modelCopy.getModel().getElementByElementId(connection.getToElement().getElementId()));
}
}
}
......@@ -147,7 +181,7 @@ public class CopyCommand extends NewModelCommand {
* original model to copy
* @return copy of the model
*/
protected Model createNotNestedCopy(Model model) {
protected Model createNotNestedCopy(ModelData model) {
Model result = new ModelFullIndexed(null);
for (SbmlUnit unit : model.getUnits()) {
......@@ -216,7 +250,7 @@ public class CopyCommand extends NewModelCommand {
for (Reaction reaction : result.getReactions()) {
updateReactionReferences(reaction, result);
}
for (ModelSubmodelConnection connection : model.getSubmodelConnections()) {
for (ModelSubmodelConnection connection : model.getSubmodels()) {
result.addSubmodelConnection(connection.copy());
}
......@@ -247,7 +281,7 @@ public class CopyCommand extends NewModelCommand {
* @param original
* original model
*/
private void assignSimpleDataToCopy(Model copy, Model original) {
private void assignSimpleDataToCopy(Model copy, ModelData original) {
copy.setName(original.getName());
copy.setWidth(original.getWidth());
......@@ -373,10 +407,10 @@ public class CopyCommand extends NewModelCommand {
* @param originalModel
* original model from which alias copy was created
*/
private void updateElementReferences(Element element, Model model, Model originalModel) {
private void updateElementReferences(Element element, Model model, ModelData originalModel) {
if (element instanceof Compartment) {
Compartment compartment = (Compartment) element;
Compartment original = originalModel.getElementByElementId(element.getElementId());
Compartment original = originalModel.getModel().getElementByElementId(element.getElementId());
for (Element child : original.getElements()) {
compartment.addElement(model.getElementByElementId(child.getElementId()));
}
......
......@@ -178,6 +178,23 @@ public class CopyCommandTest extends CommandTestFunctions {
assertEquals(0, comparator.compare(model2, copy));
}
@Test
public void testCopySubmodelInElement() throws Exception {
Project project = new Project();
Model model = getModel();
Model model2 = getModel();
model2.setNotes("ASDSA");
Element alias = model.getElementByElementId("a_id");
alias.setSubmodel(new ElementSubmodelConnection(model2, SubmodelType.DOWNSTREAM_TARGETS, "name c"));
project.addModel(model);
project.addModel(model2);
Model copy = new CopyCommand(model).execute();
assertEquals(0, comparator.compare(model, copy));
}
@Test
public void testCopyModelWithSubmodels2() throws Exception {
Model model = getModel();
......
package lcsb.mapviewer.persist;
import java.util.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Observable;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
......@@ -231,4 +233,8 @@ public class DbUtils extends Observable {
public Dialect getDialect() {
return ((SessionFactoryImpl) sessionFactory).getDialect();
}
public void detach(Object entity) {
getSessionForCurrentThread().evict(entity);
}
}
package lcsb.mapviewer.services.impl;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
......@@ -10,16 +15,24 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import lcsb.mapviewer.annotation.services.*;
import lcsb.mapviewer.annotation.services.MiriamConnector;
import lcsb.mapviewer.annotation.services.PubmedParser;
import lcsb.mapviewer.annotation.services.PubmedSearchException;
import lcsb.mapviewer.common.IProgressUpdater;
import lcsb.mapviewer.model.Article;
import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.MiriamType;
import lcsb.mapviewer.model.map.compartment.Compartment;
import lcsb.mapviewer.model.map.kinetics.SbmlParameter;
import lcsb.mapviewer.model.map.kinetics.SbmlUnit;
import lcsb.mapviewer.model.map.model.*;
import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.model.map.model.ModelData;
import lcsb.mapviewer.model.map.model.ModelFullIndexed;
import lcsb.mapviewer.model.map.reaction.Reaction;
import lcsb.mapviewer.model.map.species.*;
import lcsb.mapviewer.model.map.species.Complex;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.model.map.species.Species;
import lcsb.mapviewer.persist.DbUtils;
import lcsb.mapviewer.persist.dao.ProjectDao;
import lcsb.mapviewer.persist.dao.map.ModelDao;
import lcsb.mapviewer.persist.dao.map.kinetics.SbmlParameterDao;
......@@ -63,19 +76,23 @@ public class ModelService implements IModelService {
private SbmlParameterDao sbmlParameterDao;
private SbmlUnitDao sbmlUnitDao;
private DbUtils dbUtils;
@Autowired
public ModelService(ModelDao modelDao,
ProjectDao projectDao,
PubmedParser backend,
MiriamConnector miriamConnector,
SbmlParameterDao sbmlParameterDao,
SbmlUnitDao sbmlUnitDao) {
SbmlUnitDao sbmlUnitDao,
DbUtils dbUtils) {
this.modelDao = modelDao;
this.projectDao = projectDao;
this.backend = backend;
this.miriamConnector = miriamConnector;
this.sbmlParameterDao = sbmlParameterDao;
this.sbmlUnitDao = sbmlUnitDao;
this.dbUtils = dbUtils;
}
@Override
......@@ -210,11 +227,18 @@ public class ModelService implements IModelService {
Hibernate.initialize(map.getFunctions());
Hibernate.initialize(map.getUnits());
Hibernate.initialize(map.getLayers());
map.getSubmodels().clear();
map.getParentModels().clear();
for (Element element : map.getElements()) {
if (element instanceof Complex) {
((Complex) element).setElemets(new ArrayList<>());
}
if (element instanceof Compartment) {
((Compartment) element).setElements(new HashSet<>());
}
element.setSubmodel(null);
}
for (Element element : map.getElements()) {
if (element instanceof Species) {
......@@ -222,7 +246,14 @@ public class ModelService implements IModelService {
((Species) element).getComplex().addSpecies((Species) element);
}
}
if (element.getCompartment()!=null) {
element.getCompartment().addElement(element);
}
}
for (Element element : map.getElements()) {
dbUtils.detach(element);
}
dbUtils.detach(map);
return new ModelFullIndexed(map);
}
......
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