18
|
1 /**
|
|
2 *
|
|
3 */
|
|
4 package de.mpiwg.itgroup.annotations.restlet;
|
|
5
|
|
6 import java.util.List;
|
|
7
|
|
8 import org.apache.log4j.Logger;
|
|
9 import org.restlet.data.Form;
|
|
10 import org.restlet.data.MediaType;
|
22
|
11 import org.restlet.data.Reference;
|
|
12 import org.restlet.data.Status;
|
18
|
13 import org.restlet.representation.Representation;
|
|
14 import org.restlet.representation.StringRepresentation;
|
|
15 import org.restlet.resource.Get;
|
22
|
16 import org.restlet.resource.Post;
|
18
|
17 import org.restlet.resource.ServerResource;
|
|
18
|
22
|
19 import de.mpiwg.itgroup.annotations.Actor;
|
18
|
20 import de.mpiwg.itgroup.annotations.Group;
|
22
|
21 import de.mpiwg.itgroup.annotations.Person;
|
18
|
22 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
|
|
23
|
|
24 /**
|
|
25 * @author casties
|
22
|
26 *
|
18
|
27 */
|
|
28 public class AnnotationStoreGroups extends ServerResource {
|
|
29
|
|
30 public static Logger logger = Logger.getLogger(AnnotationStoreGroups.class);
|
|
31
|
|
32 private AnnotationStore store;
|
|
33
|
22
|
34 /**
|
|
35 * GET with HTML content type. Lists all groups.
|
|
36 *
|
|
37 * @param entity
|
|
38 * @return
|
|
39 */
|
18
|
40 @Get("html")
|
22
|
41 public Representation doGetHTML(Representation entity) {
|
18
|
42 // id from URI /annotations/groups/{id}
|
|
43 String id = (String) getRequest().getAttributes().get("id");
|
|
44 logger.debug("group-id=" + id);
|
22
|
45 String result = null;
|
18
|
46 store = getAnnotationStore();
|
|
47 if (id == null) {
|
|
48 // list all groups
|
22
|
49 result = "<html><body>\n<h1>Groups</h1>\n<table>";
|
|
50 result += "<tr><th>id</th><th>name</th><th>uri</th></tr>";
|
21
|
51 List<Group> groups = store.getGroups("uri", "*");
|
|
52 for (Group group : groups) {
|
22
|
53 Reference groupUrl = this.getReference();
|
|
54 groupUrl.addSegment(group.getId());
|
|
55 result += String.format("<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td></tr>\n", groupUrl,
|
|
56 group.getId(), group.getName(), group.getUri());
|
21
|
57 }
|
22
|
58 result += "</table>\n</body>\n</html>";
|
|
59 } else {
|
|
60 // just one group
|
|
61 Reference groupsUrl = this.getReference().getParentRef();
|
|
62 result = "<html><body>\n<h1>Group</h1>\n";
|
|
63 result += String.format("<p><a href=\"%s\">All groups</a></p>", groupsUrl);
|
|
64 Group group = new Group(id);
|
|
65 group = (Group) store.getActor(group);
|
|
66 result += "<table>";
|
|
67 result += String.format("<tr><td><b>id</b></td><td>%s</td></tr>\n", group.getId());
|
|
68 result += String.format("<tr><td><b>name</b></td><td>%s</td></tr>\n", group.getName());
|
|
69 result += String.format("<tr><td><b>uri</b></td><td>%s</td></tr>\n", group.getUri());
|
|
70 result += "<tr><td><b>members</b></td><td>";
|
|
71 List<Person> members = store.getMembersOfGroup(group);
|
|
72 for (Person p : members) {
|
|
73 result += String.format("%s (%s)\n", p.getName(), p.getIdString());
|
|
74 }
|
|
75 result += "</td></tr>\n";
|
|
76 result += "</table>\n</body>\n</html>";
|
18
|
77 }
|
22
|
78
|
18
|
79 logger.debug("sending:");
|
19
|
80 logger.debug(result);
|
22
|
81 return new StringRepresentation(result, MediaType.TEXT_HTML);
|
|
82 }
|
|
83
|
|
84 /**
|
|
85 * POST with HTML content-type. Creates a new Group.
|
|
86 *
|
|
87 * @return
|
|
88 */
|
|
89 @Post
|
|
90 public Representation doPostHTML(Representation entity) {
|
|
91 logger.debug("AnnotationStoreGroups doPostHTML!");
|
|
92 // TODO: do authentication
|
|
93 Form form = new Form(entity);
|
|
94 String id = form.getFirstValue("id");
|
|
95 String name = form.getFirstValue("name");
|
|
96 if (id == null || id.isEmpty()) {
|
|
97 // invalid id
|
|
98 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
|
|
99 return null;
|
|
100 }
|
|
101 String gid = makeGroupId(id);
|
|
102 Group newGroup = new Group(gid, null, name);
|
|
103 store = getAnnotationStore();
|
|
104 Actor storedGroup = store.storeActor(newGroup);
|
|
105 gid = storedGroup.getId();
|
|
106 // return 303: see other
|
|
107 setStatus(Status.REDIRECTION_SEE_OTHER);
|
|
108 // go GET URL for this group
|
|
109 Reference groupUrl = this.getReference();
|
|
110 groupUrl.addSegment(gid);
|
|
111 this.getResponse().setLocationRef(groupUrl);
|
|
112 return null;
|
|
113 }
|
|
114
|
|
115 /**
|
|
116 * Returns a group id based on the given id.
|
|
117 *
|
|
118 * @param id
|
|
119 * @return
|
|
120 */
|
|
121 protected String makeGroupId(String id) {
|
|
122 // TODO: should we use different ids?
|
|
123 id = id.replaceAll("\\W", "_");
|
|
124 id = id.toLowerCase();
|
|
125 return id;
|
18
|
126 }
|
|
127
|
|
128 protected AnnotationStore getAnnotationStore() {
|
|
129 if (store == null) {
|
19
|
130 store = ((BaseRestlet) getApplication()).getAnnotationStore();
|
18
|
131 }
|
|
132 return store;
|
|
133 }
|
|
134 }
|