-package fi.vtt.simantics.procore.internal;\r
-\r
-import java.lang.reflect.InvocationTargetException;\r
-import java.lang.reflect.Method;\r
-import java.util.ArrayList;\r
-import java.util.Collection;\r
-import java.util.Map;\r
-\r
-import org.simantics.db.ChangeSet;\r
-import org.simantics.db.ChangeSetIdentifier;\r
-import org.simantics.db.Metadata;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Session;\r
-import org.simantics.db.common.request.UniqueRead;\r
-import org.simantics.db.common.utils.Logger;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.exception.InternalException;\r
-import org.simantics.db.service.ManagementSupport;\r
-import org.simantics.db.service.XSupport;\r
-\r
-public class ManagementSupportImpl implements ManagementSupport {\r
-\r
- final private SessionImplSocket session;\r
-\r
- ManagementSupportImpl(SessionImplSocket session) {\r
- this.session = session;\r
- }\r
- @Override\r
- public Collection<ChangeSet> fetchChangeSets(final ReadGraph graph, final long min, final long max) throws DatabaseException {\r
- if (min < 1 || min > max)\r
- throw new IllegalArgumentException("Illegal range: min=" + min + " max=" + max);\r
- return graph.sync(new UniqueRead<Collection<ChangeSet>>() {\r
- @Override\r
- public Collection<ChangeSet> perform(ReadGraph graph) throws DatabaseException {\r
- int size = (int)(max - min + 1);\r
- ClientChangesImpl csi = new ClientChangesImpl(session);\r
- SynchronizeContext context = new SynchronizeContext(session, csi, size, true);\r
- boolean failed = session.graphSession.getChangeSets(min, max, context);\r
- if (failed)\r
- throw new InternalException("Trouble with server execution.");\r
- final boolean undo = false;\r
- if (!context.isOk(undo)) // this is a blocking operation\r
- throw new InternalException("Trouble with server reply.");\r
- return context.getChangeSets();\r
- }\r
- });\r
- }\r
- @Override\r
- public Collection<ChangeSetIdentifier> getChangeSetIdentifiers(long from, long to) throws DatabaseException {\r
- return session.graphSession.getChangeSets(from, to, session.state.getHeadRevisionId());\r
- }\r
- @Override\r
- @Deprecated\r
- public Collection<ChangeSetIdentifier> getChangeSets(long from, long to) throws DatabaseException {\r
- return session.graphSession.getChangeSets(from, to, session.state.getHeadRevisionId());\r
- }\r
- @Override\r
- public <T> Collection<T> getMetadata(ReadGraph graph, long from, long to, Class<? extends Metadata> dataClass)\r
- throws DatabaseException {\r
- return this.getMetadata(from, to, dataClass);\r
- }\r
-\r
- @SuppressWarnings("unchecked")\r
- @Override\r
- public <T> Collection<T> getMetadata(long from, long to, Class<? extends Metadata> dataClass)\r
- throws DatabaseException {\r
- ArrayList<T> results = new ArrayList<T>();\r
- try {\r
- Method m = dataClass.getMethod("deserialise", Session.class, byte[].class);\r
- Collection<ChangeSetIdentifier> css = getChangeSets(from, to);\r
- for(ChangeSetIdentifier cs : css) {\r
- Map<String, byte[]> md = cs.getMetadata();\r
- if (null != md) {\r
- byte[] result = md.get(dataClass.getName());\r
- if (null != result) {\r
- try {\r
- Object value = m.invoke(null, session, result);\r
- results.add((T)value);\r
- } catch (SecurityException e) {\r
- Logger.defaultLogError(e);\r
- } catch (IllegalArgumentException e) {\r
- Logger.defaultLogError(e);\r
- } catch (IllegalAccessException e) {\r
- Logger.defaultLogError(e);\r
- } catch (InvocationTargetException e) {\r
- Logger.defaultLogError(e);\r
- }\r
- }\r
- }\r
- }\r
- } catch (SecurityException e) {\r
- Logger.defaultLogError(e);\r
- } catch (NoSuchMethodException e) {\r
- Logger.defaultLogError(e);\r
- } catch (IllegalArgumentException e) {\r
- Logger.defaultLogError(e);\r
- }\r
- return results;\r
- }\r
-\r
- @Override\r
- public void dumpRevision(long lastChangeSetId)\r
- throws DatabaseException {\r
- XSupport xs = session.getService(XSupport.class);\r
- String s = xs.execute("dumpRevision " + lastChangeSetId);\r
- long outChangeSetId = Long.parseLong(s);\r
- if (lastChangeSetId > 0 && outChangeSetId != lastChangeSetId)\r
- throw new DatabaseException("Failed to dump revision=" + lastChangeSetId + ":\n" + s);\r
- }\r
-\r
- @Override\r
- public void dumpChangeSets(long lastChangeSetId)\r
- throws DatabaseException {\r
- XSupport xs = session.getService(XSupport.class);\r
- String s = xs.execute("dumpChangeSets " + lastChangeSetId);\r
- long outChangeSetId = Long.parseLong(s);\r
- if (lastChangeSetId > 0 && outChangeSetId != lastChangeSetId)\r
- throw new DatabaseException("Failed to dump revision=" + lastChangeSetId + ":\n" + s);\r
- }\r
-\r
- @Override\r
- public long getHeadRevisionId()\r
- throws DatabaseException {\r
- return session.state.getHeadRevisionId();\r
- // Better to use this to match getChangeSets implementation above.\r
- // or not return session.graphSession.getLastChangeSetId();\r
- }\r
-\r
- @Override\r
- public long getFirstRevisionId() throws DatabaseException {\r
- return session.graphSession.dbSession.getDatabase().serverGetTailChangeSetId();\r
- }\r
-\r
- @Override\r
- public void subscribe(ChangeSetListener changeSetListener) {\r
- session.graphSession.addChangeSetListener(changeSetListener);\r
- }\r
- @Override\r
- public void cancel(ChangeSetListener changeSetlistener) {\r
- session.graphSession.removeChangeSetListener(changeSetlistener);\r
- }\r
-}\r
+package fi.vtt.simantics.procore.internal;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Map;
+
+import org.simantics.db.ChangeSet;
+import org.simantics.db.ChangeSetIdentifier;
+import org.simantics.db.Metadata;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Session;
+import org.simantics.db.common.request.UniqueRead;
+import org.simantics.db.common.utils.Logger;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.exception.InternalException;
+import org.simantics.db.service.ManagementSupport;
+import org.simantics.db.service.XSupport;
+
+public class ManagementSupportImpl implements ManagementSupport {
+
+ final private SessionImplSocket session;
+
+ ManagementSupportImpl(SessionImplSocket session) {
+ this.session = session;
+ }
+ @Override
+ public Collection<ChangeSet> fetchChangeSets(final ReadGraph graph, final long min, final long max) throws DatabaseException {
+ if (min < 1 || min > max)
+ throw new IllegalArgumentException("Illegal range: min=" + min + " max=" + max);
+ return graph.sync(new UniqueRead<Collection<ChangeSet>>() {
+ @Override
+ public Collection<ChangeSet> perform(ReadGraph graph) throws DatabaseException {
+ int size = (int)(max - min + 1);
+ ClientChangesImpl csi = new ClientChangesImpl(session);
+ SynchronizeContext context = new SynchronizeContext(session, csi, size, true);
+ boolean failed = session.graphSession.getChangeSets(min, max, context);
+ if (failed)
+ throw new InternalException("Trouble with server execution.");
+ final boolean undo = false;
+ if (!context.isOk(undo)) // this is a blocking operation
+ throw new InternalException("Trouble with server reply.");
+ return context.getChangeSets();
+ }
+ });
+ }
+ @Override
+ public Collection<ChangeSetIdentifier> getChangeSetIdentifiers(long from, long to) throws DatabaseException {
+ return session.graphSession.getChangeSets(from, to, session.state.getHeadRevisionId());
+ }
+ @Override
+ @Deprecated
+ public Collection<ChangeSetIdentifier> getChangeSets(long from, long to) throws DatabaseException {
+ return session.graphSession.getChangeSets(from, to, session.state.getHeadRevisionId());
+ }
+ @Override
+ public <T> Collection<T> getMetadata(ReadGraph graph, long from, long to, Class<? extends Metadata> dataClass)
+ throws DatabaseException {
+ return this.getMetadata(from, to, dataClass);
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public <T> Collection<T> getMetadata(long from, long to, Class<? extends Metadata> dataClass)
+ throws DatabaseException {
+ ArrayList<T> results = new ArrayList<T>();
+ try {
+ Method m = dataClass.getMethod("deserialise", Session.class, byte[].class);
+ Collection<ChangeSetIdentifier> css = getChangeSets(from, to);
+ for(ChangeSetIdentifier cs : css) {
+ Map<String, byte[]> md = cs.getMetadata();
+ if (null != md) {
+ byte[] result = md.get(dataClass.getName());
+ if (null != result) {
+ try {
+ Object value = m.invoke(null, session, result);
+ results.add((T)value);
+ } catch (SecurityException e) {
+ Logger.defaultLogError(e);
+ } catch (IllegalArgumentException e) {
+ Logger.defaultLogError(e);
+ } catch (IllegalAccessException e) {
+ Logger.defaultLogError(e);
+ } catch (InvocationTargetException e) {
+ Logger.defaultLogError(e.getCause());
+ }
+ }
+ }
+ }
+ } catch (SecurityException e) {
+ Logger.defaultLogError(e);
+ } catch (NoSuchMethodException e) {
+ Logger.defaultLogError(e);
+ } catch (IllegalArgumentException e) {
+ Logger.defaultLogError(e);
+ }
+ return results;
+ }
+
+ @Override
+ public void dumpRevision(long lastChangeSetId)
+ throws DatabaseException {
+ XSupport xs = session.getService(XSupport.class);
+ String s = xs.execute("dumpRevision " + lastChangeSetId);
+ long outChangeSetId = Long.parseLong(s);
+ if (lastChangeSetId > 0 && outChangeSetId != lastChangeSetId)
+ throw new DatabaseException("Failed to dump revision=" + lastChangeSetId + ":\n" + s);
+ }
+
+ @Override
+ public void dumpChangeSets(long lastChangeSetId)
+ throws DatabaseException {
+ XSupport xs = session.getService(XSupport.class);
+ String s = xs.execute("dumpChangeSets " + lastChangeSetId);
+ long outChangeSetId = Long.parseLong(s);
+ if (lastChangeSetId > 0 && outChangeSetId != lastChangeSetId)
+ throw new DatabaseException("Failed to dump revision=" + lastChangeSetId + ":\n" + s);
+ }
+
+ @Override
+ public long getHeadRevisionId()
+ throws DatabaseException {
+ return session.state.getHeadRevisionId();
+ // Better to use this to match getChangeSets implementation above.
+ // or not return session.graphSession.getLastChangeSetId();
+ }
+
+ @Override
+ public long getFirstRevisionId() throws DatabaseException {
+ return session.graphSession.dbSession.getDatabase().serverGetTailChangeSetId();
+ }
+
+ @Override
+ public void subscribe(ChangeSetListener changeSetListener) {
+ session.graphSession.addChangeSetListener(changeSetListener);
+ }
+ @Override
+ public void cancel(ChangeSetListener changeSetlistener) {
+ session.graphSession.removeChangeSetListener(changeSetlistener);
+ }
+}