]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.event/src/org/simantics/event/util/EventUtils.java
Sync git svn branch with SVN repository r33324.
[simantics/platform.git] / bundles / org.simantics.event / src / org / simantics / event / util / EventUtils.java
1 package org.simantics.event.util;\r
2 \r
3 import java.util.List;\r
4 import java.util.UUID;\r
5 \r
6 import org.simantics.databoard.Bindings;\r
7 import org.simantics.db.Resource;\r
8 import org.simantics.db.WriteGraph;\r
9 import org.simantics.db.common.request.PossibleTypedParent;\r
10 import org.simantics.db.exception.DatabaseException;\r
11 import org.simantics.event.ontology.EventResource;\r
12 import org.simantics.layer0.Layer0;\r
13 \r
14 public class EventUtils {\r
15 \r
16         public static final int MAX_EVENTS = 1000;\r
17         public static final int SLICE_SIZE = 64;\r
18         public static final int KEEPS_SLICES = (MAX_EVENTS / SLICE_SIZE) + 2;\r
19         public static final String MAX_SLICE_NAME = "" + (SLICE_SIZE-1);\r
20 \r
21         public static Resource createLog(WriteGraph graph) throws DatabaseException {\r
22                 Layer0 L0 = Layer0.getInstance(graph);\r
23                 EventResource EVENT = EventResource.getInstance(graph);\r
24         Resource log = graph.newResource();\r
25         graph.claim(log, L0.InstanceOf, null, EVENT.EventLog);\r
26         graph.claimLiteral(log, L0.HasName, UUID.randomUUID().toString(), Bindings.STRING);\r
27         graph.claimLiteral(log, EVENT.HasModificationCounter, 0, Bindings.INTEGER);\r
28         return log;\r
29         }\r
30 \r
31     /**\r
32      * Bumps the modification counter value of the event log of the specified\r
33      * events using {@link #bumpModificationCounter(WriteGraph, Resource)}.\r
34      * <p>\r
35      * The code assumes that all events are from the same log.\r
36      * \r
37      * @param graph\r
38      * @param forLogOfEvents\r
39      * @throws DatabaseException\r
40      */\r
41     public static void bumpModificationCounter(WriteGraph graph, List<Resource> forLogOfEvents) throws DatabaseException {\r
42         EventResource EVENT = EventResource.getInstance(graph);\r
43         for (Resource event : forLogOfEvents) {\r
44             Resource log = graph.syncRequest(new PossibleTypedParent(event, EVENT.EventLog));\r
45             if (log != null) {\r
46                 bumpModificationCounter(graph, log);\r
47                 return;\r
48             }\r
49         }\r
50     }\r
51 \r
52     /**\r
53      * Bumps the modification counter of the specified event log by 1.\r
54      * \r
55      * @param graph\r
56      * @param eventLog\r
57      * @return new modification counter value\r
58      * @throws DatabaseException\r
59      */\r
60     public static int bumpModificationCounter(WriteGraph graph, Resource eventLog) throws DatabaseException {\r
61         EventResource EVENT = EventResource.getInstance(graph);\r
62         Resource counter = graph.getPossibleObject(eventLog, EVENT.HasModificationCounter);\r
63         if (counter != null) {\r
64             Integer c = graph.getValue(counter, Bindings.INTEGER);\r
65             c = c == null ? 1 : c+1;\r
66             graph.claimValue(counter, c, Bindings.INTEGER);\r
67             return c;\r
68         }\r
69         return 0;\r
70     }\r
71 \r
72 }\r