]> gerrit.simantics Code Review - simantics/district.git/commitdiff
Add a method for unpersisting individual routes into RouteService
authorReino Ruusu <reino.ruusu@semantum.fi>
Fri, 26 Jul 2019 12:23:17 +0000 (15:23 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Sat, 31 Aug 2019 20:10:12 +0000 (23:10 +0300)
gitlab #57

Change-Id: I5d486ca7e9362a8685ebe2c04579f001339ecde3

org.simantics.district.route/src/org/simantics/district/route/RouteService.java
org.simantics.district.route/src/org/simantics/district/route/internal/RoutePersistence.java
org.simantics.district.route/src/org/simantics/district/route/internal/RouteServiceImpl.java

index 5d3f63dc2499d471bdb40978a7a537b3b053bebc..ce8c2848635adff2a65455289f42ed5de4668766 100644 (file)
@@ -30,4 +30,6 @@ public interface RouteService {
 
     List<Router> routers();
 
+    Route readRoute(Object backendRouteObject);
+
 }
index fceb6612cb521fd2d81e3d5759046c4daee2f120..4bc0c6f0cc4fa3d73d93a5fae9eda5ab25796147 100644 (file)
@@ -23,6 +23,7 @@ import org.simantics.db.common.utils.NameUtils;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.QueryIndexUtils;
 import org.simantics.db.layer0.request.PossibleActiveModel;
+import org.simantics.db.layer0.request.PossibleModel;
 import org.simantics.db.layer0.util.RemoverUtil;
 import org.simantics.district.network.ontology.DistrictNetworkResource;
 import org.simantics.district.route.Waypoint;
@@ -135,16 +136,33 @@ public class RoutePersistence {
         List<RouteImpl> routes = new ArrayList<>();
 
         for (Resource route : graph.syncRequest(new ObjectsWithType(rf, L0.ConsistsOf, RR.Route))) {
-            RouteImpl ri = new RouteImpl(graph.getRelatedValue(route, L0.HasLabel, Bindings.STRING))
-                    .backend(route)
-                    .modelEntity(model)
-                    .waypoints(toWaypoints(graph, ListUtils.toList(graph, route)));
-            routes.add(ri);
+            routes.add(getRoute(graph, model, route));
         }
 
         return routes;
     }
 
+    public static RouteImpl getRoute(ReadGraph graph, Resource model, Resource route) throws DatabaseException {
+        Layer0 L0 = Layer0.getInstance(graph);
+        RouteImpl ri = new RouteImpl(graph.getRelatedValue(route, L0.HasLabel, Bindings.STRING))
+                .backend(route)
+                .modelEntity(model)
+                .waypoints(toWaypoints(graph, ListUtils.toList(graph, route)));
+        return ri;
+    }
+
+    public static class RouteRequest extends ResourceRead<RouteImpl> {
+        public RouteRequest(Resource resource) {
+            super(resource);
+        }
+
+        @Override
+        public RouteImpl perform(ReadGraph graph) throws DatabaseException {
+            Resource model = graph.syncRequest(new PossibleModel(resource));
+            return model != null ? getRoute(graph, model, resource) : null;
+        }
+    }
+
     public static class ModelRoutesRequest extends ResourceRead<List<RouteImpl>> {
         public ModelRoutesRequest(Resource model) {
             super(model);
@@ -154,7 +172,7 @@ public class RoutePersistence {
             return findRoutes(graph, resource);
         }
     }
-
+    
     public static class ActiveModelRoutesRequest extends UniqueRead<List<RouteImpl>> {
         @Override
         public List<RouteImpl> perform(ReadGraph graph) throws DatabaseException {
index 6817ea3282843de3f70531c69d47889c71786bcc..4690ac46b3dafa87727096273365ef17afc3c646 100644 (file)
@@ -13,6 +13,7 @@ import org.simantics.db.Resource;
 import org.simantics.db.Session;
 import org.simantics.db.common.procedure.adapter.DisposableListener;
 import org.simantics.db.common.procedure.adapter.DisposableSyncListener;
+import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.request.PossibleActiveModel;
 import org.simantics.db.management.ISessionContext;
 import org.simantics.db.management.ISessionContextChangedListener;
@@ -201,4 +202,17 @@ public class RouteServiceImpl implements RouteService, ISessionContextChangedLis
         });
     }
 
+    @Override
+    public Route readRoute(Object backendRouteObject) {
+        if (!(backendRouteObject instanceof Resource))
+            return null;
+        
+        try {
+            return Simantics.getSession().syncRequest(new RoutePersistence.RouteRequest((Resource)backendRouteObject));
+        } catch (DatabaseException e) {
+            LOGGER.error("Failed to read district route object for " + backendRouteObject, e);
+            return null;
+        }
+    }
+
 }