]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ManagementSupportImpl.java
Merge commit 'c46f0ff'
[simantics/platform.git] / bundles / org.simantics.db.procore / src / fi / vtt / simantics / procore / internal / ManagementSupportImpl.java
1 package fi.vtt.simantics.procore.internal;\r
2 \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
8 \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
20 \r
21 public class ManagementSupportImpl implements ManagementSupport {\r
22 \r
23     final private SessionImplSocket session;\r
24 \r
25     ManagementSupportImpl(SessionImplSocket session) {\r
26         this.session = session;\r
27     }\r
28     @Override\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
33             @Override\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
39                 if (failed)\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
45             }\r
46         });\r
47     }\r
48     @Override\r
49     public Collection<ChangeSetIdentifier> getChangeSetIdentifiers(long from, long to) throws DatabaseException {\r
50         return session.graphSession.getChangeSets(from, to, session.state.getHeadRevisionId());\r
51     }\r
52     @Override\r
53     @Deprecated\r
54     public Collection<ChangeSetIdentifier> getChangeSets(long from, long to) throws DatabaseException {\r
55         return session.graphSession.getChangeSets(from, to, session.state.getHeadRevisionId());\r
56     }\r
57     @Override\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
61     }\r
62 \r
63     @SuppressWarnings("unchecked")\r
64     @Override\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
68         try {\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
73                 if (null != md) {\r
74                     byte[] result = md.get(dataClass.getName());\r
75                     if (null != result) {\r
76                         try {\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.getCause());\r
87                         }\r
88                     }\r
89                 }\r
90             }\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
97         }\r
98         return results;\r
99     }\r
100 \r
101     @Override\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
109     }\r
110 \r
111     @Override\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
119     }\r
120 \r
121     @Override\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
127     }\r
128 \r
129     @Override\r
130     public long getFirstRevisionId() throws DatabaseException {\r
131         return session.graphSession.dbSession.getDatabase().serverGetTailChangeSetId();\r
132     }\r
133 \r
134     @Override\r
135     public void subscribe(ChangeSetListener changeSetListener) {\r
136         session.graphSession.addChangeSetListener(changeSetListener);\r
137     }\r
138     @Override\r
139     public void cancel(ChangeSetListener changeSetlistener) {\r
140         session.graphSession.removeChangeSetListener(changeSetlistener);\r
141     }\r
142 }\r