source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/PersonsResource.java @ 45:707902d468f6

Last change on this file since 45:707902d468f6 was 45:707902d468f6, checked in by casties, 12 years ago

store reads and sends annotations resources now.

File size: 4.4 KB
Line 
1/**
2 *
3 */
4package de.mpiwg.itgroup.annotations.restlet.annotations_ui;
5
6import java.util.List;
7
8import org.apache.log4j.Logger;
9import org.restlet.data.Form;
10import org.restlet.data.MediaType;
11import org.restlet.data.Reference;
12import org.restlet.data.Status;
13import org.restlet.representation.Representation;
14import org.restlet.representation.StringRepresentation;
15import org.restlet.resource.Get;
16import org.restlet.resource.Post;
17import org.restlet.resource.ResourceException;
18import org.restlet.resource.ServerResource;
19
20import de.mpiwg.itgroup.annotations.Actor;
21import de.mpiwg.itgroup.annotations.Person;
22import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
23import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore.NodeTypes;
24import de.mpiwg.itgroup.annotations.restlet.BaseRestlet;
25
26/**
27 * Resource class for the list of annotation users (Person class).
28 *
29 * @author casties
30 *
31 */
32public class PersonsResource extends ServerResource {
33
34    public static Logger logger = Logger.getLogger(PersonsResource.class);
35
36    private AnnotationStore store;
37
38    @Override
39    protected void doInit() throws ResourceException {
40        super.doInit();
41        // get store instance
42        if (store == null) {
43            store = ((BaseRestlet) getApplication()).getAnnotationStore();
44        }
45    }
46
47    /**
48     * GET with HTML content type. Lists all persons.
49     *
50     * @param entity
51     * @return
52     */
53    @Get("html")
54    public Representation doGetHTML(Representation entity) {
55        String result = null;
56        // get form parameter
57        Form f = this.getQuery();
58        String form = f.getFirstValue("form");
59        if (form != null && form.equals("new_person")) {
60            // output new group form
61            result = "<html><body>\n";
62            result += "<h1>New person</h1>\n";
63            result += String.format("<p><a href=\"%s\">All persons</a></p>", this.getReference());
64            result += String.format("<form method=\"post\" action=\"%s\">\n", this.getReference().getHierarchicalPart());
65            result += "<table>";
66            result += "<tr><td><b>id</b></td><td><input type=\"text\" name=\"id\"/></td></tr>\n";
67            result += "<tr><td><b>name</b></td><td><input type=\"text\" name=\"name\"/></td></tr>\n";
68            result += "</table>\n";
69            result += "<p><input type=\"submit\"/></p>\n";
70            result += "</form>\n</body>\n</html>";
71        } else {
72            // list all groups
73            result = "<html><body>\n<h1>Persons</h1>\n<table>";
74            result += "<tr><th>id</th><th>name</th><th>uri</th></tr>";
75            List<Person> persons = store.getPersons("uri", "*");
76            for (Person person : persons) {
77                Reference url = this.getReference().clone();
78                url.addSegment(person.getId());
79                result += String.format("<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td></tr>\n", url,
80                        person.getIdString(), person.getName(), person.getUri());
81            }
82            result += "</table>\n";
83            result += "<p><a href=\"?form=new_person\">Add new person</a></p>\n";
84            result += "</body>\n</html>";
85        }
86        return new StringRepresentation(result, MediaType.TEXT_HTML);
87    }
88
89    /**
90     * POST creates a new Group.
91     *
92     * @return
93     */
94    @Post
95    public Representation doPost(Representation entity) {
96        logger.debug("PersonsResource doPost!");
97        // TODO: do authentication
98        Form form = new Form(entity);
99        String id = form.getFirstValue("id");
100        if (id == null || id.isEmpty() || id.matches("\\W")) {
101            // invalid id
102            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
103            return null;
104        }
105        String name = form.getFirstValue("name");
106        if (name == null || name.isEmpty()) {
107            name = ((BaseRestlet) getApplication()).getFullNameFromLdap(id);
108        }
109        String uri = form.getFirstValue("uri");
110        if (uri != null && uri.isEmpty()) uri = null;
111        Person newPerson = new Person(id, uri, name);
112        Actor storedPerson = store.storeActor(newPerson);
113        id = storedPerson.getId();
114        // return 303: see other
115        setStatus(Status.REDIRECTION_SEE_OTHER);
116        // go GET URL for this person
117        Reference url = this.getReference().clone();
118        url.addSegment(id);
119        this.getResponse().setLocationRef(url);
120        return null;
121    }
122
123}
Note: See TracBrowser for help on using the repository browser.