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

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