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