1 /*******************************************************************************
\r
2 * Copyright (c) 2012 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.ui.workbench;
\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
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
32 * @author Tuukka Lehtonen
\r
34 final class ResourceInputs {
\r
36 public static <T> Reference<T> makeReference(T t) {
\r
37 return new SoftReference<T>(t);
\r
40 public static Resource resolveResource(RequestProcessor processor, String resourceId) throws DatabaseException {
\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
49 public static ResourceArray makeResourceArray(RequestProcessor processor, Collection<String> resourceIds) throws DatabaseException {
\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
61 public static String getRandomAccessId(Resource r) {
\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
70 public static List<String> getRandomAccessIds(ResourceArray ra) {
\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
83 * @return a graph instance if it exists and has not yet been disposed,
\r
84 * <code>null</code> otherwise
\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
94 * @return a graph instance if it exists and has not yet been disposed,
\r
95 * <code>null</code> otherwise
\r
97 public static Session peekSession() {
\r
98 Session s = Simantics.peekSession();
\r
101 if (s.getService(LifecycleSupport.class).isClosed())
\r
102 throw new IllegalStateException("database session is closed");
\r