1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.ui.workbench;
14 import java.lang.ref.Reference;
15 import java.lang.ref.SoftReference;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.List;
21 import org.simantics.Simantics;
22 import org.simantics.db.RequestProcessor;
23 import org.simantics.db.Resource;
24 import org.simantics.db.Session;
25 import org.simantics.db.common.ResourceArray;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.db.exception.RuntimeDatabaseException;
28 import org.simantics.db.service.LifecycleSupport;
29 import org.simantics.db.service.SerialisationSupport;
32 * @author Tuukka Lehtonen
34 final class ResourceInputs {
36 public static <T> Reference<T> makeReference(T t) {
37 return new SoftReference<T>(t);
40 public static Resource resolveResource(RequestProcessor processor, String resourceId) throws DatabaseException {
42 SerialisationSupport ss = processor.getService(SerialisationSupport.class);
43 return ss.getResource(Long.parseLong(resourceId));
44 } catch (NumberFormatException e) {
45 throw new DatabaseException(e);
49 public static ResourceArray makeResourceArray(RequestProcessor processor, Collection<String> resourceIds) throws DatabaseException {
51 SerialisationSupport ss = processor.getService(SerialisationSupport.class);
52 List<Resource> rs = new ArrayList<Resource>();
53 for (String id : resourceIds)
54 rs.add(ss.getResource(Long.parseLong(id)));
55 return new ResourceArray(rs);
56 } catch (NumberFormatException e) {
57 throw new DatabaseException(e);
61 public static String getRandomAccessId(Resource r) {
63 SerialisationSupport support = getSession().getService(SerialisationSupport.class);
64 return String.valueOf( support.getRandomAccessId(r) );
65 } catch (DatabaseException e) {
66 throw new RuntimeDatabaseException(e);
70 public static List<String> getRandomAccessIds(ResourceArray ra) {
72 List<String> randoms = new ArrayList<String>();
73 SerialisationSupport support = getSession().getService(SerialisationSupport.class);
74 for (Resource r : ra.resources)
75 randoms.add(String.valueOf(support.getRandomAccessId(r)));
76 return Collections.unmodifiableList(randoms);
77 } catch (DatabaseException e) {
78 throw new RuntimeDatabaseException(e);
83 * @return a graph instance if it exists and has not yet been disposed,
84 * <code>null</code> otherwise
86 public static Session getSession() {
87 Session s = Simantics.getSession();
88 if (s.getService(LifecycleSupport.class).isClosed())
89 throw new IllegalStateException("database session is closed");
94 * @return a graph instance if it exists and has not yet been disposed,
95 * <code>null</code> otherwise
97 public static Session peekSession() {
98 Session s = Simantics.peekSession();
101 if (s.getService(LifecycleSupport.class).isClosed())
102 throw new IllegalStateException("database session is closed");