1 package fi.vtt.simantics.procore.internal;
3 import java.lang.reflect.InvocationTargetException;
4 import java.lang.reflect.Method;
5 import java.util.ArrayList;
6 import java.util.Collection;
9 import org.simantics.db.ChangeSet;
10 import org.simantics.db.ChangeSetIdentifier;
11 import org.simantics.db.Metadata;
12 import org.simantics.db.ReadGraph;
13 import org.simantics.db.Session;
14 import org.simantics.db.common.request.UniqueRead;
15 import org.simantics.db.common.utils.Logger;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.exception.InternalException;
18 import org.simantics.db.service.ManagementSupport;
19 import org.simantics.db.service.XSupport;
21 public class ManagementSupportImpl implements ManagementSupport {
23 final private SessionImplSocket session;
25 ManagementSupportImpl(SessionImplSocket session) {
26 this.session = session;
29 public Collection<ChangeSet> fetchChangeSets(final ReadGraph graph, final long min, final long max) throws DatabaseException {
30 if (min < 1 || min > max)
31 throw new IllegalArgumentException("Illegal range: min=" + min + " max=" + max);
32 return graph.sync(new UniqueRead<Collection<ChangeSet>>() {
34 public Collection<ChangeSet> perform(ReadGraph graph) throws DatabaseException {
35 int size = (int)(max - min + 1);
36 ClientChangesImpl csi = new ClientChangesImpl(session);
37 SynchronizeContext context = new SynchronizeContext(session, csi, size, true);
38 boolean failed = session.graphSession.getChangeSets(min, max, context);
40 throw new InternalException("Trouble with server execution.");
41 final boolean undo = false;
42 if (!context.isOk(undo)) // this is a blocking operation
43 throw new InternalException("Trouble with server reply.");
44 return context.getChangeSets();
49 public Collection<ChangeSetIdentifier> getChangeSetIdentifiers(long from, long to) throws DatabaseException {
50 return session.graphSession.getChangeSets(from, to, session.state.getHeadRevisionId());
54 public Collection<ChangeSetIdentifier> getChangeSets(long from, long to) throws DatabaseException {
55 return session.graphSession.getChangeSets(from, to, session.state.getHeadRevisionId());
58 public <T> Collection<T> getMetadata(ReadGraph graph, long from, long to, Class<? extends Metadata> dataClass)
59 throws DatabaseException {
60 return this.getMetadata(from, to, dataClass);
63 @SuppressWarnings("unchecked")
65 public <T> Collection<T> getMetadata(long from, long to, Class<? extends Metadata> dataClass)
66 throws DatabaseException {
67 ArrayList<T> results = new ArrayList<T>();
69 Method m = dataClass.getMethod("deserialise", Session.class, byte[].class);
70 Collection<ChangeSetIdentifier> css = getChangeSets(from, to);
71 for(ChangeSetIdentifier cs : css) {
72 Map<String, byte[]> md = cs.getMetadata();
74 byte[] result = md.get(dataClass.getName());
77 Object value = m.invoke(null, session, result);
78 results.add((T)value);
79 } catch (SecurityException e) {
80 Logger.defaultLogError(e);
81 } catch (IllegalArgumentException e) {
82 Logger.defaultLogError(e);
83 } catch (IllegalAccessException e) {
84 Logger.defaultLogError(e);
85 } catch (InvocationTargetException e) {
86 Logger.defaultLogError(e.getCause());
91 } catch (SecurityException e) {
92 Logger.defaultLogError(e);
93 } catch (NoSuchMethodException e) {
94 Logger.defaultLogError(e);
95 } catch (IllegalArgumentException e) {
96 Logger.defaultLogError(e);
102 public void dumpRevision(long lastChangeSetId)
103 throws DatabaseException {
104 XSupport xs = session.getService(XSupport.class);
105 String s = xs.execute("dumpRevision " + lastChangeSetId);
106 long outChangeSetId = Long.parseLong(s);
107 if (lastChangeSetId > 0 && outChangeSetId != lastChangeSetId)
108 throw new DatabaseException("Failed to dump revision=" + lastChangeSetId + ":\n" + s);
112 public void dumpChangeSets(long lastChangeSetId)
113 throws DatabaseException {
114 XSupport xs = session.getService(XSupport.class);
115 String s = xs.execute("dumpChangeSets " + lastChangeSetId);
116 long outChangeSetId = Long.parseLong(s);
117 if (lastChangeSetId > 0 && outChangeSetId != lastChangeSetId)
118 throw new DatabaseException("Failed to dump revision=" + lastChangeSetId + ":\n" + s);
122 public long getHeadRevisionId()
123 throws DatabaseException {
124 return session.state.getHeadRevisionId();
125 // Better to use this to match getChangeSets implementation above.
126 // or not return session.graphSession.getLastChangeSetId();
130 public long getFirstRevisionId() throws DatabaseException {
131 return session.graphSession.dbSession.getDatabase().serverGetTailChangeSetId();
135 public void subscribe(ChangeSetListener changeSetListener) {
136 session.graphSession.addChangeSetListener(changeSetListener);
139 public void cancel(ChangeSetListener changeSetlistener) {
140 session.graphSession.removeChangeSetListener(changeSetlistener);