]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/export/DiagramContentTypeAction.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / export / DiagramContentTypeAction.java
1 package org.simantics.diagram.export;
2
3 import java.util.Collection;
4 import java.util.Deque;
5 import java.util.HashMap;
6 import java.util.LinkedList;
7 import java.util.Map;
8
9 import org.simantics.NameLabelMode;
10 import org.simantics.NameLabelUtil;
11 import org.simantics.db.ReadGraph;
12 import org.simantics.db.Resource;
13 import org.simantics.db.common.request.UniqueRead;
14 import org.simantics.db.exception.AssumptionException;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
17 import org.simantics.db.exception.ServiceException;
18 import org.simantics.db.exception.ValidationException;
19 import org.simantics.db.layer0.request.PossibleModel;
20 import org.simantics.db.request.Read;
21 import org.simantics.export.core.ExportContext;
22 import org.simantics.export.core.error.ExportException;
23 import org.simantics.export.core.intf.ContentTypeAction;
24 import org.simantics.layer0.Layer0;
25 import org.simantics.simulation.ontology.SimulationResource;
26
27 public class DiagramContentTypeAction implements ContentTypeAction {
28         
29         public static String getDiagramLabel(ReadGraph graph, Resource r) throws DatabaseException
30         {
31                 SimulationResource SIM = SimulationResource.getInstance(graph);
32
33                 NameLabelMode mode = NameLabelUtil.getNameLabelMode(graph);
34
35                 labelFailed:
36                 try { 
37                         Resource model = graph.sync( new PossibleModel(r) );
38                         if ( model == null ) break labelFailed;
39                         Resource configuration = graph.getPossibleObject(model, SIM.HasConfiguration);
40                         if ( configuration == null ) break labelFailed;
41                         Deque<Resource> path = getPathTo(graph, r, model);
42                         if ( path == null) break labelFailed;
43                         path.remove(configuration);
44
45                         StringBuilder sb = new StringBuilder();
46                         for ( Resource node : path ) {
47                                 if ( sb.length()>0 ) sb.append(" / ");
48                                 String nodeLabel = NameLabelUtil.modalName(graph, node, mode);
49                                 sb.append( nodeLabel );
50                         }
51                                 
52                         return sb.toString();
53                 } catch (AssumptionException e) {
54                 } catch (ValidationException e) {
55                 } catch (ServiceException e) {
56                 }
57                 
58                 String uri = graph.getURI( r );
59                 int c = uri.lastIndexOf('/');
60                 if ( c>=0 ) {
61                         String label = uri.substring(c+1);
62                         if ( !label.isEmpty() ) return label;
63                 }
64                 
65                 return uri;
66         }
67
68         @Override
69         public Map<String, String> getLabels(ExportContext ctx, final Collection<String> uris) throws ExportException {
70                 Read<Map<String, String>> req = new UniqueRead<Map<String, String>>() {
71                         @Override
72                         public Map<String, String> perform(ReadGraph graph) throws DatabaseException {
73                                 Map<String, String> result = new HashMap<String, String>();
74                                 
75                                 for ( String uri : uris ) {
76                                 Resource r = graph.getResource( uri );
77                                         String label = getDiagramLabel(graph, r);
78                                 result.put(uri, label);
79                                 }
80                                 
81                                 return result;
82                         }
83                 };
84                 
85                 try {
86                         return ctx.session.syncRequest( req );
87                 } catch (DatabaseException e) {
88                         throw new ExportException( e );
89                 }
90         }
91         
92         /**
93          * Get all resource between start and end by following PartOf relation.
94          * If there is no path, returns null.
95          * Start is included, end is excluded.
96          * 
97          * @param graph
98          * @param start
99          * @param end
100          * @return path from end to start
101          * @throws ServiceException 
102          * @throws ManyObjectsForFunctionalRelationException 
103          */
104         static Deque<Resource> getPathTo(ReadGraph graph, Resource start, Resource end) throws DatabaseException {
105                 LinkedList<Resource> result = new LinkedList<Resource>();
106                 
107                 Layer0 L0 = Layer0.getInstance(graph);
108                 Resource pos = start;
109                 while ( !pos.equals(end) ) {
110                         result.add(0, pos);
111                         pos = graph.getPossibleObject(pos, L0.PartOf);
112                         if ( pos==null ) return null;
113                 }
114                 return result;
115         }
116
117 }