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