Removed unnecessary dependencies on org.apache.log4j
[simantics/platform.git] / bundles / org.simantics.diagram.profile / src / org / simantics / diagram / profile / view / CreateProfileAction.java
1 package org.simantics.diagram.profile.view;
2
3 import org.simantics.Simantics;
4 import org.simantics.browsing.ui.swt.ModelledActionImpl;
5 import org.simantics.db.Resource;
6 import org.simantics.db.WriteGraph;
7 import org.simantics.db.common.request.WriteRequest;
8 import org.simantics.db.common.utils.NameUtils;
9 import org.simantics.db.common.utils.OrderedSetUtils;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.diagram.stubs.DiagramResource;
12 import org.simantics.layer0.Layer0;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 public class CreateProfileAction extends ModelledActionImpl<Resource> {
17
18         private static final Logger LOGGER = LoggerFactory.getLogger(CreateProfileAction.class);
19
20         public CreateProfileAction(Resource configuration) {
21                 super(configuration);
22         }
23         
24         public void run(final Resource runtimeDiagram) {
25
26                 try {
27                 
28                         Simantics.getSession().syncRequest(new WriteRequest() {
29         
30                                 @Override
31                                 public void perform(WriteGraph graph) throws DatabaseException {
32         
33                                         Layer0 L0 = Layer0.getInstance(graph);
34                                         DiagramResource DIA = DiagramResource.getInstance(graph);
35                                 String modelURI = graph.getPossibleRelatedValue((Resource)runtimeDiagram, DIA.RuntimeDiagram_HasModelURI);
36                                 if (modelURI == null) return;
37                                 
38                                 Resource model = graph.getPossibleResource(modelURI);
39                                 if(model == null) return;
40                                 
41                                 String name = NameUtils.findFreshName(graph, "Profile", model, L0.ConsistsOf);
42
43                                 Resource list = OrderedSetUtils.create(graph, DIA.Profile);
44
45                                 Resource profile = graph.newResource();
46                                 graph.claim(profile, L0.InstanceOf, null, DIA.Profile);
47                                 graph.claimLiteral(profile, L0.HasName, name);
48                                 graph.claim(profile, DIA.HasEntries, null, list);
49                                 graph.claim(model, L0.ConsistsOf, profile);
50                                 //graph.claim(model, DIA.HasProfile, profile);
51         
52                                 }
53         
54                         });
55                 
56                 } catch (DatabaseException e) {
57                         LOGGER.error("CreateProfileAction failed", e);
58                 }
59
60         }
61
62 }