]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/ActivateModel.java
Merge "Documented difference of RuntimeEnvironmentRequest and Runti...quest2" into...
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / request / ActivateModel.java
1 package org.simantics.db.layer0.request;
2
3 import org.simantics.db.Resource;
4 import org.simantics.db.VirtualGraph;
5 import org.simantics.db.WriteGraph;
6 import org.simantics.db.common.request.WriteResultRequest;
7 import org.simantics.db.exception.DatabaseException;
8 import org.simantics.db.service.VirtualGraphSupport;
9 import org.simantics.operation.Layer0X;
10
11 public class ActivateModel extends WriteResultRequest<Boolean> {
12
13     final Resource project;
14     final Resource model;
15
16     public ActivateModel(Resource project, Resource model) {
17         
18         if(project == null) throw new IllegalArgumentException("project can not be null");
19         this.project = project;
20         this.model = model;
21         
22     }
23
24     public static Boolean perform(WriteGraph graph, Resource project, Resource model) throws DatabaseException {
25         Layer0X L0X = Layer0X.getInstance(graph);
26         if (model != null) {
27             if (!graph.hasStatement(model, L0X.IsActivatedBy, project)) {
28                 graph.deny(project, L0X.Activates);
29                 graph.claim(project, L0X.Activates, model);
30                 return true;
31             }
32         } else {
33             if (graph.hasStatement(project, L0X.Activates)) {
34                 graph.deny(project, L0X.Activates);
35                 return true;
36             }
37         }
38         return false;
39     }
40
41     @Override
42     public Boolean perform(WriteGraph graph) throws DatabaseException {
43         VirtualGraphSupport support = graph.getService(VirtualGraphSupport.class);
44         VirtualGraph activations = support.getWorkspacePersistent("activations");
45         VirtualGraph vg = graph.getProvider();
46         if (vg == activations) {
47             return perform(graph, project, model);
48         } else {
49             return graph.syncRequest(new WriteResultRequest<Boolean>(activations) {
50                 @Override
51                 public Boolean perform(WriteGraph graph) throws DatabaseException {
52                     return ActivateModel.perform(graph, project, model);
53                 }
54             });
55         }
56     }
57
58 }