]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/SetActiveSessionRole.java
possibleObjectForType utility
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / request / SetActiveSessionRole.java
1 package org.simantics.db.layer0.request;
2
3 import org.simantics.db.RequestProcessor;
4 import org.simantics.db.Resource;
5 import org.simantics.db.SessionModel;
6 import org.simantics.db.WriteGraph;
7 import org.simantics.db.common.request.WriteRequest;
8 import org.simantics.db.exception.DatabaseException;
9 import org.simantics.db.service.VirtualGraphSupport;
10 import org.simantics.layer0.Layer0;
11 import org.simantics.operation.Layer0X;
12 import org.simantics.user.UserResource;
13
14 public class SetActiveSessionRole extends WriteRequest {
15     
16     final private Resource model;
17     final private Resource role;
18     
19     public SetActiveSessionRole(RequestProcessor processor, Resource model, Resource role) {
20         super(processor.getService(VirtualGraphSupport.class).getWorkspacePersistent("activations"));
21         this.model = model;
22         this.role = role;
23     }
24
25     @Override
26     public void perform(WriteGraph graph) throws DatabaseException {
27         
28         Layer0 L0 = Layer0.getInstance(graph);
29         Layer0X L0X = Layer0X.getInstance(graph);
30         UserResource USER = UserResource.getInstance(graph);
31         Resource session = graph.getService(SessionModel.class).getResource();
32         
33         if(session != null) {
34             for(Resource selection : graph.getObjects(session, L0X.Session_HasRoleSelection)) {
35                 Resource selectionModel = graph.getPossibleObject(selection, USER.RoleSelection_HasModel);
36                 if(selectionModel.equals(model)) {
37                     graph.deny(selection, USER.RoleSelection_HasRole);
38                     graph.claim(selection, USER.RoleSelection_HasRole, role);
39                     return;
40                 }
41             }
42         }
43         
44         Resource selection = graph.newResource();
45         graph.claim(selection, L0.InstanceOf, null, USER.RoleSelection);
46         graph.claim(selection, USER.RoleSelection_HasModel, model);
47         graph.claim(selection, USER.RoleSelection_HasRole, role);
48         graph.claim(session, L0X.Session_HasRoleSelection, selection);
49         
50     }
51
52 }