]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.network/src/org/simantics/district/network/techtype/requests/WriteTechTypeTable.java
TechTypeTable features
[simantics/district.git] / org.simantics.district.network / src / org / simantics / district / network / techtype / requests / WriteTechTypeTable.java
diff --git a/org.simantics.district.network/src/org/simantics/district/network/techtype/requests/WriteTechTypeTable.java b/org.simantics.district.network/src/org/simantics/district/network/techtype/requests/WriteTechTypeTable.java
new file mode 100644 (file)
index 0000000..1f85c5a
--- /dev/null
@@ -0,0 +1,45 @@
+package org.simantics.district.network.techtype.requests;
+
+import java.util.UUID;
+
+import org.simantics.Simantics;
+import org.simantics.db.Resource;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.procedure.adapter.TransientCacheListener;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.request.PossibleActiveModel;
+import org.simantics.district.network.ontology.DistrictNetworkResource;
+import org.simantics.layer0.Layer0;
+
+public final class WriteTechTypeTable extends WriteRequest {
+       private final String data;
+       private final Resource componentType;
+
+       public WriteTechTypeTable(Resource componentType, String data) {
+               this.data = data;
+               this.componentType = componentType;
+       }
+
+       @Override
+       public void perform(WriteGraph graph) throws DatabaseException {
+               Layer0 L0 = Layer0.getInstance(graph);
+               DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+
+               Resource model = graph.syncRequest(new PossibleActiveModel(Simantics.getProjectResource()));
+               if (model == null)
+                       throw new DatabaseException("No active model for storing tech type data");
+
+               Resource ttt = graph.syncRequest(new PossibleTechTypeTable(model, componentType), TransientCacheListener.instance());
+
+               if (ttt == null) {
+                       ttt = graph.newResource();
+                       graph.claim(ttt, L0.InstanceOf, DN.TechType_TechTypeTable);
+                       graph.claimLiteral(ttt, L0.HasName, UUID.randomUUID().toString());
+                       graph.claim(model, L0.ConsistsOf, ttt);
+                       graph.claim(ttt, DN.TechType_TechTypeTable_HasComponentType, componentType);
+               }
+
+               graph.claimLiteral(ttt, DN.TechType_TechTypeTable_HasData, data);
+       }
+}
\ No newline at end of file