]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.event/src/org/simantics/event/view/handler/MilestoneListQuery.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.event / src / org / simantics / event / view / handler / MilestoneListQuery.java
diff --git a/bundles/org.simantics.event/src/org/simantics/event/view/handler/MilestoneListQuery.java b/bundles/org.simantics.event/src/org/simantics/event/view/handler/MilestoneListQuery.java
new file mode 100644 (file)
index 0000000..b5fc41d
--- /dev/null
@@ -0,0 +1,83 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.event.view.handler;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collections;\r
+import java.util.Comparator;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.ResourceRead;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;\r
+import org.simantics.db.exception.ServiceException;\r
+import org.simantics.event.ontology.EventResource;\r
+import org.simantics.layer0.Layer0;\r
+\r
+/**\r
+ * Query the all milestones of an event log. \r
+ * The result is an array if resourcs, ordered by time in ascending order.  \r
+ *  \r
+ * @author toni.kalajainen\r
+ */\r
+public class MilestoneListQuery extends ResourceRead<MilestoneList> {\r
+       \r
+       public MilestoneListQuery(Resource eventlog) {\r
+               super(eventlog);\r
+       }\r
+       \r
+    @Override\r
+    public MilestoneList perform(final ReadGraph graph) throws DatabaseException {\r
+       MilestoneList result = new MilestoneList();\r
+       result.milestones = new ArrayList<Resource>();\r
+       Layer0 L0 = Layer0.getInstance(graph);\r
+        final EventResource EVENT = EventResource.getInstance(graph);\r
+\r
+        //System.out.println("listing milestones");\r
+        ArrayList<Resource> list = new ArrayList<Resource>();\r
+        Resource eventLog = resource;\r
+        if (eventLog != null) {\r
+               result.baseline = graph.getPossibleObject(eventLog, EVENT.EventLog_HasBaselineEvent);\r
+               for (Resource slice : graph.getObjects(eventLog, L0.ConsistsOf)) {\r
+               for (Resource event : graph.getObjects(slice, L0.ConsistsOf)) {\r
+                       if (!graph.hasStatement(event, EVENT.Milestone)) continue;\r
+                       list.add(event);\r
+               }\r
+               }\r
+        }\r
+        \r
+        Collections.sort(list, new Comparator<Resource>() {\r
+                       @Override\r
+                       public int compare(Resource o1, Resource o2) {\r
+                               try {\r
+                               Double t1 = graph.getPossibleRelatedValue(o1, EVENT.HasTimestamp);\r
+                               Double t2 = graph.getPossibleRelatedValue(o2, EVENT.HasTimestamp);\r
+                               if (t1==null) t1 = 0.0;\r
+                               if (t2==null) t2 = 0.0;\r
+                                       return Double.compare(t1, t2);\r
+                               } catch (ManyObjectsForFunctionalRelationException e) {\r
+                                       return 0;\r
+                               } catch (ServiceException e) {\r
+                                       return 0;\r
+                               }\r
+                       }});\r
+               result.milestones = list;\r
+        return result;\r
+    }\r
+\r
+       public Resource getEventLog() {\r
+               return resource;\r
+       }       \r
+\r
+\r
+}\r