Skip to content

Commit f2ce295

Browse files
author
Ove Gram Nipen
committed
Added support to get associated TopicMapObject(s) from an instance of a TopicMapObject.
- Added getAssociatedTopics and getAssociatedTopicMapObject to TopicMapObject - The OpenSessionInViewFilter now sets a thread bound instance on TopicMapObjectRepository, so we don't have to pass that around - Refined the examples, making use of getAssociatedTopicMapObjects to get Albums, Artist, etc.
1 parent aee313f commit f2ce295

12 files changed

Lines changed: 159 additions & 14 deletions

File tree

examples/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@
4848
<configuration>
4949
<scanIntervalSeconds>10</scanIntervalSeconds>
5050
<useTestClasspath>true</useTestClasspath>
51+
<connectors>
52+
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
53+
<port>8081</port>
54+
</connector>
55+
</connectors>
5156
</configuration>
5257
</plugin>
5358
</plugins>

examples/src/main/java/no/delfidata/topicmaps/domain/Album.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@ public class Album extends TopicMapObject {
99
public Album( TopicIF topic ) {
1010
super( topic );
1111
}
12+
13+
public Artist getArtist() {
14+
return (Artist)getAssociatedTopicMapObject( "ex:album-created-by", "ex:album", "ex:artist" );
15+
}
1216
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package no.delfidata.topicmaps.domain;
2+
3+
import java.util.List;
4+
5+
import net.ontopia.topicmaps.core.TopicIF;
6+
import no.delfidata.topicmaps.Psi;
7+
import no.delfidata.topicmaps.TopicMapObject;
8+
9+
@Psi("http://psi.example.org/artist")
10+
public class Artist extends TopicMapObject {
11+
12+
public Artist( TopicIF topic ) {
13+
super( topic );
14+
}
15+
16+
public List<Album> getAlbums() {
17+
return getAssociatedTopicMapObjects( Album.class, "ex:album-created-by", "ex:artist", "ex:album" );
18+
}
19+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package no.delfidata.topicmaps.domain;
2+
3+
import net.ontopia.topicmaps.core.TopicIF;
4+
import no.delfidata.topicmaps.Psi;
5+
6+
@Psi("http://psi.example.org/group")
7+
public class Group extends Artist {
8+
9+
public Group( TopicIF topic ) {
10+
super( topic );
11+
}
12+
13+
}

examples/src/main/java/no/delfidata/topicmaps/examples/gui/HelloWorldController.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package no.delfidata.topicmaps.examples.gui;
22

3-
import java.util.Date;
43
import java.util.HashMap;
54
import java.util.Map;
65

@@ -13,6 +12,7 @@
1312
import org.springframework.web.servlet.ModelAndView;
1413
import org.springframework.web.servlet.mvc.Controller;
1514

15+
@SuppressWarnings("unchecked")
1616
public class HelloWorldController implements Controller {
1717

1818
private String viewName;
@@ -24,8 +24,6 @@ public void setViewName( String viewName ) {
2424
@Override
2525
public ModelAndView handleRequest( HttpServletRequest request, HttpServletResponse response ) throws Exception {
2626
Map model = new HashMap();
27-
model.put( "foo", new Date() );
28-
2927
TopicMapObjectRepository repository = (TopicMapObjectRepository)request.getAttribute( "repository" );
3028
TopicMapObject topic = repository.getByPsi( "http://psi.example.org/AHardDaysNightAlbum" );
3129

examples/src/main/webapp/WEB-INF/applicationContext.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ http://www.springframework.org/schema/util http://www.springframework.org/schema
1010
<property name="classes">
1111
<util:list value-type="java.lang.Class">
1212
<value>no.delfidata.topicmaps.domain.Album</value>
13+
<value>no.delfidata.topicmaps.domain.Artist</value>
14+
<value>no.delfidata.topicmaps.domain.Group</value>
1315
</util:list>
1416
</property>
1517
</bean>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<%@ page isErrorPage="true" %>
2+
<h1>Exception</h1>
3+
<%
4+
5+
exception.printStackTrace();
6+
%>
Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
<%@ page language="java" contentType="text/html; charset=UTF-8"
2-
pageEncoding="UTF-8"%>
2+
pageEncoding="UTF-8" errorPage="/WEB-INF/error.jsp"%>
3+
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
34
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
45
<html>
56
<head>
6-
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7-
<title>Insert title here</title>
7+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
8+
<title>Insert title here</title>
89
</head>
910
<body>
10-
${foo}
11-
${topic.name}
11+
12+
<h1>${topic.name}</h1>
13+
<h2>by ${topic.artist}</h2>
14+
${topic.description}
15+
16+
<h2>Other albums by ${topic.artist}</h2>
17+
<table>
18+
<tr><th>Title</th><th>Description</th></tr>
19+
<c:forEach items="${topic.artist.albums}" var="album">
20+
<tr><td>${album}</td><td>${album.description}</td></tr>
21+
</c:forEach>
22+
</table>
1223
</body>
1324
</html>

tmobjects-spring/src/main/java/no/delfidata/topicmaps/spring/OpenSessionInViewFilter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ protected void doFilterInternal( HttpServletRequest request, HttpServletResponse
2525
TopicMapStoreIF store = (TopicMapStoreIF)context.getBean( "topicMapStore" );
2626
TopicMapIF tm = store.getTopicMap();
2727
repository.setTopicMap( tm );
28+
TopicMapObjectRepository.setInstance( repository );
2829
request.setAttribute( "repository", repository );
2930
request.setAttribute( "tm", tm );
3031

tmobjects/src/main/java/no/delfidata/topicmaps/TopicMapObject.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,27 @@ public List<TopicIF> getAssociatedTopics( String associationTypePsi, String myRo
6363
return util.findBinaryPlayers( associationTypePsi, topic, myRoleTypePsi, wantedRoleTypePsi );
6464
}
6565

66+
public TopicIF getAssociatedTopic( String associationTypePsi, String myRoleTypePsi, String wantedRoleTypePsi ) {
67+
List<TopicIF> topics = getAssociatedTopics( associationTypePsi, myRoleTypePsi, wantedRoleTypePsi );
68+
if (topics.size() == 0) return null;
69+
return topics.get( 0 );
70+
}
71+
72+
public TopicMapObject getAssociatedTopicMapObject( String associationTypePsi, String myRoleTypePsi, String wantedRoleTypePsi ) {
73+
TopicIF associatedTopic = getAssociatedTopic( associationTypePsi, myRoleTypePsi, wantedRoleTypePsi );
74+
return TopicMapObjectRepository.getInstance().createInstance( associatedTopic );
75+
}
76+
77+
public List<TopicMapObject> getAssociatedTopicMapObjects( String associationTypePsi, String myRoleTypePsi, String wantedRoleTypePsi ) {
78+
return getAssociatedTopicMapObjects( TopicMapObject.class, associationTypePsi, myRoleTypePsi, wantedRoleTypePsi );
79+
}
80+
81+
public <T extends TopicMapObject> List<T> getAssociatedTopicMapObjects( Class<T> theclass, String associationTypePsi,
82+
String myRoleTypePsi, String wantedRoleTypePsi ) {
83+
List<TopicIF> topics = getAssociatedTopics( associationTypePsi, myRoleTypePsi, wantedRoleTypePsi );
84+
return TopicMapObjectRepository.getInstance().createInstances( theclass, topics );
85+
}
86+
6687
public void associateWithTopic( TopicMapObject otherTopic, String associationTypePsi, String myRolePsi, String otherRolePsi ) {
6788
new AssociationBuilder( tm ).createAssociation( associationTypePsi ).addRole( topic, myRolePsi ).addRole( otherTopic,
6889
otherRolePsi );
@@ -72,4 +93,13 @@ public TopicIF getTopic() {
7293
return topic;
7394
}
7495

96+
public String getDescription() {
97+
return getOccurrenceValue( "http://psi.ontopia.net/ontology/description" );
98+
}
99+
100+
@Override
101+
public String toString() {
102+
return getName();
103+
}
104+
75105
}

0 commit comments

Comments
 (0)