]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.route/src/org/simantics/district/route/internal/RoutePersistenceJob.java
Initial version of the district network Routes view.
[simantics/district.git] / org.simantics.district.route / src / org / simantics / district / route / internal / RoutePersistenceJob.java
diff --git a/org.simantics.district.route/src/org/simantics/district/route/internal/RoutePersistenceJob.java b/org.simantics.district.route/src/org/simantics/district/route/internal/RoutePersistenceJob.java
new file mode 100644 (file)
index 0000000..037b590
--- /dev/null
@@ -0,0 +1,94 @@
+package org.simantics.district.route.internal;
+
+import java.util.concurrent.CompletableFuture;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.simantics.DatabaseJob;
+import org.simantics.Simantics;
+import org.simantics.db.Resource;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.request.PossibleModel;
+import org.simantics.db.request.Write;
+import org.simantics.district.route.Route;
+import org.simantics.district.route.RouteEvent;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * @author Tuukka Lehtonen
+ * @since 6.09
+ */
+public class RoutePersistenceJob extends DatabaseJob {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(RoutePersistenceJob.class);
+
+    private RouteImpl route;
+    private int eventType;
+    private CompletableFuture<Route> future;
+
+    public RoutePersistenceJob(RouteImpl route, int eventType, CompletableFuture<Route> future) {
+        super("Route Persistence");
+        this.route = route;
+        this.eventType = eventType;
+        this.future = future;
+        setUser(false);
+        setSystem(true);
+    }
+
+    @Override
+    protected IStatus run(IProgressMonitor monitor) {
+        try {
+            Simantics.getSession().syncRequest((Write) graph -> persist(graph));
+            future.complete(route);
+        } catch (DatabaseException e) {
+            LOGGER.error("Failed to persist route {}", route, e);
+            future.completeExceptionally(e);
+        }
+        return Status.OK_STATUS;
+    }
+
+    private void persist(WriteGraph graph) throws DatabaseException {
+        switch (eventType) {
+            case RouteEvent.TYPE_ROUTE_DISCARDING:
+                remove(graph);
+                break;
+
+            case RouteEvent.TYPE_ROUTE_PERSISTING:
+                write(graph);
+                break;
+        }
+    }
+
+    private void write(WriteGraph graph) throws DatabaseException {
+        graph.markUndoPoint();
+        Resource r = route.backend();
+        Resource me = route.modelEntity();
+        if (r == null) {
+            Resource model = graph.sync(new PossibleModel(me));
+            route.backend(
+                    RoutePersistence.createRoute(graph,
+                            model,
+                            route.getName(),
+                            RoutePersistence.toResources(route.waypoints()))
+                    );
+        } else {
+            RoutePersistence.updateRoute(graph,
+                    r,
+                    route.getName(),
+                    RoutePersistence.toResources(route.waypoints())
+            );
+        }
+    }
+
+    private void remove(WriteGraph graph) throws DatabaseException {
+        graph.markUndoPoint();
+        Resource r = route.backend();
+        if (r != null) {
+            RoutePersistence.removeRoute(graph, r);
+        }
+    }
+
+}