--- /dev/null
+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