X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.event%2Fsrc%2Forg%2Fsimantics%2Fevent%2Fview%2Fhandler%2FMilestoneListQuery.java;fp=bundles%2Forg.simantics.event%2Fsrc%2Forg%2Fsimantics%2Fevent%2Fview%2Fhandler%2FMilestoneListQuery.java;h=b5fc41d26d00e177df5875de180515d375597893;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git 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 index 000000000..b5fc41d26 --- /dev/null +++ b/bundles/org.simantics.event/src/org/simantics/event/view/handler/MilestoneListQuery.java @@ -0,0 +1,83 @@ +/******************************************************************************* + * Copyright (c) 2007, 2011 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.event.view.handler; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; + +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.request.ResourceRead; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.exception.ManyObjectsForFunctionalRelationException; +import org.simantics.db.exception.ServiceException; +import org.simantics.event.ontology.EventResource; +import org.simantics.layer0.Layer0; + +/** + * Query the all milestones of an event log. + * The result is an array if resourcs, ordered by time in ascending order. + * + * @author toni.kalajainen + */ +public class MilestoneListQuery extends ResourceRead { + + public MilestoneListQuery(Resource eventlog) { + super(eventlog); + } + + @Override + public MilestoneList perform(final ReadGraph graph) throws DatabaseException { + MilestoneList result = new MilestoneList(); + result.milestones = new ArrayList(); + Layer0 L0 = Layer0.getInstance(graph); + final EventResource EVENT = EventResource.getInstance(graph); + + //System.out.println("listing milestones"); + ArrayList list = new ArrayList(); + Resource eventLog = resource; + if (eventLog != null) { + result.baseline = graph.getPossibleObject(eventLog, EVENT.EventLog_HasBaselineEvent); + for (Resource slice : graph.getObjects(eventLog, L0.ConsistsOf)) { + for (Resource event : graph.getObjects(slice, L0.ConsistsOf)) { + if (!graph.hasStatement(event, EVENT.Milestone)) continue; + list.add(event); + } + } + } + + Collections.sort(list, new Comparator() { + @Override + public int compare(Resource o1, Resource o2) { + try { + Double t1 = graph.getPossibleRelatedValue(o1, EVENT.HasTimestamp); + Double t2 = graph.getPossibleRelatedValue(o2, EVENT.HasTimestamp); + if (t1==null) t1 = 0.0; + if (t2==null) t2 = 0.0; + return Double.compare(t1, t2); + } catch (ManyObjectsForFunctionalRelationException e) { + return 0; + } catch (ServiceException e) { + return 0; + } + }}); + result.milestones = list; + return result; + } + + public Resource getEventLog() { + return resource; + } + + +}