source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorSearch.java @ 63:9f8c9611848a

Last change on this file since 63:9f8c9611848a was 63:9f8c9611848a, checked in by casties, 11 years ago

fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.

File size: 3.3 KB
Line 
1/**
2 * Implements the "search" uri of the Annotator API.
3 */
4package de.mpiwg.itgroup.annotations.restlet;
5
6import java.util.ArrayList;
7import java.util.List;
8
9import org.json.JSONArray;
10import org.json.JSONException;
11import org.json.JSONObject;
12import org.restlet.data.Form;
13import org.restlet.data.Status;
14import org.restlet.ext.json.JsonRepresentation;
15import org.restlet.representation.Representation;
16import org.restlet.resource.Get;
17
18import de.mpiwg.itgroup.annotations.Annotation;
19import de.mpiwg.itgroup.annotations.Person;
20import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
21import de.mpiwg.itgroup.annotations.restlet.utils.JSONObjectComparator;
22
23/**
24 * Implements the "search" uri of the Annotator API. see
25 * <https://github.com/okfn/annotator/wiki/Storage>
26 *
27 * @author casties
28 *
29 */
30public class AnnotatorSearch extends AnnotatorResourceImpl {
31
32    protected String getAllowedMethodsForHeader() {
33        return "OPTIONS,GET";
34    }
35
36    /**
37     * result for JSON content-type. optional search parameters: uri, user, limit,
38     * offset, sortBy.
39     *
40     * @param entity
41     * @return
42     */
43    @Get("json")
44    public Representation doGetJSON(Representation entity) {
45        logger.debug("AnnotatorSearch doGetJSON!");
46        setCorsHeaders();
47        // do authentication
48        Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));
49        logger.debug("request authenticated=" + authUser);
50
51        Form form = getRequest().getResourceRef().getQueryAsForm();
52        String uri = form.getFirstValue("uri");
53        String user = form.getFirstValue("user");
54        int limit = getInt(form.getFirstValue("limit"));
55        int offset = getInt(form.getFirstValue("offset"));
56        String sortBy = form.getFirstValue("sortBy");
57
58        // do search
59        ArrayList<JSONObject> results = new ArrayList<JSONObject>();
60        logger.debug(String.format("searching for uri=%s user=%s", uri, user));
61        AnnotationStore store = getAnnotationStore();
62        List<Annotation> annots = store.searchAnnotationByUriUser(uri, user);
63        for (Annotation annot : annots) {
64            // check permission
65            if (!annot.isActionAllowed("read", authUser, store)) continue;
66            JSONObject jo = createAnnotatorJson(annot, (authUser == null));
67            if (jo != null) {
68                results.add(jo);
69            } else {
70                setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
71                return null;
72            }
73        }
74
75        // sort if necessary
76        if (sortBy != null) {
77            JSONObjectComparator.sortAnnotations(results, sortBy);
78        }
79       
80        // put in JSON list
81        JSONArray rows = new JSONArray();
82        int cnt = 0;
83        for (JSONObject result : results) {
84            cnt += 1;
85            if (offset > 0 && cnt < offset) continue;
86            rows.put(result);
87            if (limit > 0 && cnt >= limit) break;
88        }
89
90        // assemble result object
91        JSONObject result = new JSONObject();
92        try {
93            result.put("rows", rows);
94            result.put("total", rows.length());
95        } catch (JSONException e) {
96            setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
97            return null;
98        }
99
100        logger.debug("sending:");
101        logger.debug(result);
102        return new JsonRepresentation(result);
103    }
104
105}
Note: See TracBrowser for help on using the repository browser.