From 1fb7600c4b87ba99bc99d96acc1a32a9d9f7260c Mon Sep 17 00:00:00 2001 From: Keiichiro Ono Date: Wed, 13 Aug 2014 14:42:14 -0700 Subject: [PATCH] Some more documents have been added to table API. --- .../rest/internal/datamapper/TableMapper.java | 3 +- .../internal/service/TableDataService.java | 178 +++++++++++++----- 2 files changed, 130 insertions(+), 51 deletions(-) diff --git a/src/main/java/org/cytoscape/rest/internal/datamapper/TableMapper.java b/src/main/java/org/cytoscape/rest/internal/datamapper/TableMapper.java index 05f48725..8f840fb7 100644 --- a/src/main/java/org/cytoscape/rest/internal/datamapper/TableMapper.java +++ b/src/main/java/org/cytoscape/rest/internal/datamapper/TableMapper.java @@ -10,6 +10,7 @@ import org.cytoscape.model.CyIdentifiable; import org.cytoscape.model.CyRow; import org.cytoscape.model.CyTable; +import org.cytoscape.rest.internal.service.JsonTags; import com.fasterxml.jackson.databind.JsonNode; @@ -41,7 +42,7 @@ public void createNewColumn(final JsonNode rootNode, final CyTable table) { // Optional: mutability boolean isImmutable = false; - final JsonNode immutable = rootNode.get("immutable"); + final JsonNode immutable = rootNode.get(JsonTags.COLUMN_IMMUTABLE); if(immutable != null) { isImmutable = immutable.asBoolean(); } diff --git a/src/main/java/org/cytoscape/rest/internal/service/TableDataService.java b/src/main/java/org/cytoscape/rest/internal/service/TableDataService.java index 1bf7cfc9..3006aea8 100644 --- a/src/main/java/org/cytoscape/rest/internal/service/TableDataService.java +++ b/src/main/java/org/cytoscape/rest/internal/service/TableDataService.java @@ -10,6 +10,7 @@ import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.GET; +import javax.ws.rs.NotFoundException; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; @@ -27,15 +28,25 @@ import org.cytoscape.rest.internal.datamapper.TableMapper; import org.cytoscape.rest.internal.serializer.CyTableSerializer; import org.cytoscape.rest.internal.serializer.TableModule; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +/** + * REST API for CyTable objects. This is for assigned table only. + * + * @author kono + * + */ @Singleton @Path("/v1/networks/{networkId}/tables") public class TableDataService extends AbstractDataService { + private final static Logger logger = LoggerFactory.getLogger(TableDataService.class); + private static enum TableType { DEFAULT_NODE("defaultnode"), DEFAULT_EDGE("defaultedge"), DEFAULT_NETWORK("defaultnetwork"); @@ -50,11 +61,9 @@ private String getType() { } } - private final TableMapper tableMapper; private final ObjectMapper tableObjectMapper; - public TableDataService() { super(); this.tableMapper = new TableMapper(); @@ -62,133 +71,176 @@ public TableDataService() { this.tableObjectMapper.registerModule(new TableModule()); } - /** - * Create new column to the target table. * - * @param id + * @title Create new column in the target table. + * + * This method creates a new, empty column in the specified table. + * + * @param networkId + * Network ID * @param tableType - * @param is + * Table type (defaultnode, defaultedge or defaultnetwork) + * */ @POST @Path("/{tableType}/columns") @Consumes(MediaType.APPLICATION_JSON) - public void createColumn(@PathParam("networkId") Long id, @PathParam("tableType") String tableType, final InputStream is) { - final CyNetwork network = getCyNetwork(id); + public void createColumn(@PathParam("networkId") Long networkId, @PathParam("tableType") String tableType, + final InputStream is) { + final CyNetwork network = getCyNetwork(networkId); final CyTable table = getTableByType(network, tableType); final ObjectMapper objMapper = new ObjectMapper(); try { final JsonNode rootNode = objMapper.readValue(is, JsonNode.class); tableMapper.createNewColumn(rootNode, table); } catch (IOException e) { - e.printStackTrace(); + logger.error("Failed to create column.", e); throw new WebApplicationException("Could not create column.", e, 500); } } - - /** - * Delete a column from a table. + * Delete a column from a table * - * @param id + * @param networkId + * Network SUID * @param tableType + * Table type (defaultnode, defaultedge or defaultnetwork) * @param columnName + * Name of the column to be deleted + * */ @DELETE @Path("/{tableType}/columns/{columnName}") - public void deleteColumn(@PathParam("networkId") Long id, @PathParam("tableType") String tableType, + public void deleteColumn(@PathParam("networkId") Long networkId, @PathParam("tableType") String tableType, @PathParam("columnName") String columnName) { - final CyNetwork network = getCyNetwork(id); + final CyNetwork network = getCyNetwork(networkId); final CyTable table = getTableByType(network, tableType); if (table != null) { table.deleteColumn(columnName); + } else { + logger.error("Failed to delete a column. (Missing table?)"); + throw new NotFoundException("Could not find the table. (This should not happen!)"); } } /** - * This if for update existing column name to a new one. + * @title Update a column name * - * @param id + * @param networkId + * Network SUID * @param tableType + * Table type (defaultnode, defaultedge or defaultnetwork) * @param columnName - * @param is + * Original name of the column to be updated. + * */ @PUT @Path("/{tableType}/columns") @Consumes(MediaType.APPLICATION_JSON) - public void updateColumnName(@PathParam("networkId") Long id, @PathParam("tableType") String tableType, + public void updateColumnName(@PathParam("networkId") Long networkId, @PathParam("tableType") String tableType, @PathParam("columnName") String columnName, final InputStream is) { - final CyNetwork network = getCyNetwork(id); + final CyNetwork network = getCyNetwork(networkId); final CyTable table = getTableByType(network, tableType); final ObjectMapper objMapper = new ObjectMapper(); try { final JsonNode rootNode = objMapper.readValue(is, JsonNode.class); tableMapper.updateColumnName(rootNode, table); } catch (IOException e) { - throw new WebApplicationException(e, 500); + logger.error("Could not parse the input JSON (new column name)", e); + throw new WebApplicationException("Could not parse the input JSON.", e, 412); } } - + /** + * @title Update a values in a column + * + * @param networkId + * Network SUID + * @param tableType + * Table type (defaultnode, defaultedge or defaultnetwork) + * @param columnName + * Target column name. + * + */ @PUT @Path("/{tableType}/columns/{columnName}") @Consumes(MediaType.APPLICATION_JSON) - public void createColumnValues(@PathParam("networkId") Long id, @PathParam("tableType") String tableType, + public void updateColumnValues(@PathParam("networkId") Long networkId, @PathParam("tableType") String tableType, @PathParam("columnName") String columnName, final InputStream is) { - final CyNetwork network = getCyNetwork(id); + final CyNetwork network = getCyNetwork(networkId); final CyTable table = getTableByType(network, tableType); - final ObjectMapper objMapper = new ObjectMapper(); try { final JsonNode rootNode = objMapper.readValue(is, JsonNode.class); tableMapper.updateColumnValues(rootNode, table, columnName); } catch (IOException e) { - e.printStackTrace(); - throw new WebApplicationException(e, 500); + logger.error("Could not read JSON for new column values.", e); + throw new WebApplicationException("Could not parse the input JSON.", e, 412); } } - + /** + * @title Update table data + * + * @param networkId + * Network SUID + * @param tableType + * Table type (defaultnode, defaultedge or defaultnetwork) + */ @PUT @Path("/{tableType}") @Consumes(MediaType.APPLICATION_JSON) - public void updateTable(@PathParam("networkId") Long id, @PathParam("tableType") String tableType, final InputStream is) { - final CyNetwork network = getCyNetwork(id); + public void updateTable(@PathParam("networkId") Long networkId, @PathParam("tableType") String tableType, + final InputStream is) { + final CyNetwork network = getCyNetwork(networkId); final CyTable table = getTableByType(network, tableType); - + final ObjectMapper objMapper = new ObjectMapper(); try { final JsonNode rootNode = objMapper.readValue(is, JsonNode.class); tableMapper.updateTableValues(rootNode, table); } catch (IOException e) { - e.printStackTrace(); - throw new WebApplicationException(e, 500); + logger.error("Could not parse JSON for new table entries.", e); + throw new WebApplicationException("Could not parse new table data JSON.", e, 412); } } - + /** + * @title Get a row in a table + * + * @param networkId + * Network SUID + * @param tableType + * Table type (defaultnode, defaultedge or defaultnetwork) + * @param primaryKey + * Name of primary key column + * + * @return Row in the table + */ @GET @Path("/{tableType}/rows/{primaryKey}") @Produces(MediaType.APPLICATION_JSON) - public String getRow(@PathParam("networkId") Long id, @PathParam("tableType") String tableType, + public String getRow(@PathParam("networkId") Long networkId, @PathParam("tableType") String tableType, @PathParam("primaryKey") Long primaryKey) { - final CyNetwork network = getCyNetwork(id); + final CyNetwork network = getCyNetwork(networkId); final CyTable table = getTableByType(network, tableType); final CyRow row = table.getRow(primaryKey); try { return this.serializer.serializeRow(row); } catch (IOException e) { - throw new WebApplicationException(e, 500); + logger.error("Copuld not serialize a table."); + throw new WebApplicationException("Could not serialize the table.", e, 500); } } @GET @Path("/{tableType}/rows/{primaryKey}/{columnName}") @Produces(MediaType.APPLICATION_JSON) - public String getCell(@PathParam("networkId") Long id, @PathParam("tableType") String tableType, + public String getCell(@PathParam("networkId") Long networkId, @PathParam("tableType") String tableType, @PathParam("primaryKey") Long primaryKey, @PathParam("columnName") String columnName) { - final CyNetwork network = getCyNetwork(id); + final CyNetwork network = getCyNetwork(networkId); final CyTable table = getTableByType(network, tableType); final CyRow row = table.getRow(primaryKey); try { @@ -201,8 +253,8 @@ public String getCell(@PathParam("networkId") Long id, @PathParam("tableType") S @GET @Path("/{tableType}/rows") @Produces(MediaType.APPLICATION_JSON) - public String getRows(@PathParam("networkId") Long id, @PathParam("tableType") String tableType) { - final CyNetwork network = getCyNetwork(id); + public String getRows(@PathParam("networkId") Long networkId, @PathParam("tableType") String tableType) { + final CyNetwork network = getCyNetwork(networkId); final CyTable table = getTableByType(network, tableType); try { return this.serializer.serializeAllRows(table.getAllRows()); @@ -214,8 +266,8 @@ public String getRows(@PathParam("networkId") Long id, @PathParam("tableType") S @GET @Path("/{tableType}/columns") @Produces(MediaType.APPLICATION_JSON) - public String getColumnNames(@PathParam("networkId") Long id, @PathParam("tableType") String tableType) { - final CyNetwork network = getCyNetwork(id); + public String getColumnNames(@PathParam("networkId") Long networkId, @PathParam("tableType") String tableType) { + final CyNetwork network = getCyNetwork(networkId); final CyTable table = getTableByType(network, tableType); try { return this.serializer.serializeColumns(table.getColumns()); @@ -224,13 +276,12 @@ public String getColumnNames(@PathParam("networkId") Long id, @PathParam("tableT } } - @GET @Path("/{tableType}/columns/{columnName}") @Produces(MediaType.APPLICATION_JSON) - public String getColumnValues(@PathParam("networkId") Long id, @PathParam("tableType") String tableType, + public String getColumnValues(@PathParam("networkId") Long networkId, @PathParam("tableType") String tableType, @PathParam("columnName") String columnName) { - final CyNetwork network = getCyNetwork(id); + final CyNetwork network = getCyNetwork(networkId); final CyTable table = getTableByType(network, tableType); final CyColumn column = table.getColumn(columnName); final List values = column.getValues(column.getType()); @@ -251,7 +302,7 @@ public String getColumnValues(@PathParam("networkId") Long id, @PathParam("table @GET @Path("/") @Produces(MediaType.APPLICATION_JSON) - public String getTables(@PathParam("networkId") Long id) { + public String getTables(@PathParam("networkId") Long networkId) { final Set tables = this.tableManager.getAllTables(true); try { @@ -294,14 +345,13 @@ private final List getGraphObjectsByType(final CyNetwo return objects; } - @GET @Path("/{tableType}") @Produces(MediaType.APPLICATION_JSON) - public String getTable(@PathParam("networkId") Long id, @PathParam("tableType") String tableType, + public String getTable(@PathParam("networkId") Long networkId, @PathParam("tableType") String tableType, @QueryParam(JsonTags.TABLE_FORMAT) String format) { - final CyNetwork network = getCyNetwork(id); + final CyNetwork network = getCyNetwork(networkId); final CyTable table = getTableByType(network, tableType); if (format != null && format.equals("csv")) { @@ -321,4 +371,32 @@ public String getTable(@PathParam("networkId") Long id, @PathParam("tableType") } } } + + /** + * @title Get a table as CSV + * + * @param networkId + * Network SUID + * @param tableType + * Table type (defaultnode, defaultedge or defaultnetwork) + * + * @return Entire table as CSV + * + */ + @GET + @Path("/{tableType}.csv") + @Produces(MediaType.APPLICATION_JSON) + public String getTableAsCsv(@PathParam("networkId") Long networkId, @PathParam("tableType") String tableType, + @QueryParam(JsonTags.TABLE_FORMAT) String format) { + + final CyNetwork network = getCyNetwork(networkId); + final CyTable table = getTableByType(network, tableType); + final CyTableSerializer tableSerializer = new CyTableSerializer(); + try { + final String result = tableSerializer.toCSV(table); + return result; + } catch (Exception e) { + throw new WebApplicationException("Could not serialize table to CSV.", e, 500); + } + } } \ No newline at end of file