Commit 0966ac7a authored by Gradl, Tobias's avatar Gradl, Tobias
Browse files

998: Fix broken subtree export/import

Task-Url: https://pm.winseda.de/issues/998
parent fc23978c
......@@ -281,55 +281,10 @@ public class SchemaEditorController extends BaseMainEditorController implements
}
@RequestMapping(method = RequestMethod.GET, value = "/async/exportSubtree")
public @ResponseBody ModelActionPojo exportSubtree(@PathVariable String entityId, @RequestParam String elementId, Model model, Locale locale) {
Datamodel s = schemaService.findSchemaById(entityId);
Identifiable rootE = elementService.getElementSubtree(entityId, elementId);
Element expE;
if (Element.class.isAssignableFrom(rootE.getClass())) {
expE = (Element)rootE;
} else {
expE = new NonterminalImpl(s.getId(), "EXPORT_CONTAINER");
expE.setGrammars(new ArrayList<Grammar>());
GrammarImpl expG;
if (GrammarImpl.class.isAssignableFrom(rootE.getClass())) {
expG = (GrammarImpl)rootE;
} else {
expG = new GrammarImpl(entityId, "EXPORT_CONTAINER");
expG.setFunctions(new ArrayList<Function>());
FunctionImpl expF;
if (FunctionImpl.class.isAssignableFrom(rootE.getClass())) {
expF = (FunctionImpl)rootE;
} else {
return null;
}
expG.getFunctions().add(expF);
}
expE.getGrammars().add(expG);
}
DatamodelContainer sp = new DatamodelContainer();
sp.setModel(schemaService.cloneSchemaForSubtree(s, expE));
sp.setRoot(expE);
ChangeSet ch = schemaService.getLatestChangeSetForEntity(s.getId());
if (ch!=null) {
s.setVersionId(ch.getId());
}
List<ModelElement> relevantGrammarsI = IdentifiableServiceImpl.extractAllByTypes(expE, IdentifiableServiceImpl.getGrammarClasses());
if (relevantGrammarsI!=null && relevantGrammarsI.size()>0) {
List<Grammar> relevantGrammars = new ArrayList<Grammar>(relevantGrammarsI.size());
for (ModelElement g : relevantGrammarsI) {
if (!relevantGrammars.contains(g)) {
relevantGrammars.add((Grammar)g);
}
}
sp.setGrammars(grammarService.serializeGrammarSources(relevantGrammars));
}
public @ResponseBody ModelActionPojo exportSubtree(@PathVariable String entityId, @RequestParam String elementId, Model model, HttpServletRequest request, Locale locale) {
AuthPojo auth = authInfoHelper.getAuth(request);
ModelActionPojo result = new ModelActionPojo(true);
result.setPojo(sp);
result.setPojo(datamodelExporter.exportDatamodelSubtree(entityId, elementId, auth));
return result;
}
......
......@@ -3,6 +3,7 @@ package de.unibamberg.minf.dme.exporter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
......@@ -11,12 +12,21 @@ import de.unibamberg.minf.dme.model.RightsContainer;
import de.unibamberg.minf.dme.model.base.Element;
import de.unibamberg.minf.dme.model.base.Function;
import de.unibamberg.minf.dme.model.base.Grammar;
import de.unibamberg.minf.dme.model.base.Identifiable;
import de.unibamberg.minf.dme.model.base.Label;
import de.unibamberg.minf.dme.model.base.ModelElement;
import de.unibamberg.minf.dme.model.base.Nonterminal;
import de.unibamberg.minf.dme.model.datamodel.DatamodelImpl;
import de.unibamberg.minf.dme.model.datamodel.NonterminalImpl;
import de.unibamberg.minf.dme.model.datamodel.base.Datamodel;
import de.unibamberg.minf.dme.model.datamodel.base.DatamodelNature;
import de.unibamberg.minf.dme.model.function.FunctionImpl;
import de.unibamberg.minf.dme.model.grammar.GrammarImpl;
import de.unibamberg.minf.dme.model.reference.Reference;
import de.unibamberg.minf.dme.model.serialization.DatamodelContainer;
import de.unibamberg.minf.dme.model.serialization.DatamodelReferenceContainer;
import de.unibamberg.minf.dme.model.tracking.ChangeSet;
import de.unibamberg.minf.dme.service.IdentifiableServiceImpl;
import de.unibamberg.minf.dme.service.interfaces.ElementService;
import de.unibamberg.minf.dme.service.interfaces.FunctionService;
import de.unibamberg.minf.dme.service.interfaces.GrammarService;
......@@ -69,6 +79,102 @@ public class DatamodelExporter {
return result;
}
public DatamodelReferenceContainer exportDatamodelSubtree(String entityId, String exportElementId, AuthPojo auth) {
RightsContainer<Datamodel> s = datamodelService.findByIdAndAuth(entityId, auth);
if (s==null || s.getElement()==null) {
return null;
}
DatamodelReferenceContainer sp = new DatamodelReferenceContainer();
ChangeSet ch = datamodelService.getLatestChangeSetForEntity(s.getId());
if (ch!=null) {
s.getElement().setVersionId(ch.getId());
}
s.flush();
Identifiable rootE = elementService.getElementSubtree(entityId, exportElementId);
Element expE;
if (Element.class.isAssignableFrom(rootE.getClass())) {
expE = (Element)rootE;
} else {
expE = new NonterminalImpl(s.getId(), "EXPORT_CONTAINER");
expE.setGrammars(new ArrayList<Grammar>());
GrammarImpl expG;
if (GrammarImpl.class.isAssignableFrom(rootE.getClass())) {
expG = (GrammarImpl)rootE;
} else {
expG = new GrammarImpl(entityId, "EXPORT_CONTAINER");
expG.setFunctions(new ArrayList<Function>());
FunctionImpl expF;
if (FunctionImpl.class.isAssignableFrom(rootE.getClass())) {
expF = (FunctionImpl)rootE;
} else {
return null;
}
expG.getFunctions().add(expF);
}
expE.getGrammars().add(expG);
}
// Clone relevant parts of DatamodelNatures
sp.setModel(datamodelService.cloneSchemaForSubtree(s.getElement(), expE));
Reference rootR = new Reference(entityId);
Reference parentReference = referenceService.findReferenceBySchemaAndChildId(entityId, exportElementId);
for (String className : parentReference.getChildReferences().keySet()) {
boolean found = false;
for (Reference rChild : parentReference.getChildReferences().get(className)) {
if (rChild.getId().equals(exportElementId)) {
rootR.setChildReferences(new HashMap<String, Reference[]>());
rootR.getChildReferences().put(className, new Reference[] {rChild});
found = true;
break;
}
}
if (found) {
break;
}
}
sp.setRoot(rootR);
Map<String, ModelElement> elements = new HashMap<String, ModelElement>();
this.flattenElements(elements, expE);
sp.setElements(elements);
return sp;
}
private void flattenElements(Map<String, ModelElement> flattenedMap, ModelElement e) {
flattenedMap.put(e.getId(), e);
if (Nonterminal.class.isAssignableFrom(e.getClass())) {
this.flattenElements(flattenedMap, ((Nonterminal)e).getChildNonterminals());
this.flattenElements(flattenedMap, ((Nonterminal)e).getGrammars());
} else if (Label.class.isAssignableFrom(e.getClass())) {
this.flattenElements(flattenedMap, ((Label)e).getSubLabels());
this.flattenElements(flattenedMap, ((Label)e).getGrammars());
} else if (Grammar.class.isAssignableFrom(e.getClass())) {
this.flattenElements(flattenedMap, ((Grammar)e).getFunctions());
} else if (Function.class.isAssignableFrom(e.getClass())) {
this.flattenElements(flattenedMap, ((Function)e).getOutputElements());
}
}
private void flattenElements(Map<String, ModelElement> flattenedMap, List<? extends ModelElement> elements) {
if (elements==null || elements.size()==0) {
return;
}
for (ModelElement e : elements) {
this.flattenElements(flattenedMap, e);
}
}
public DatamodelReferenceContainer exportDatamodel(String entityId, AuthPojo auth) {
RightsContainer<Datamodel> s = datamodelService.findByIdAndAuth(entityId, auth);
if (s==null || s.getElement()==null) {
......
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