comparison src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupMembersResource.java @ 24:e208a7b1a37a

more work on groups ui.
author casties
date Sun, 23 Sep 2012 16:28:05 +0200
parents d22d01ba953a
children 2140ef107551
comparison
equal deleted inserted replaced
23:d22d01ba953a 24:e208a7b1a37a
3 */ 3 */
4 package de.mpiwg.itgroup.annotations.restlet.annotations_ui; 4 package de.mpiwg.itgroup.annotations.restlet.annotations_ui;
5 5
6 import java.util.List; 6 import java.util.List;
7 7
8 import org.apache.log4j.Logger;
9 import org.restlet.data.Form; 8 import org.restlet.data.Form;
10 import org.restlet.data.MediaType; 9 import org.restlet.data.MediaType;
11 import org.restlet.data.Reference; 10 import org.restlet.data.Reference;
12 import org.restlet.data.Status; 11 import org.restlet.data.Status;
13 import org.restlet.representation.Representation; 12 import org.restlet.representation.Representation;
14 import org.restlet.representation.StringRepresentation; 13 import org.restlet.representation.StringRepresentation;
15 import org.restlet.resource.Get; 14 import org.restlet.resource.Get;
16 import org.restlet.resource.Post; 15 import org.restlet.resource.Post;
17 import org.restlet.resource.ServerResource;
18 16
19 import de.mpiwg.itgroup.annotations.Actor; 17 import de.mpiwg.itgroup.annotations.Actor;
20 import de.mpiwg.itgroup.annotations.Group;
21 import de.mpiwg.itgroup.annotations.Person; 18 import de.mpiwg.itgroup.annotations.Person;
22 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; 19 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore.NodeTypes;
23 import de.mpiwg.itgroup.annotations.restlet.BaseRestlet;
24 20
25 /** 21 /**
26 * Resource class for the members of an annotation group. 22 * Resource class for the members of an annotation group.
27 * 23 *
28 * @author casties 24 * @author casties
29 * 25 *
30 */ 26 */
31 public class GroupMembersResource extends ServerResource { 27 public class GroupMembersResource extends GroupResource {
32
33 public static Logger logger = Logger.getLogger(GroupMembersResource.class);
34
35 private AnnotationStore store;
36 28
37 /** 29 /**
38 * GET with HTML content type. Shows the members of the group. 30 * GET with HTML content type. Shows the members of the group.
39 * 31 *
40 * @param entity 32 * @param entity
41 * @return 33 * @return
42 */ 34 */
43 @Get("html") 35 @Get("html")
44 public Representation doGetHTML(Representation entity) { 36 public Representation doGetHTML(Representation entity) {
45 // id from URI /annotations/groups/{id}/members 37 // id from URI /annotations/groups/{id}/members
46 String id = (String) getRequest().getAttributes().get("id"); 38 if (requestId == null || requestId.isEmpty()) {
47 logger.debug("group-id=" + id);
48 if (id == null || id.isEmpty()) {
49 // invalid id 39 // invalid id
50 setStatus(Status.CLIENT_ERROR_BAD_REQUEST); 40 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
51 return null; 41 return null;
52 } 42 }
53 String result = null; 43 String result = null;
54 store = getAnnotationStore(); 44 Reference thisUrl = this.getReference();
55 Reference groupsUrl = this.getReference().getParentRef(); 45 Reference groupsUrl = thisUrl.getParentRef();
56 result = "<html><body>\n<h1>Members of group</h1>\n"; 46 result = "<html><body>\n<h1>Group members</h1>\n";
57 Group group = new Group(id);
58 group = (Group) store.getActor(group);
59 result += String.format("<p>Group: %s <a href=\"%s\">(%s)</a></p>\n", group.getName(), groupsUrl, group.getId()); 47 result += String.format("<p>Group: %s <a href=\"%s\">(%s)</a></p>\n", group.getName(), groupsUrl, group.getId());
60 result += "<p>Members:</p>\n"; 48 result += "<p>Members:</p>\n";
61 result += "<table>"; 49 result += "<table>";
62 List<Person> members = store.getMembersOfGroup(group); 50 List<Person> members = store.getMembersOfGroup(group);
63 for (Person p : members) { 51 for (Person p : members) {
64 result += String.format("<tr><td>%s</td><td>(%s)</td></tr>\n", p.getName(), p.getIdString()); 52 result += String.format("<tr><td>%s</td><td>(%s)</td>", p.getName(), p.getIdString());
53 //result += String.format("<td></td></tr>\n", p.getName(), p.getIdString());
65 } 54 }
66 result += "</table>\n"; 55 result += "</table>\n";
67 result += "<form method=\"post\" action=\"\">\n"; 56 result += String.format("<form method=\"post\" action=\"%s\">\n", thisUrl);
68 result += "<p>Add new member: <select name=\"new_member\">\n"; 57 result += "<p>Add new member: <select name=\"add_member\">\n";
69 result += String.format("<option value=\"%s\">%s</option>\n", "casties", "casties"); 58 for (Actor p : store.getActors("uri", "*", NodeTypes.PERSON)) {
59 result += String.format("<option value=\"%s\">%s</option>\n", p.getIdString(), p.getName());
60 }
70 result += "</select>\n"; 61 result += "</select>\n";
71 result += "<input type=\"submit\"/>\n"; 62 result += "<input type=\"submit\"/>\n";
72 result += "</form>\n"; 63 result += "</form>\n";
73 result += "</body>\n</html>"; 64 result += "</body>\n</html>";
74 65
75 return new StringRepresentation(result, MediaType.TEXT_HTML); 66 return new StringRepresentation(result, MediaType.TEXT_HTML);
76 } 67 }
77 68
78 /** 69 /**
79 * POST creates a new Group. 70 * POST adds or deletes members of the Group.
80 * 71 *
81 * @return 72 * @return
82 */ 73 */
83 @Post 74 @Post
84 public Representation doPost(Representation entity) { 75 public Representation doPost(Representation entity) {
85 logger.debug("AnnotationsUiGroup doPost!"); 76 logger.debug("GroupMembersResource doPost!");
86 // TODO: do authentication 77 // TODO: do authentication
87 Form form = new Form(entity); 78 Form form = new Form(entity);
88 String id = form.getFirstValue("id"); 79 String addMemberId = form.getFirstValue("add_member");
89 String name = form.getFirstValue("name"); 80 String delMemberId = form.getFirstValue("del_member");
90 if (id == null || id.isEmpty()) { 81 if (group == null || ((addMemberId == null || addMemberId.isEmpty())
91 // invalid id 82 && (delMemberId == null || delMemberId.isEmpty()))) {
83 // no id
92 setStatus(Status.CLIENT_ERROR_BAD_REQUEST); 84 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
93 return null; 85 return null;
94 } 86 }
95 String gid = null; 87
96 Group newGroup = new Group(gid, null, name);
97 store = getAnnotationStore();
98 Actor storedGroup = store.storeActor(newGroup);
99 gid = storedGroup.getId();
100 // return 303: see other 88 // return 303: see other
101 setStatus(Status.REDIRECTION_SEE_OTHER); 89 setStatus(Status.REDIRECTION_SEE_OTHER);
102 // go GET URL for this group 90 // go get same URL
103 Reference groupUrl = this.getReference(); 91 Reference thisUrl = this.getReference();
104 groupUrl.addSegment(gid); 92 this.getResponse().setLocationRef(thisUrl);
105 this.getResponse().setLocationRef(groupUrl);
106 return null; 93 return null;
107 } 94 }
108 95
109 protected AnnotationStore getAnnotationStore() {
110 if (store == null) {
111 store = ((BaseRestlet) getApplication()).getAnnotationStore();
112 }
113 return store;
114 }
115 } 96 }