X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.ui%2Fsrc%2Forg%2Fsimantics%2Fui%2Fworkbench%2FResourceInputs.java;fp=bundles%2Forg.simantics.ui%2Fsrc%2Forg%2Fsimantics%2Fui%2Fworkbench%2FResourceInputs.java;h=01d937eb2269f03882834118bceb4dbdfbaf41fa;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.ui/src/org/simantics/ui/workbench/ResourceInputs.java b/bundles/org.simantics.ui/src/org/simantics/ui/workbench/ResourceInputs.java new file mode 100644 index 000000000..01d937eb2 --- /dev/null +++ b/bundles/org.simantics.ui/src/org/simantics/ui/workbench/ResourceInputs.java @@ -0,0 +1,106 @@ +/******************************************************************************* + * Copyright (c) 2012 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.ui.workbench; + +import java.lang.ref.Reference; +import java.lang.ref.SoftReference; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +import org.simantics.Simantics; +import org.simantics.db.RequestProcessor; +import org.simantics.db.Resource; +import org.simantics.db.Session; +import org.simantics.db.common.ResourceArray; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.exception.RuntimeDatabaseException; +import org.simantics.db.service.LifecycleSupport; +import org.simantics.db.service.SerialisationSupport; + +/** + * @author Tuukka Lehtonen + */ +final class ResourceInputs { + + public static Reference makeReference(T t) { + return new SoftReference(t); + } + + public static Resource resolveResource(RequestProcessor processor, String resourceId) throws DatabaseException { + try { + SerialisationSupport ss = processor.getService(SerialisationSupport.class); + return ss.getResource(Long.parseLong(resourceId)); + } catch (NumberFormatException e) { + throw new DatabaseException(e); + } + } + + public static ResourceArray makeResourceArray(RequestProcessor processor, Collection resourceIds) throws DatabaseException { + try { + SerialisationSupport ss = processor.getService(SerialisationSupport.class); + List rs = new ArrayList(); + for (String id : resourceIds) + rs.add(ss.getResource(Long.parseLong(id))); + return new ResourceArray(rs); + } catch (NumberFormatException e) { + throw new DatabaseException(e); + } + } + + public static String getRandomAccessId(Resource r) { + try { + SerialisationSupport support = getSession().getService(SerialisationSupport.class); + return String.valueOf( support.getRandomAccessId(r) ); + } catch (DatabaseException e) { + throw new RuntimeDatabaseException(e); + } + } + + public static List getRandomAccessIds(ResourceArray ra) { + try { + List randoms = new ArrayList(); + SerialisationSupport support = getSession().getService(SerialisationSupport.class); + for (Resource r : ra.resources) + randoms.add(String.valueOf(support.getRandomAccessId(r))); + return Collections.unmodifiableList(randoms); + } catch (DatabaseException e) { + throw new RuntimeDatabaseException(e); + } + } + + /** + * @return a graph instance if it exists and has not yet been disposed, + * null otherwise + */ + public static Session getSession() { + Session s = Simantics.getSession(); + if (s.getService(LifecycleSupport.class).isClosed()) + throw new IllegalStateException("database session is closed"); + return s; + } + + /** + * @return a graph instance if it exists and has not yet been disposed, + * null otherwise + */ + public static Session peekSession() { + Session s = Simantics.peekSession(); + if (s == null) + return null; + if (s.getService(LifecycleSupport.class).isClosed()) + throw new IllegalStateException("database session is closed"); + return s; + } + +}