]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.event/src/org/simantics/event/view/handler/Delete.java
claimWithInverse & possibleInverseOf function to DB.scl
[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.ui.ISelectionUtils;
36
37
38 /**
39  * @author Tuukka Lehtonen
40  */
41 public class Delete extends AbstractHandler {
42
43     protected final String virtualGraphId;
44         
45     public Delete() {
46         virtualGraphId = "experiments";
47     }
48     
49     @Override
50     public Object execute(ExecutionEvent event) throws ExecutionException {
51         Session session = Simantics.peekSession();
52         if (session == null)
53             return null;
54         ISelection selection = HandlerUtil.getCurrentSelection(event);
55         final List<Resource> resources = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
56         final VirtualGraph vg = virtualGraphId == null ? null :
57             session.getService(VirtualGraphSupport.class).getWorkspacePersistent(virtualGraphId);
58
59         session.asyncRequest(new WriteRequest(vg) {
60             @Override
61             public void perform(WriteGraph graph) throws DatabaseException {
62                 Layer0 L0 = Layer0.getInstance(graph);
63                 EventResource EVENT = EventResource.getInstance(graph);
64                 Set<Resource> eventLogs = new HashSet<Resource>();
65                 for (Resource event : resources) {
66                         if (graph.isInstanceOf(event, EVENT.Event)) {
67                                 Resource slice = graph.getPossibleObject(event, L0.PartOf);
68                                 if(slice != null) {
69                                         Resource eventlog =     graph.getPossibleObject(slice, L0.PartOf);
70                                 if (eventlog != null) eventLogs.add(eventlog);
71                                 }
72                         }
73                         graph.deny( event );
74                 }
75                 for (Resource eventlog : eventLogs) {
76                         if (graph.isInstanceOf(eventlog, EVENT.EventLog))
77                             graph.syncRequest( new CorrectMilestoneLabelsAction(eventlog, vg) );
78                 }
79             }
80         }, e -> {
81             if (e != null) Logger.defaultLogError(e);
82         });
83         return null;
84     }
85
86 }