Skip to content

Commit

Permalink
added first version of rest api for tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Luca Rossetto committed Sep 19, 2017
1 parent 6631056 commit 17f119e
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/org/vitrivr/cineast/api/rest/RestfulAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.vitrivr.cineast.api.rest.handlers.actions.FindSegmentAllByObjectIdActionHandler;
import org.vitrivr.cineast.api.rest.handlers.actions.FindSegmentSimilarActionHandler;
import org.vitrivr.cineast.api.rest.handlers.actions.FindSegmentsByIdActionHandler;
import org.vitrivr.cineast.api.rest.handlers.actions.FindTagsByActionHandler;
import org.vitrivr.cineast.api.rest.handlers.actions.StatusInvokationHandler;
import org.vitrivr.cineast.api.rest.handlers.actions.session.EndSessionHandler;
import org.vitrivr.cineast.api.rest.handlers.actions.session.StartSessionHandler;
Expand Down Expand Up @@ -153,12 +154,15 @@ public static void start() { //TODO check if already running
http.get("/objects/all/:type", new FindObjectAllActionHandler());

http.get("/segments/all/object/:id", new FindSegmentAllByObjectIdActionHandler());

http.get("/tags/by/:attribute/:value", new FindTagsByActionHandler(false));

http.post("/segments/similar", new FindSegmentSimilarActionHandler());

http.post("/segments/by/id", new FindSegmentsByIdActionHandler());
http.post("/objects/by/id", new FindObjectsByIdActionHandler());
http.post("/metas/by/id", new FindMetadatasByIdActionHandler());
http.post("/tags/by/id", new FindTagsByActionHandler(true));
});

http.path(makePath("session"), () -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.vitrivr.cineast.api.rest.handlers.actions;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.vitrivr.cineast.api.rest.exceptions.ActionHandlerException;
import org.vitrivr.cineast.api.rest.handlers.abstracts.ParsingActionHandler;
import org.vitrivr.cineast.core.data.entities.Tag;
import org.vitrivr.cineast.core.data.messages.lookup.IdList;
import org.vitrivr.cineast.core.db.dao.TagHandler;

public class FindTagsByActionHandler extends ParsingActionHandler<IdList> {

private static TagHandler tagHandler = new TagHandler();
private static final Logger LOGGER = LogManager.getLogger();


private static final String ATTRIBUTE_NAME = ":attribute";
private static final String VALUE_NAME = ":value";



private final boolean post;

public FindTagsByActionHandler(boolean post){
this.post = post;
}

@Override
public Object invoke(IdList context, Map<String, String> parameters)
throws ActionHandlerException {

if(post){

if(context == null || context.getIds().length == 0){
return Collections.emptyList();
}

return tagHandler.getTagsById(context.getIds());

}else{
String attribute = parameters.get(ATTRIBUTE_NAME);
String value = parameters.get(VALUE_NAME);

ArrayList<Tag> list = new ArrayList<>();

switch(attribute.toLowerCase()){
case "id":{
list.add(tagHandler.getTagById(value));
break;
}
case "name":{
list.addAll(tagHandler.getTagsByName(value));
break;
}
default:{
LOGGER.error("Unknown attribute '{}' in FindTagsByActionHandler", attribute);
}
}
return list;
}
}

@Override
public Class<IdList> inClass() {
return IdList.class;
}

}
19 changes: 19 additions & 0 deletions src/org/vitrivr/cineast/core/db/dao/TagHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,25 @@ public Tag getTagById(String id) {
return fromMap(rows.get(0));

}

public List<Tag> getTagsById(String... ids) {
if (ids == null) {
return null;
}
List<Map<String, PrimitiveTypeProvider>> rows = this.selector.getRows("id", ids);
if (rows.isEmpty()) {
return null;
}
ArrayList<Tag> _return = new ArrayList<>(rows.size());
for (Map<String, PrimitiveTypeProvider> row : rows) {
Tag t = fromMap(row);
if (t != null) {
_return.add(t);
}
}
return _return;

}

public List<Tag> getAll() {
return this.selector.getAll().stream().map(TagHandler::fromMap).filter(Objects::nonNull)
Expand Down

0 comments on commit 17f119e

Please sign in to comment.