/
TopicmapsPlugin.java
295 lines (236 loc) · 11.1 KB
/
TopicmapsPlugin.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package de.deepamehta.plugins.topicmaps;
import de.deepamehta.plugins.topicmaps.model.ClusterCoords;
import de.deepamehta.plugins.topicmaps.model.TopicmapViewmodel;
import de.deepamehta.plugins.topicmaps.service.TopicmapsService;
import de.deepamehta.core.Association;
import de.deepamehta.core.Topic;
import de.deepamehta.core.model.AssociationModel;
import de.deepamehta.core.model.AssociationRoleModel;
import de.deepamehta.core.model.CompositeValueModel;
import de.deepamehta.core.model.TopicModel;
import de.deepamehta.core.model.TopicRoleModel;
import de.deepamehta.core.osgi.PluginActivator;
import de.deepamehta.core.service.ClientState;
import de.deepamehta.core.service.Directives;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.POST;
import javax.ws.rs.DELETE;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
import java.awt.Point;
import java.io.InputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
@Path("/topicmap")
@Consumes("application/json")
@Produces("application/json")
public class TopicmapsPlugin extends PluginActivator implements TopicmapsService {
// ------------------------------------------------------------------------------------------------------- Constants
private static final String DEFAULT_TOPICMAP_NAME = "untitled";
private static final String DEFAULT_TOPICMAP_URI = "dm4.topicmaps.default_topicmap";
private static final String DEFAULT_TOPICMAP_RENDERER = "dm4.webclient.default_topicmap_renderer";
// association type semantics ### TODO: to be dropped. Model-driven manipulators required.
private static final String TOPIC_MAPCONTEXT = "dm4.topicmaps.topic_mapcontext";
private static final String ASSOCIATION_MAPCONTEXT = "dm4.topicmaps.association_mapcontext";
private static final String ROLE_TYPE_TOPICMAP = "dm4.core.default";
private static final String ROLE_TYPE_TOPIC = "dm4.topicmaps.topicmap_topic";
private static final String ROLE_TYPE_ASSOCIATION = "dm4.topicmaps.topicmap_association";
// ---------------------------------------------------------------------------------------------- Instance Variables
Map<String, TopicmapRenderer> topicmapRenderers = new HashMap();
Set<ViewmodelCustomizer> viewmodelCustomizers = new HashSet();
private Logger logger = Logger.getLogger(getClass().getName());
// -------------------------------------------------------------------------------------------------- Public Methods
public TopicmapsPlugin() {
// Note: registering the default renderer in the InitializePluginListener would be too late.
// The renderer is already needed in the PostInstallPluginListener.
registerTopicmapRenderer(new DefaultTopicmapRenderer());
}
// ***************************************
// *** TopicmapsService Implementation ***
// ***************************************
@POST
@Path("/{name}/{topicmap_renderer_uri}")
@Override
public Topic createTopicmap(@PathParam("name") String name,
@PathParam("topicmap_renderer_uri") String topicmapRendererUri,
@HeaderParam("Cookie") ClientState clientState) {
return createTopicmap(name, null, topicmapRendererUri, clientState);
}
@Override
public Topic createTopicmap(String name, String uri, String topicmapRendererUri, ClientState clientState) {
CompositeValueModel topicmapState = getTopicmapRenderer(topicmapRendererUri).initialTopicmapState();
return dms.createTopic(new TopicModel(uri, "dm4.topicmaps.topicmap", new CompositeValueModel()
.put("dm4.topicmaps.name", name)
.put("dm4.topicmaps.topicmap_renderer_uri", topicmapRendererUri)
.put("dm4.topicmaps.state", topicmapState)
), clientState);
}
// ---
@GET
@Path("/{id}")
@Override
public TopicmapViewmodel getTopicmap(@PathParam("id") long topicmapId) {
try {
return new TopicmapViewmodel(topicmapId, dms, viewmodelCustomizers);
} catch (Exception e) {
throw new RuntimeException("Fetching topicmap " + topicmapId + " failed", e);
}
}
// ---
@POST
@Path("/{id}/topic/{topic_id}")
@Override
public void addTopicToTopicmap(@PathParam("id") long topicmapId, @PathParam("topic_id") long topicId,
CompositeValueModel viewProps) {
dms.createAssociation(new AssociationModel(TOPIC_MAPCONTEXT,
new TopicRoleModel(topicmapId, ROLE_TYPE_TOPICMAP),
new TopicRoleModel(topicId, ROLE_TYPE_TOPIC),
viewProps
), null); // FIXME: clientState=null
}
@POST
@Path("/{id}/association/{assoc_id}")
@Override
public void addAssociationToTopicmap(@PathParam("id") long topicmapId, @PathParam("assoc_id") long assocId) {
dms.createAssociation(new AssociationModel(ASSOCIATION_MAPCONTEXT,
new TopicRoleModel(topicmapId, ROLE_TYPE_TOPICMAP),
new AssociationRoleModel(assocId, ROLE_TYPE_ASSOCIATION)
), null); // FIXME: clientState=null
}
@PUT
@Path("/{id}/topic/{topic_id}")
@Override
public void updateViewProperties(long topicmapId, long topicId, CompositeValueModel viewProps) {
updateStandardViewProperties(topicmapId, topicId, viewProps);
// ### TODO: update custom view properties (invoking the viewmodel customizers)
}
@PUT
@Path("/{id}/topic/{topic_id}/{x}/{y}")
@Override
public void moveTopic(@PathParam("id") long topicmapId, @PathParam("topic_id") long topicId, @PathParam("x") int x,
@PathParam("y") int y) {
updateStandardViewProperties(topicmapId, topicId, new CompositeValueModel()
.put("dm4.topicmaps.x", x)
.put("dm4.topicmaps.y", y)
);
}
@PUT
@Path("/{id}/topic/{topic_id}/{visibility}")
@Override
public void setTopicVisibility(@PathParam("id") long topicmapId, @PathParam("topic_id") long topicId,
@PathParam("visibility") boolean visibility) {
updateStandardViewProperties(topicmapId, topicId, new CompositeValueModel()
.put("dm4.topicmaps.visibility", visibility)
);
}
@DELETE
@Path("/{id}/association/{assoc_id}")
@Override
public void removeAssociationFromTopicmap(@PathParam("id") long topicmapId, @PathParam("assoc_id") long assocId) {
fetchAssociationRefAssociation(topicmapId, assocId).delete(new Directives());
}
@PUT
@Path("/{id}")
@Override
public void moveCluster(@PathParam("id") long topicmapId, ClusterCoords coords) {
for (ClusterCoords.Entry entry : coords) {
moveTopic(topicmapId, entry.topicId, entry.x, entry.y);
}
}
@PUT
@Path("/{id}/translation/{x}/{y}")
@Override
public void setTopicmapTranslation(@PathParam("id") long topicmapId, @PathParam("x") int transX,
@PathParam("y") int transY) {
try {
CompositeValueModel topicmapState = new CompositeValueModel()
.put("dm4.topicmaps.state", new CompositeValueModel()
.put("dm4.topicmaps.translation", new CompositeValueModel()
.put("dm4.topicmaps.translation_x", transX)
.put("dm4.topicmaps.translation_y", transY)
)
);
dms.updateTopic(new TopicModel(topicmapId, topicmapState), null);
} catch (Exception e) {
throw new RuntimeException("Setting translation of topicmap " + topicmapId + " failed (transX=" +
transX + ", transY=" + transY + ")", e);
}
}
// ---
@Override
public void registerTopicmapRenderer(TopicmapRenderer renderer) {
logger.info("### Registering topicmap renderer \"" + renderer.getClass().getName() + "\"");
topicmapRenderers.put(renderer.getUri(), renderer);
}
@Override
public void registerViewmodelCustomizer(ViewmodelCustomizer customizer) {
logger.info("### Registering viewmodel customizer \"" + customizer.getClass().getName() + "\"");
viewmodelCustomizers.add(customizer);
}
// ---
// Note: not part of topicmaps service
@GET
@Path("/{id}")
@Produces("text/html")
public InputStream getTopicmapInWebclient() {
// Note: the path parameter is evaluated at client-side
return invokeWebclient();
}
// Note: not part of topicmaps service
@GET
@Path("/{id}/topic/{topic_id}")
@Produces("text/html")
public InputStream getTopicmapAndTopicInWebclient() {
// Note: the path parameters are evaluated at client-side
return invokeWebclient();
}
// ****************************
// *** Hook Implementations ***
// ****************************
@Override
public void postInstall() {
createTopicmap(DEFAULT_TOPICMAP_NAME, DEFAULT_TOPICMAP_URI, DEFAULT_TOPICMAP_RENDERER, null);
// Note: null is passed as clientState. On post-install we have no clientState.
// The workspace assignment is made by the Access Control plugin on all-plugins-active.
}
// ------------------------------------------------------------------------------------------------- Private Methods
public void updateStandardViewProperties(long topicmapId, long topicId, CompositeValueModel viewProps) {
fetchTopicRefAssociation(topicmapId, topicId).setCompositeValue(viewProps, null, new Directives());
} // clientState=null
// ---
private Association fetchTopicRefAssociation(long topicmapId, long topicId) {
return dms.getAssociation(TOPIC_MAPCONTEXT, topicmapId, topicId,
ROLE_TYPE_TOPICMAP, ROLE_TYPE_TOPIC, false); // fetchComposite=false
}
private Association fetchAssociationRefAssociation(long topicmapId, long assocId) {
return dms.getAssociationBetweenTopicAndAssociation(ASSOCIATION_MAPCONTEXT, topicmapId, assocId,
ROLE_TYPE_TOPICMAP, ROLE_TYPE_ASSOCIATION, false); // fetchComposite=false
}
// ---
private TopicmapRenderer getTopicmapRenderer(String rendererUri) {
TopicmapRenderer renderer = topicmapRenderers.get(rendererUri);
//
if (renderer == null) {
throw new RuntimeException("\"" + rendererUri + "\" is an unknown topicmap renderer");
}
//
return renderer;
}
// ---
private InputStream invokeWebclient() {
try {
return dms.getPlugin("de.deepamehta.webclient").getResourceAsStream("web/index.html");
} catch (Exception e) {
throw new RuntimeException("Invoking the webclient failed", e);
}
}
}