]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.event/src/org/simantics/event/view/handler/Delete.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.event / src / org / simantics / event / view / handler / Delete.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management
3  * in 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.HashSet;
15 import java.util.List;
16 import java.util.Set;
17
18 import org.eclipse.core.commands.AbstractHandler;
19 import org.eclipse.core.commands.ExecutionEvent;
20 import org.eclipse.core.commands.ExecutionException;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.ui.handlers.HandlerUtil;
23 import org.simantics.Simantics;
24 import org.simantics.db.Resource;
25 import org.simantics.db.Session;
26 import org.simantics.db.VirtualGraph;
27 import org.simantics.db.WriteGraph;
28 import org.simantics.db.common.request.WriteRequest;
29 import org.simantics.db.common.utils.Logger;
30 import org.simantics.db.exception.DatabaseException;
31 import org.simantics.db.layer0.SelectionHints;
32 import org.simantics.db.service.VirtualGraphSupport;
33 import org.simantics.event.ontology.EventResource;
34 import org.simantics.layer0.Layer0;
35 import org.simantics.utils.datastructures.Callback;
36 import org.simantics.utils.ui.ISelectionUtils;
37
38
39 /**
40  * @author Tuukka Lehtonen
41  */
42 public class Delete extends AbstractHandler {
43
44     protected final String virtualGraphId;
45         
46     public Delete() {
47         virtualGraphId = "experiments";
48     }
49     
50     @Override
51     public Object execute(ExecutionEvent event) throws ExecutionException {
52         Session session = Simantics.peekSession();
53         if (session == null)
54             return null;
55         ISelection selection = HandlerUtil.getCurrentSelection(event);
56         final List<Resource> resources = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
57         final VirtualGraph vg = virtualGraphId == null ? null :
58             session.getService(VirtualGraphSupport.class).getWorkspacePersistent(virtualGraphId);
59
60         session.asyncRequest(new WriteRequest(vg) {
61             @Override
62             public void perform(WriteGraph graph) throws DatabaseException {
63                 Layer0 L0 = Layer0.getInstance(graph);
64                 EventResource EVENT = EventResource.getInstance(graph);
65                 Set<Resource> eventLogs = new HashSet<Resource>();
66                 for (Resource event : resources) {
67                         if (graph.isInstanceOf(event, EVENT.Event)) {
68                                 Resource slice = graph.getPossibleObject(event, L0.PartOf);
69                                 if(slice != null) {
70                                         Resource eventlog =     graph.getPossibleObject(slice, L0.PartOf);
71                                 if (eventlog != null) eventLogs.add(eventlog);
72                                 }
73                         }
74                         graph.deny( event );
75                 }
76                 for (Resource eventlog : eventLogs) {
77                         if (graph.isInstanceOf(eventlog, EVENT.EventLog))
78                             graph.syncRequest( new CorrectMilestoneLabelsAction(eventlog, vg) );
79                 }
80             }
81         }, new Callback<DatabaseException>() {
82             @Override
83             public void run(DatabaseException e) {
84                 if (e != null) Logger.defaultLogError(e);
85             }
86         });
87         return null;
88     }
89
90 }