/******************************************************************************* * Copyright (c) 2007, 2013 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.objmap.graph.impl; import org.simantics.db.ReadGraph; import org.simantics.db.exception.DatabaseException; import org.simantics.db.procedure.SyncListener; import org.simantics.db.request.Read; import org.simantics.objmap.exceptions.MappingException; import org.simantics.objmap.forward.IForwardMapping; import org.simantics.objmap.graph.impl.Link; public class RangeUpdateRequest implements Read, SyncListener { Link link; /* * Note that this map uses a read request that it has got from caller and * not the one that is used in updateRange. This is intentional. */ IForwardMapping map; // map==null is used to flag that request is performed once Mapping mapping; // mapping==null is used as a flag the request disposed int counter; public RangeUpdateRequest(Link link, IForwardMapping map, Mapping mapping) { this.link = link; this.map = map; this.mapping = mapping; this.counter = 0; } @Override public Integer perform(ReadGraph g) throws DatabaseException { boolean changed = false; if (map != null) changed = link.type.checkChanges(g, map, link.domainElement, link.rangeElement); else if (mapping != null) changed = link.type.checkChanges(g, mapping, link.domainElement, link.rangeElement); return changed ? counter + 1 : counter; } @Override public void exception(ReadGraph graph, Throwable throwable) throws DatabaseException { if(throwable instanceof DatabaseException) throw (DatabaseException)throwable; else throw new MappingException(throwable); } @Override public void execute(ReadGraph graph, Integer result) throws DatabaseException { boolean changed = result != counter; counter = result; if (map != null) { if (changed) link.type.updateRange(graph, map, link.domainElement, link.rangeElement); map = null; } else if (mapping != null && changed) { mapping.domainModified(link); mapping = null; } } @Override public boolean isDisposed() { return mapping == null || link.removed || mapping.isDisposed(); } }