1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.event.view.handler;
\r
14 import java.util.ArrayList;
\r
15 import java.util.Collections;
\r
16 import java.util.Comparator;
\r
18 import org.simantics.db.ReadGraph;
\r
19 import org.simantics.db.Resource;
\r
20 import org.simantics.db.common.request.ResourceRead;
\r
21 import org.simantics.db.exception.DatabaseException;
\r
22 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
\r
23 import org.simantics.db.exception.ServiceException;
\r
24 import org.simantics.event.ontology.EventResource;
\r
25 import org.simantics.layer0.Layer0;
\r
28 * Query the all milestones of an event log.
\r
29 * The result is an array if resourcs, ordered by time in ascending order.
\r
31 * @author toni.kalajainen
\r
33 public class MilestoneListQuery extends ResourceRead<MilestoneList> {
\r
35 public MilestoneListQuery(Resource eventlog) {
\r
40 public MilestoneList perform(final ReadGraph graph) throws DatabaseException {
\r
41 MilestoneList result = new MilestoneList();
\r
42 result.milestones = new ArrayList<Resource>();
\r
43 Layer0 L0 = Layer0.getInstance(graph);
\r
44 final EventResource EVENT = EventResource.getInstance(graph);
\r
46 //System.out.println("listing milestones");
\r
47 ArrayList<Resource> list = new ArrayList<Resource>();
\r
48 Resource eventLog = resource;
\r
49 if (eventLog != null) {
\r
50 result.baseline = graph.getPossibleObject(eventLog, EVENT.EventLog_HasBaselineEvent);
\r
51 for (Resource slice : graph.getObjects(eventLog, L0.ConsistsOf)) {
\r
52 for (Resource event : graph.getObjects(slice, L0.ConsistsOf)) {
\r
53 if (!graph.hasStatement(event, EVENT.Milestone)) continue;
\r
59 Collections.sort(list, new Comparator<Resource>() {
\r
61 public int compare(Resource o1, Resource o2) {
\r
63 Double t1 = graph.getPossibleRelatedValue(o1, EVENT.HasTimestamp);
\r
64 Double t2 = graph.getPossibleRelatedValue(o2, EVENT.HasTimestamp);
\r
65 if (t1==null) t1 = 0.0;
\r
66 if (t2==null) t2 = 0.0;
\r
67 return Double.compare(t1, t2);
\r
68 } catch (ManyObjectsForFunctionalRelationException e) {
\r
70 } catch (ServiceException e) {
\r
74 result.milestones = list;
\r
78 public Resource getEventLog() {
\r