]> gerrit.simantics Code Review - simantics/interop.git/commitdiff
Handling choice exporting properly by checking exported resource type
authorluukkainen <luukkainen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 27 Jan 2017 14:22:41 +0000 (14:22 +0000)
committerMarko Luukkainen <marko.luukkainen@vtt.fi>
Thu, 2 Feb 2017 12:01:35 +0000 (14:01 +0200)
refs #6985

git-svn-id: https://www.simantics.org/svn/simantics/interoperability/trunk@33439 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.xml.sax.base/src/org/simantics/xml/sax/base/XMLElementNamedChildWriterBase.java
org.simantics.xml.sax/src/org/simantics/xml/sax/ExporterGenerator.java

index 9c4ceb6b9a26a4963b143d31d6e4655762948ab2..8e6a678dab848182df40e720c7109caf9bc29570 100644 (file)
@@ -14,8 +14,9 @@ import org.simantics.db.exception.DatabaseException;
 \r
 public abstract class XMLElementNamedChildWriterBase implements XMLElementNamedChildWriter{\r
 \r
-       private Map<Resource,Class<? extends XMLElementWriter>> typeWriters = new HashMap<Resource, Class<? extends XMLElementWriter>>();\r
-       private Map<Resource,Class<? extends XMLElementWriter>> relationWriters = new HashMap<Resource, Class<? extends XMLElementWriter>>();\r
+       private Map<Resource,Class<? extends XMLElementWriter>> typeWriters = new HashMap<>();\r
+       private Map<Resource,Class<? extends XMLElementWriter>> relationWriters = new HashMap<>();\r
+       private Map<Resource,Map<Resource,Class<? extends XMLElementWriter>>> relationTypeWriters = new HashMap<>();\r
        private Set<Class<? extends XMLElementWriter>> writers = new HashSet<Class<? extends XMLElementWriter>>();\r
        \r
        \r
@@ -61,6 +62,12 @@ public abstract class XMLElementNamedChildWriterBase implements XMLElementNamedC
                if (writerClass != null)\r
                        return writerClass;\r
                if (child.statement != null) {\r
+                       Map<Resource,Class<? extends XMLElementWriter>> typeWriters = relationTypeWriters.get(child.statement.getPredicate());\r
+                       if (typeWriters != null) {\r
+                               writerClass = typeWriters.get(type);\r
+                               if (writerClass != null)\r
+                                       return writerClass;\r
+                       }\r
                        writerClass = relationWriters.get(child.statement.getPredicate());\r
                        if (writerClass != null)\r
                                return writerClass;\r
@@ -85,6 +92,16 @@ public abstract class XMLElementNamedChildWriterBase implements XMLElementNamedC
                relationWriters.put(relation, writer);\r
        }\r
        \r
+       public void addRelationTypeWriter(Resource relation, Resource type, Class<? extends XMLElementWriter> writer) {\r
+               Map<Resource,Class<? extends XMLElementWriter>> typeWriters = relationTypeWriters.get(relation);\r
+               if (typeWriters == null) {\r
+                       typeWriters = new HashMap<>();\r
+                       relationTypeWriters.put(relation, typeWriters);\r
+               }\r
+               typeWriters.put(type, writer);\r
+       }\r
+       \r
+       \r
        public void addWriter(Class<? extends XMLElementWriter> writer) {\r
                writers.add(writer);\r
        }\r
index 74538ea1c7716cc9fca8d2f94aa94d9159b628f3..243a1c2415b9f3476c270e9be542a8ff5c441f99 100644 (file)
@@ -11,7 +11,6 @@ import java.util.Map;
 import javax.xml.namespace.QName;\r
 \r
 import org.simantics.utils.datastructures.BijectionMap;\r
-import org.simantics.xml.sax.SchemaConversionBase.RefType;\r
 import org.simantics.xml.sax.SchemaObject.ObjectType;\r
 import org.simantics.xml.sax.configuration.AttributeComposition;\r
 import org.simantics.xml.sax.configuration.Configuration;\r
@@ -280,10 +279,15 @@ public class ExporterGenerator extends JavaGenerator{
        protected void createReferenceIndicator(SchemaObject parent, RefType referenceType, String refName, String objectName,\r
                        String primaryClassName, String secondaryClassName, boolean useElementList, boolean useOriginalList) {\r
                FileWriter fw = getWriter(parent);\r
+               if ("TextOrField".equals(refName))\r
+                       System.out.println();\r
                if (referenceType == RefType.Type) {\r
                // create internal class for handling the element and child attachment\r
                secondaryClassName = getName(parent) +"_" +objectName;\r
-               fw.writer.println("        addRelationWriter("+ontShort+ getName(parent)+"_has"+refName +", "+secondaryClassName+".class);");\r
+               if (objectName.equals(refName))\r
+               fw.writer.println("      addRelationWriter("+ontShort+ getName(parent)+"_has"+refName +", "+secondaryClassName+".class);");\r
+               else // FIXME : type name may not be correct\r
+               fw.writer.println("      addRelationTypeWriter("+ontShort+ getName(parent)+"_has"+refName +", " +ontShort+primaryClassName +", "+secondaryClassName+".class);");\r
                fw.delayedWriter2.println("   public static class " + secondaryClassName +" extends " + primaryClassName +"{");\r
                fw.delayedWriter2.println("      public "+ secondaryClassName +"(ReadGraph graph){");\r
                fw.delayedWriter2.println("         super(graph);");\r
@@ -304,17 +308,17 @@ public class ExporterGenerator extends JavaGenerator{
                if (useElementList) {\r
 \r
                // element type specific list\r
-               fw.delayedWriter.println("            {");\r
-               fw.delayedWriter.println("               org.simantics.db.Resource list = graph.getPossibleObject(instance.getResource(),"+ontShort+getName(parent)+"_has"+refName + "List);");\r
-               fw.delayedWriter.println("               if (list != null) {");\r
-               fw.delayedWriter.println("                  java.util.List<org.simantics.db.Resource> l = org.simantics.db.common.utils.ListUtils.toList(graph, list);");\r
-               fw.delayedWriter.println("                  result.addAll(l);");\r
-               fw.delayedWriter.println("               } else {");\r
-               fw.delayedWriter.println("                  result.addAll(graph.getObjects(instance.getResource(), "+ontShort+getName(parent)+"_has"+refName + "));");\r
-               fw.delayedWriter.println("               }");\r
-               fw.delayedWriter.println("            }");\r
+               fw.delayedWriter.println("      {");\r
+               fw.delayedWriter.println("         org.simantics.db.Resource list = graph.getPossibleObject(instance.getResource(),"+ontShort+getName(parent)+"_has"+refName + "List);");\r
+               fw.delayedWriter.println("         if (list != null) {");\r
+               fw.delayedWriter.println("            java.util.List<org.simantics.db.Resource> l = org.simantics.db.common.utils.ListUtils.toList(graph, list);");\r
+               fw.delayedWriter.println("            result.addAll(l);");\r
+               fw.delayedWriter.println("         } else {");\r
+               fw.delayedWriter.println("            result.addAll(graph.getObjects(instance.getResource(), "+ontShort+getName(parent)+"_has"+refName + "));");\r
+               fw.delayedWriter.println("         }");\r
+               fw.delayedWriter.println("      }");\r
                } else {\r
-               fw.delayedWriter.println("         result.addAll(graph.getObjects(instance.getResource(), "+ontShort+getName(parent)+"_has"+refName + "));");\r
+               fw.delayedWriter.println("      result.addAll(graph.getObjects(instance.getResource(), "+ontShort+getName(parent)+"_has"+refName + "));");\r
                }\r
 //                     if (useOriginalList(parent, indicator,element, reference, ref, refType)) {\r
 //                     // generic list\r
@@ -376,17 +380,6 @@ public class ExporterGenerator extends JavaGenerator{
                } else {\r
                fw.delayedWriter.println("         result.addAll(graph.getObjects(instance.getResource(), "+ontShort+getName(parent)+"_has"+refName + "));");\r
                }\r
-//             // element type specific list\r
-//             fw.delayedWriter.println("            {");\r
-//             fw.delayedWriter.println("               Resource list = graph.getPossibleObject(element.getData(),"+ontShort+getName(parent)+"_has"+ref + "List);");\r
-//             fw.delayedWriter.println("               if (list == null) {");\r
-//             fw.delayedWriter.println("                  list = org.simantics.db.common.utils.ListUtils.create(graph, java.util.Collections.singletonList(child.getData()));");\r
-//             fw.delayedWriter.println("                  graph.claim(element.getData(),"+ontShort+getName(parent)+"_has"+ref + "List,list);");\r
-//             fw.delayedWriter.println("               } else {");\r
-//             fw.delayedWriter.println("                  org.simantics.db.common.utils.ListUtils.insertBack(graph, list, java.util.Collections.singletonList(child.getData()));");\r
-//             fw.delayedWriter.println("               }");\r
-//             fw.delayedWriter.println("            }");\r
-//             }\r
 //             if (useOriginalList(parent, indicator,element, reference, ref, new QName(obj.getName()))) {\r
 //             // generic list\r
 //             fw.delayedWriter.println("            {");\r