]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/ResourceInputs.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / ResourceInputs.java
1 /*******************************************************************************\r
2  * Copyright (c) 2012 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.ui.workbench;\r
13 \r
14 import java.lang.ref.Reference;\r
15 import java.lang.ref.SoftReference;\r
16 import java.util.ArrayList;\r
17 import java.util.Collection;\r
18 import java.util.Collections;\r
19 import java.util.List;\r
20 \r
21 import org.simantics.Simantics;\r
22 import org.simantics.db.RequestProcessor;\r
23 import org.simantics.db.Resource;\r
24 import org.simantics.db.Session;\r
25 import org.simantics.db.common.ResourceArray;\r
26 import org.simantics.db.exception.DatabaseException;\r
27 import org.simantics.db.exception.RuntimeDatabaseException;\r
28 import org.simantics.db.service.LifecycleSupport;\r
29 import org.simantics.db.service.SerialisationSupport;\r
30 \r
31 /**\r
32  * @author Tuukka Lehtonen\r
33  */\r
34 final class ResourceInputs {\r
35 \r
36     public static <T> Reference<T> makeReference(T t) {\r
37         return new SoftReference<T>(t);\r
38     }\r
39 \r
40     public static Resource resolveResource(RequestProcessor processor, String resourceId) throws DatabaseException {\r
41         try {\r
42             SerialisationSupport ss = processor.getService(SerialisationSupport.class);\r
43             return ss.getResource(Long.parseLong(resourceId));\r
44         } catch (NumberFormatException e) {\r
45             throw new DatabaseException(e);\r
46         }\r
47     }\r
48 \r
49     public static ResourceArray makeResourceArray(RequestProcessor processor, Collection<String> resourceIds) throws DatabaseException {\r
50         try {\r
51             SerialisationSupport ss = processor.getService(SerialisationSupport.class);\r
52             List<Resource> rs = new ArrayList<Resource>();\r
53             for (String id : resourceIds)\r
54                 rs.add(ss.getResource(Long.parseLong(id)));\r
55             return new ResourceArray(rs);\r
56         } catch (NumberFormatException e) {\r
57             throw new DatabaseException(e);\r
58         }\r
59     }\r
60 \r
61     public static String getRandomAccessId(Resource r) {\r
62         try {\r
63             SerialisationSupport support = getSession().getService(SerialisationSupport.class);\r
64             return String.valueOf( support.getRandomAccessId(r) );\r
65         } catch (DatabaseException e) {\r
66             throw new RuntimeDatabaseException(e);\r
67         }\r
68     }\r
69 \r
70     public static List<String> getRandomAccessIds(ResourceArray ra) {\r
71         try {\r
72             List<String> randoms = new ArrayList<String>();\r
73             SerialisationSupport support = getSession().getService(SerialisationSupport.class);\r
74             for (Resource r : ra.resources)\r
75                 randoms.add(String.valueOf(support.getRandomAccessId(r)));\r
76             return Collections.unmodifiableList(randoms);\r
77         } catch (DatabaseException e) {\r
78             throw new RuntimeDatabaseException(e);\r
79         }\r
80     }\r
81 \r
82     /**\r
83      * @return a graph instance if it exists and has not yet been disposed,\r
84      *         <code>null</code> otherwise\r
85      */\r
86     public static Session getSession() {\r
87         Session s = Simantics.getSession();\r
88         if (s.getService(LifecycleSupport.class).isClosed())\r
89             throw new IllegalStateException("database session is closed");\r
90         return s;\r
91     }\r
92 \r
93     /**\r
94      * @return a graph instance if it exists and has not yet been disposed,\r
95      *         <code>null</code> otherwise\r
96      */\r
97     public static Session peekSession() {\r
98         Session s = Simantics.peekSession();\r
99         if (s == null)\r
100             return null;\r
101         if (s.getService(LifecycleSupport.class).isClosed())\r
102             throw new IllegalStateException("database session is closed");\r
103         return s;\r
104     }\r
105 \r
106 }\r