--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2012 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.ui.workbench;\r
+\r
+import java.lang.ref.Reference;\r
+import java.lang.ref.SoftReference;\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.List;\r
+\r
+import org.simantics.Simantics;\r
+import org.simantics.db.RequestProcessor;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.common.ResourceArray;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.exception.RuntimeDatabaseException;\r
+import org.simantics.db.service.LifecycleSupport;\r
+import org.simantics.db.service.SerialisationSupport;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+final class ResourceInputs {\r
+\r
+ public static <T> Reference<T> makeReference(T t) {\r
+ return new SoftReference<T>(t);\r
+ }\r
+\r
+ public static Resource resolveResource(RequestProcessor processor, String resourceId) throws DatabaseException {\r
+ try {\r
+ SerialisationSupport ss = processor.getService(SerialisationSupport.class);\r
+ return ss.getResource(Long.parseLong(resourceId));\r
+ } catch (NumberFormatException e) {\r
+ throw new DatabaseException(e);\r
+ }\r
+ }\r
+\r
+ public static ResourceArray makeResourceArray(RequestProcessor processor, Collection<String> resourceIds) throws DatabaseException {\r
+ try {\r
+ SerialisationSupport ss = processor.getService(SerialisationSupport.class);\r
+ List<Resource> rs = new ArrayList<Resource>();\r
+ for (String id : resourceIds)\r
+ rs.add(ss.getResource(Long.parseLong(id)));\r
+ return new ResourceArray(rs);\r
+ } catch (NumberFormatException e) {\r
+ throw new DatabaseException(e);\r
+ }\r
+ }\r
+\r
+ public static String getRandomAccessId(Resource r) {\r
+ try {\r
+ SerialisationSupport support = getSession().getService(SerialisationSupport.class);\r
+ return String.valueOf( support.getRandomAccessId(r) );\r
+ } catch (DatabaseException e) {\r
+ throw new RuntimeDatabaseException(e);\r
+ }\r
+ }\r
+\r
+ public static List<String> getRandomAccessIds(ResourceArray ra) {\r
+ try {\r
+ List<String> randoms = new ArrayList<String>();\r
+ SerialisationSupport support = getSession().getService(SerialisationSupport.class);\r
+ for (Resource r : ra.resources)\r
+ randoms.add(String.valueOf(support.getRandomAccessId(r)));\r
+ return Collections.unmodifiableList(randoms);\r
+ } catch (DatabaseException e) {\r
+ throw new RuntimeDatabaseException(e);\r
+ }\r
+ }\r
+\r
+ /**\r
+ * @return a graph instance if it exists and has not yet been disposed,\r
+ * <code>null</code> otherwise\r
+ */\r
+ public static Session getSession() {\r
+ Session s = Simantics.getSession();\r
+ if (s.getService(LifecycleSupport.class).isClosed())\r
+ throw new IllegalStateException("database session is closed");\r
+ return s;\r
+ }\r
+\r
+ /**\r
+ * @return a graph instance if it exists and has not yet been disposed,\r
+ * <code>null</code> otherwise\r
+ */\r
+ public static Session peekSession() {\r
+ Session s = Simantics.peekSession();\r
+ if (s == null)\r
+ return null;\r
+ if (s.getService(LifecycleSupport.class).isClosed())\r
+ throw new IllegalStateException("database session is closed");\r
+ return s;\r
+ }\r
+\r
+}\r