]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/view/Bean.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / view / Bean.java
1 package org.simantics.modeling.ui.view;
2
3 import java.util.Collection;
4 import java.util.Date;
5
6 import org.simantics.charts.ontology.ChartResource;
7 import org.simantics.databoard.Bindings;
8 import org.simantics.databoard.util.URIStringUtils;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.common.utils.NameUtils;
12 import org.simantics.db.common.utils.Versions;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.layer0.Layer0;
15 import org.simantics.modeling.All;
16 import org.simantics.modeling.ModelingResources;
17 import org.simantics.modeling.adapters.ChangeInformation;
18 import org.simantics.modeling.subscription.SubscriptionItemLabel;
19 import org.simantics.utils.format.ValueFormat;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public class Bean {
24
25         private static final Logger LOGGER = LoggerFactory.getLogger(Bean.class);
26
27         public String createdBy = "";
28         public String modifiedBy = "";
29         public long modifiedAt = 0;
30         public long createdAt = 0;
31         public String name = "";
32         public String path = "";
33         public String types = "";
34         
35         public Bean() {
36                 
37         }
38
39         public Bean(ReadGraph graph, Resource r, ChangeInformation i, String baseURI) throws DatabaseException {
40                 
41                 Layer0 L0 = Layer0.getInstance(graph);
42                 ModelingResources MOD = ModelingResources.getInstance(graph);
43                 ChartResource CHART = ChartResource.getInstance(graph);
44
45                 Collection<Resource> principalTypes = graph.getPrincipalTypes(r);
46
47                 if (principalTypes.contains(MOD.Subscription)) {
48                         String label = graph.getPossibleRelatedValue2(r, L0.HasLabel, Bindings.STRING);
49                         setName((label != null && !label.isEmpty()) ? label : "<unnamed subscription>");
50                 } else if (principalTypes.contains(MOD.Subscription_Item)) {
51                         String label = SubscriptionItemLabel.resolveLabel(graph, r, ValueFormat.Default, true, true, false);
52                         setName(label);
53                 } else if (principalTypes.contains(CHART.Chart_Item)) {
54                         String label = graph.getPossibleRelatedValue2(r, L0.HasLabel, Bindings.STRING);
55                         setName((label != null && !label.isEmpty()) ? label : "<unlabeled chart item>");
56                 } else {
57
58                         Resource possibleComponent = graph.getPossibleObject(r, MOD.ElementToComponent);
59                         Resource possibleComposite = graph.getPossibleObject(r, MOD.DiagramToComposite);
60                         
61                         Resource nameResource = r;
62                         if(possibleComponent != null) nameResource = possibleComponent;
63                         if(possibleComposite != null) nameResource = possibleComposite;
64
65                         String name = Versions.getStandardNameString(graph, nameResource);
66                         
67                         if(possibleComponent != null) name += " Element";
68                         if(possibleComposite != null) name += " Diagram";
69                         
70                         setName(name);
71                         
72                 }
73                 
74                 StringBuilder types = new StringBuilder();
75                 for(Resource t : principalTypes) {
76                         if(types.length() > 0) types.append(" ");
77                         types.append(NameUtils.getSafeName(graph, t));
78                 }
79                 
80                 setTypes(types.toString());
81                 
82                 Resource parent = graph.getPossibleObject(r, L0.PartOf);
83                 if(parent != null) {
84                         String uri = graph.getPossibleURI(parent);
85                         if (uri != null) {
86                                 if(uri.endsWith("/__CONTAINER__/__DIAGRAM__")) uri = uri.substring(0, uri.length()-"/__CONTAINER__/__DIAGRAM__".length());
87                                 if(uri.endsWith("/__CONTAINER__")) uri = uri.substring(0, uri.length()-"/__CONTAINER__".length());
88                                 if(uri.equals(baseURI)) uri = "<selected resource>";
89                                 if(uri.startsWith(baseURI)) uri = uri.substring(baseURI.length());
90                                 if(uri.startsWith("/Configuration/")) uri = uri.substring("/Configuration/".length());
91
92                                 uri = uri.replace("http://Projects/Development%20Project/", "");
93                                 uri = URIStringUtils.unescape(uri);
94
95                                 if(graph.isInstanceOf(parent, MOD.Subscription)) {
96
97                                         String name = graph.getPossibleRelatedValue(parent, L0.HasName, Bindings.STRING);
98                                         String label = graph.getPossibleRelatedValue(parent, L0.HasLabel, Bindings.STRING);
99                                         uri = uri.replace(name, label);
100
101                                 }
102
103                                 setPath(uri);
104                         } else {
105                                 LOGGER.warn("Could not resolve path (URI) for parent resource {} of child {}", parent, r);
106                                 setPath("");
107                         }
108                 } else {
109                         setPath("");
110                 }
111                 
112                 setCreatedBy(i.createdBy);
113                 setModifiedBy(i.modifiedBy);
114                 setModifiedAt(i.modifiedAt);
115                 setCreatedAt(i.createdAt);
116                 
117         }
118
119         public String getCreatedBy() {
120                 return createdBy;
121         }
122
123         public void setCreatedBy(String createdBy) {
124                 this.createdBy = createdBy;
125         }
126
127         public String getModifiedBy() {
128                 return modifiedBy;
129         }
130
131         public void setModifiedBy(String modifiedBy) {
132                 this.modifiedBy = modifiedBy;
133         }
134
135         public String getModifiedAt() {
136                 return All.sdfLong.format(new Date(modifiedAt));
137         }
138
139         public void setModifiedAt(long modifiedAt) {
140                 this.modifiedAt = modifiedAt;
141         }
142
143         public String getCreatedAt() {
144                 return All.sdfLong.format(new Date(createdAt));
145         }
146
147         public void setCreatedAt(long createdAt) {
148                 this.createdAt = createdAt;
149         }
150
151         public String getName() {
152                 return name;
153         }
154
155         public void setName(String name) {
156                 this.name = name;
157         }
158
159         public String getPath() {
160                 return path;
161         }
162
163         public void setPath(String path) {
164                 this.path = path;
165         }
166
167         public String getTypes() {
168                 return types;
169         }
170
171         public void setTypes(String types) {
172                 this.types = types;
173         }
174
175 }