Support diagram profile activity store/restore in model export/import
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / profile / ProfileActivityBeanRequest.java
1 package org.simantics.diagram.profile;
2
3 import org.simantics.db.ReadGraph;
4 import org.simantics.db.Resource;
5 import org.simantics.db.common.NamedResource;
6 import org.simantics.db.common.request.ResourceRead;
7 import org.simantics.db.exception.DatabaseException;
8 import org.simantics.diagram.profile.ProfileActivityBean.Profile;
9 import org.simantics.diagram.stubs.DiagramResource;
10 import org.simantics.simulation.ontology.SimulationResource;
11
12 /**
13  * @author Tuukka Lehtonen
14  * @since 1.32.0
15  */
16 class ProfileActivityBeanRequest extends ResourceRead<ProfileActivityBean> {
17
18         public ProfileActivityBeanRequest(Resource root) {
19                 super(root);
20         }
21
22         @Override
23         public ProfileActivityBean perform(ReadGraph graph) throws DatabaseException {
24                 ProfileActivityBean bean = new ProfileActivityBean();
25                 String rootUri = graph.getPossibleURI(resource);
26                 if (rootUri == null)
27                         return bean;
28
29                 for (NamedResource r : graph.syncRequest(new TopLevelProfiles(resource))) {
30                         Profile prof = readProfile(graph, rootUri, r.getResource());
31                         if (prof != null)
32                                 bean.topLevelProfiles.put(prof.relativeUri, prof);
33                 }
34
35                 DiagramResource DIA = DiagramResource.getInstance(graph);
36                 Resource activeProfile = graph.getPossibleObject(resource, DIA.HasActiveProfile);
37                 if (activeProfile != null) {
38                         bean.activeProfile = Profiles.possiblyRelativeUri(graph, rootUri, activeProfile);
39                 }
40
41                 return bean;
42         }
43
44         private static Profile readProfile(ReadGraph graph, String rootUri, Resource profile) throws DatabaseException {
45                 Profile prof = new Profile();
46                 prof.relativeUri = Profiles.possiblyRelativeUri(graph, rootUri, profile);
47                 if (prof.relativeUri == null)
48                         return null;
49
50                 SimulationResource SIMU = SimulationResource.getInstance(graph);
51                 for (Resource active : graph.getObjects(profile, SIMU.IsActive)) {
52                         String euri = Profiles.possiblyRelativeUri(graph, rootUri, active);
53                         if (euri != null)
54                                 prof.activeEntries.add(euri);
55                 }
56
57                 return prof;
58         }
59
60 }