X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.db.layer0%2Fsrc%2Forg%2Fsimantics%2Fdb%2Flayer0%2Fbindings%2FResourceBinding.java;h=d623b439fc2917a051d3999412141b08ebc4b5d2;hp=18eca47a48bbf2c78359ad5c4276bd265a77527d;hb=refs%2Fchanges%2F38%2F238%2F2;hpb=24e2b34260f219f0d1644ca7a138894980e25b14 diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/bindings/ResourceBinding.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/bindings/ResourceBinding.java index 18eca47a4..d623b439f 100644 --- a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/bindings/ResourceBinding.java +++ b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/bindings/ResourceBinding.java @@ -1,167 +1,167 @@ -/******************************************************************************* - * Copyright (c) 2007, 2010 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.db.layer0.bindings; - -import org.simantics.databoard.Datatypes; -import org.simantics.databoard.binding.LongBinding; -import org.simantics.databoard.binding.error.BindingException; -import org.simantics.databoard.type.Datatype; -import org.simantics.databoard.type.LongType; -import org.simantics.db.Resource; -import org.simantics.db.Session; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.layer0.internal.SimanticsInternal; -import org.simantics.db.service.SerialisationSupport; - -/** - * This class binds Resource to LongType - * - * @author toni.kalajainen - */ -public class ResourceBinding extends LongBinding { - - public static final Datatype RESOURCE_TYPE; - - //SerialisationSupport support; - - /** - * Create resource binding. If session is not provided, this binding cannot instantiate resources. - * - * @param session or null - */ - public ResourceBinding(Session session) { - super(Datatypes.LONG); -// if (session != null) { -// support = session.peekService(SerialisationSupport.class); -// } - } - - public ResourceBinding(SerialisationSupport serializationSupport) { - super(Datatypes.LONG); -// this.support = serializationSupport; - } - - /** - * @return current {@link SerialisationSupport} - * @throws BindingException - * if not able to return a current {@link SerialisationSupport} - */ - private SerialisationSupport getCurrentSupport() throws BindingException { - // FIXME: this is wrong but should be optimized if possible. -// if (support != null) -// return support; - - Session s = SimanticsInternal.peekSession(); - if ( s == null ) { - throw new BindingException("Cannot instantiate Resource without an alive database Session."); - } - SerialisationSupport support = s.peekService(SerialisationSupport.class); - if ( support == null ) { - throw new BindingException("Cannot instantiate Resource without an alive database Session."); - } - return support; - } - - @Override - public Object create(long value) throws BindingException { - if(value == 0) return null; - SerialisationSupport support = getCurrentSupport(); - try { - return support.getResource(value); - } catch (DatabaseException e) { - throw new BindingException(e); - } - } - - @Override - public Object create(Long value) throws BindingException { - SerialisationSupport support = getCurrentSupport(); - try { - return support.getResource(value.longValue()); - } catch (DatabaseException e) { - throw new BindingException(e); - } - } - - @Override - public Object create(Number value) throws BindingException { - SerialisationSupport support = getCurrentSupport(); - try { - return support.getResource(value.longValue()); - } catch (DatabaseException e) { - throw new BindingException(e); - } - } - - @Override - public Object create(String value) throws BindingException { - SerialisationSupport support = getCurrentSupport(); - try { - return support.getResource(Long.parseLong(value)); - } catch (NumberFormatException e) { - throw new BindingException(e); - } catch (DatabaseException e) { - throw new BindingException(e); - } - } - - @Override - public Long getValue(Object o) throws BindingException { - SerialisationSupport support = getCurrentSupport(); - // NOTE: r.getResourceId() is unsafe for this purpose, it will just return 0 if it fails, thus corrupting anything serialized with this method -// Resource r = (Resource) o; -// return r.getResourceId(); - try { - return support.getRandomAccessId((Resource)o); - } catch (DatabaseException e) { - throw new BindingException(e); - } - } - - @Override - public long getValue_(Object o) throws BindingException { - SerialisationSupport support = getCurrentSupport(); - // NOTE: r.getResourceId() is unsafe for this purpose, it will just return 0 if it fails, thus corrupting anything serialized with this method -// Resource r = (Resource) o; -// return r.getResourceId(); - try { - return support.getRandomAccessId((Resource)o); - } catch (DatabaseException e) { - throw new BindingException(e); - } - } - - @Override - public void setValue(Object obj, Number value) throws BindingException { - throw new BindingException("Resource is immutable"); - } - - @Override - public void setValue(Object obj, long value) throws BindingException { - throw new BindingException("Resource is immutable"); - } - - @Override - public boolean isImmutable() { - return true; - } - - @Override - public boolean isInstance(Object obj) { - return obj instanceof Resource; - } - - static { - RESOURCE_TYPE = new LongType(); - RESOURCE_TYPE.metadata.put("resource", "true"); - } -} +/******************************************************************************* + * Copyright (c) 2007, 2010 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.db.layer0.bindings; + +import org.simantics.databoard.Datatypes; +import org.simantics.databoard.binding.LongBinding; +import org.simantics.databoard.binding.error.BindingException; +import org.simantics.databoard.type.Datatype; +import org.simantics.databoard.type.LongType; +import org.simantics.db.Resource; +import org.simantics.db.Session; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.internal.SimanticsInternal; +import org.simantics.db.service.SerialisationSupport; + +/** + * This class binds Resource to LongType + * + * @author toni.kalajainen + */ +public class ResourceBinding extends LongBinding { + + public static final Datatype RESOURCE_TYPE; + + //SerialisationSupport support; + + /** + * Create resource binding. If session is not provided, this binding cannot instantiate resources. + * + * @param session or null + */ + public ResourceBinding(Session session) { + super(Datatypes.LONG); +// if (session != null) { +// support = session.peekService(SerialisationSupport.class); +// } + } + + public ResourceBinding(SerialisationSupport serializationSupport) { + super(Datatypes.LONG); +// this.support = serializationSupport; + } + + /** + * @return current {@link SerialisationSupport} + * @throws BindingException + * if not able to return a current {@link SerialisationSupport} + */ + private SerialisationSupport getCurrentSupport() throws BindingException { + // FIXME: this is wrong but should be optimized if possible. +// if (support != null) +// return support; + + Session s = SimanticsInternal.peekSession(); + if ( s == null ) { + throw new BindingException("Cannot instantiate Resource without an alive database Session."); + } + SerialisationSupport support = s.peekService(SerialisationSupport.class); + if ( support == null ) { + throw new BindingException("Cannot instantiate Resource without an alive database Session."); + } + return support; + } + + @Override + public Object create(long value) throws BindingException { + if(value == 0) return null; + SerialisationSupport support = getCurrentSupport(); + try { + return support.getResource(value); + } catch (DatabaseException e) { + throw new BindingException(e); + } + } + + @Override + public Object create(Long value) throws BindingException { + SerialisationSupport support = getCurrentSupport(); + try { + return support.getResource(value.longValue()); + } catch (DatabaseException e) { + throw new BindingException(e); + } + } + + @Override + public Object create(Number value) throws BindingException { + SerialisationSupport support = getCurrentSupport(); + try { + return support.getResource(value.longValue()); + } catch (DatabaseException e) { + throw new BindingException(e); + } + } + + @Override + public Object create(String value) throws BindingException { + SerialisationSupport support = getCurrentSupport(); + try { + return support.getResource(Long.parseLong(value)); + } catch (NumberFormatException e) { + throw new BindingException(e); + } catch (DatabaseException e) { + throw new BindingException(e); + } + } + + @Override + public Long getValue(Object o) throws BindingException { + SerialisationSupport support = getCurrentSupport(); + // NOTE: r.getResourceId() is unsafe for this purpose, it will just return 0 if it fails, thus corrupting anything serialized with this method +// Resource r = (Resource) o; +// return r.getResourceId(); + try { + return support.getRandomAccessId((Resource)o); + } catch (DatabaseException e) { + throw new BindingException(e); + } + } + + @Override + public long getValue_(Object o) throws BindingException { + SerialisationSupport support = getCurrentSupport(); + // NOTE: r.getResourceId() is unsafe for this purpose, it will just return 0 if it fails, thus corrupting anything serialized with this method +// Resource r = (Resource) o; +// return r.getResourceId(); + try { + return support.getRandomAccessId((Resource)o); + } catch (DatabaseException e) { + throw new BindingException(e); + } + } + + @Override + public void setValue(Object obj, Number value) throws BindingException { + throw new BindingException("Resource is immutable"); + } + + @Override + public void setValue(Object obj, long value) throws BindingException { + throw new BindingException("Resource is immutable"); + } + + @Override + public boolean isImmutable() { + return true; + } + + @Override + public boolean isInstance(Object obj) { + return obj instanceof Resource; + } + + static { + RESOURCE_TYPE = new LongType(); + RESOURCE_TYPE.metadata.put("resource", "true"); + } +}