]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.event/src/org/simantics/event/view/handler/MilestoneListQuery.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.event / src / org / simantics / event / view / handler / MilestoneListQuery.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.event.view.handler;
13
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.Comparator;
17
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.common.request.ResourceRead;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
23 import org.simantics.db.exception.ServiceException;
24 import org.simantics.event.ontology.EventResource;
25 import org.simantics.layer0.Layer0;
26
27 /**
28  * Query the all milestones of an event log. 
29  * The result is an array if resourcs, ordered by time in ascending order.  
30  *  
31  * @author toni.kalajainen
32  */
33 public class MilestoneListQuery extends ResourceRead<MilestoneList> {
34         
35         public MilestoneListQuery(Resource eventlog) {
36                 super(eventlog);
37         }
38         
39     @Override
40     public MilestoneList perform(final ReadGraph graph) throws DatabaseException {
41         MilestoneList result = new MilestoneList();
42         result.milestones = new ArrayList<Resource>();
43         Layer0 L0 = Layer0.getInstance(graph);
44         final EventResource EVENT = EventResource.getInstance(graph);
45
46         //System.out.println("listing milestones");
47         ArrayList<Resource> list = new ArrayList<Resource>();
48         Resource eventLog = resource;
49         if (eventLog != null) {
50                 result.baseline = graph.getPossibleObject(eventLog, EVENT.EventLog_HasBaselineEvent);
51                 for (Resource slice : graph.getObjects(eventLog, L0.ConsistsOf)) {
52                 for (Resource event : graph.getObjects(slice, L0.ConsistsOf)) {
53                         if (!graph.hasStatement(event, EVENT.Milestone)) continue;
54                         list.add(event);
55                 }
56                 }
57         }
58         
59         Collections.sort(list, new Comparator<Resource>() {
60                         @Override
61                         public int compare(Resource o1, Resource o2) {
62                                 try {
63                                 Double t1 = graph.getPossibleRelatedValue(o1, EVENT.HasTimestamp);
64                                 Double t2 = graph.getPossibleRelatedValue(o2, EVENT.HasTimestamp);
65                                 if (t1==null) t1 = 0.0;
66                                 if (t2==null) t2 = 0.0;
67                                         return Double.compare(t1, t2);
68                                 } catch (ManyObjectsForFunctionalRelationException e) {
69                                         return 0;
70                                 } catch (ServiceException e) {
71                                         return 0;
72                                 }
73                         }});
74                 result.milestones = list;
75         return result;
76     }
77
78         public Resource getEventLog() {
79                 return resource;
80         }       
81
82
83 }