Changeset 101:7268c3ca025b in AnnotationManagerN4J for src
- Timestamp:
- Feb 13, 2015, 5:10:11 PM (9 years ago)
- Branch:
- default
- Location:
- src/main/java/de/mpiwg/itgroup/annotations
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java
r98 r101 62 62 public class AnnotationStore { 63 63 64 protected static Logger logger = Logger.getLogger( "de.mpiwg.itgroup.annotations.neo4j.AnnotationStore");64 protected static Logger logger = Logger.getLogger(AnnotationStore.class.getCanonicalName()); 65 65 66 66 protected GraphDatabaseService graphDb; … … 257 257 258 258 /** 259 * Returns the total number of Annotations. 260 * 261 * @return number of annotations 262 */ 263 public int getAnnotationCount() { 264 int num = -1; 265 Index<Node> idx = getNodeIndex(NodeTypes.ANNOTATION); 266 try (Transaction tx = graphDb.beginTx()) { 267 IndexHits<Node> annotNodes = idx.query("id", "*"); 268 num = annotNodes.size(); 269 tx.success(); 270 } 271 return num; 272 } 273 274 /** 259 275 * Returns List of Annotations. Key has to be indexed. 260 276 * 261 277 * @param key 262 278 * @param query 263 * @return 264 */ 265 public List<Annotation> getAnnotations(String key, String query) { 279 * @param limit 280 * @param offset 281 * @return 282 */ 283 public List<Annotation> getAnnotations(String key, String query, int limit, int offset) { 284 long ts = System.currentTimeMillis(); 266 285 ArrayList<Annotation> annotations = new ArrayList<Annotation>(); 267 286 Index<Node> idx = getNodeIndex(NodeTypes.ANNOTATION); … … 272 291 try (Transaction tx = graphDb.beginTx()) { 273 292 IndexHits<Node> annotNodes = idx.query(key, query); 293 int cnt = 0; 294 int max = offset + limit; 274 295 for (Node annotNode : annotNodes) { 296 cnt += 1; 297 if (cnt < offset) continue; 275 298 Annotation annotation = createAnnotationFromNode(annotNode); 276 299 annotations.add(annotation); 277 } 278 tx.success(); 279 } 300 if (limit > 0 && cnt >= max) { 301 annotNodes.close(); 302 break; 303 } 304 } 305 tx.success(); 306 } 307 logger.finer("got "+annotations.size()+" annotations in "+(System.currentTimeMillis()-ts)+"ms"); 280 308 return annotations; 281 309 } -
src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java
r91 r101 85 85 // no id -- send all annotations 86 86 Form form = getRequest().getResourceRef().getQueryAsForm(); 87 int limit = getInt(form.getFirstValue("limit" ));88 int offset = getInt(form.getFirstValue("offset" ));87 int limit = getInt(form.getFirstValue("limit", "1000")); 88 int offset = getInt(form.getFirstValue("offset", "0")); 89 89 String sortBy = form.getFirstValue("sortBy"); 90 90 return getAllAnnotations(authUser, limit, offset, sortBy); … … 113 113 114 114 // read all annotations 115 List<Annotation> annotations = store.getAnnotations(null, null );115 List<Annotation> annotations = store.getAnnotations(null, null, 0, 0); 116 116 for (Annotation annotation : annotations) { 117 117 // check permission … … 131 131 JSONArray rows = new JSONArray(); 132 132 int cnt = 0; 133 int max = limit + offset; 133 134 for (JSONObject result : results) { 134 135 cnt += 1; 135 if (offset > 0 && cnt < offset) 136 continue; 136 if (cnt < offset) continue; 137 137 rows.put(result); 138 if (limit > 0 && cnt >= limit) 139 break; 138 if (limit > 0 && cnt >= max) break; 140 139 } 141 140 -
src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/AnnotationsResource.java
r99 r101 29 29 import java.util.logging.Logger; 30 30 31 import org.restlet.data.Form; 31 32 import org.restlet.data.MediaType; 32 33 import org.restlet.data.Reference; … … 39 40 import de.mpiwg.itgroup.annotations.Annotation; 40 41 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; 42 import de.mpiwg.itgroup.annotations.restlet.AnnotatorResourceImpl; 41 43 import de.mpiwg.itgroup.annotations.restlet.BaseRestlet; 42 44 … … 70 72 @Get("html") 71 73 public Representation doGetHTML(Representation entity) { 72 String result = null; 74 Form form = getRequest().getResourceRef().getQueryAsForm(); 75 int limit = AnnotatorResourceImpl.getInt(form.getFirstValue("limit", "100")); 76 int offset = AnnotatorResourceImpl.getInt(form.getFirstValue("offset", "0")); 77 int max = offset + limit; 78 int numannots = store.getAnnotationCount(); 79 String baseUrl = this.getReference().getHierarchicalPart(); 80 StringBuilder result = null; 73 81 // list all annotations 74 result = "<html><body>\n<h1>Annotations</h1>\n<table>"; 75 result += "<tr><th>uri</th><th>text</th><th>target</th><th>fragment</th><th>creator</th></tr>"; 76 List<Annotation> annotations = store.getAnnotations("id", "*"); 82 result = new StringBuilder("<html><body>\n<h1>Annotations</h1>\n"); 83 result.append("<p>"); 84 if (offset > 0) { 85 result.append(String.format(" <a href=\"%s?offset=%s&limit=%s\">prev</a> ", baseUrl, Math.max(offset - limit, 0), limit)); 86 } 87 result.append(String.format("| %s - %s of %s annotations |", offset, (offset + limit), numannots)); 88 if (max < numannots) { 89 result.append(String.format(" <a href=\"%s?offset=%s&limit=%s\">next</a>", baseUrl, Math.min(max, numannots), limit)); 90 } 91 result.append("</p>\n"); 92 result.append("<table>"); 93 result.append("<tr><th>uri</th><th>text</th><th>target</th><th>fragment</th><th>creator</th></tr>"); 94 List<Annotation> annotations = store.getAnnotations("id", "*", limit, offset); 77 95 for (Annotation annotation : annotations) { 78 96 Reference url = this.getReference().clone(); 79 97 url.addSegment(annotation.getUrlId()); 80 result +=String.format("<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td></tr>\n", url,98 result.append(String.format("<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td></tr>\n", url, 81 99 annotation.getUri(), annotation.getBodyText(), annotation.getTargetBaseUri(), annotation.getTargetBaseUri(), 82 annotation.getTargetFragment(), annotation.getCreatorName()) ;100 annotation.getTargetFragment(), annotation.getCreatorName())); 83 101 } 84 result += "</table>\n";85 result += "</body>\n</html>";102 result.append("</table>\n"); 103 result.append("</body>\n</html>"); 86 104 return new StringRepresentation(result, MediaType.TEXT_HTML); 87 105 }
Note: See TracChangeset
for help on using the changeset viewer.