Search in sources :

Example 11 with JSONResult

use of org.cytoscape.work.json.JSONResult in project cytoscape-impl by cytoscape.

the class GetEdgeAttributeTask method getResults.

public Object getResults(Class requestedType) {
    if (requestedType.equals(String.class)) {
        return DataUtils.convertMapToString(edgeDataMap);
    } else if (requestedType.equals(JSONResult.class)) {
        JSONResult res = () -> {
            if (edgeDataMap == null)
                return "[]";
            else {
                StringBuilder output = new StringBuilder("[");
                CyJSONUtil cyJSONUtil = serviceRegistrar.getService(CyJSONUtil.class);
                List<CyColumn> cyColumn = columnTunable.getColumnList(edgeTable);
                CyColumn[] cyColumnArray = cyColumn.size() > 0 ? cyColumn.toArray(new CyColumn[0]) : new CyColumn[] {};
                int count = edgeDataMap.size();
                for (CyIdentifiable edge : edgeDataMap.keySet()) {
                    CyRow row = edgeTable.getRow(edge.getSUID());
                    output.append(" " + cyJSONUtil.toJson(row, cyColumnArray));
                    if (count > 1) {
                        output.append(",\n");
                    }
                    count--;
                }
                output.append("\n]");
                return output.toString();
            }
        };
        return res;
    }
    return edgeDataMap;
}
Also used : JSONResult(org.cytoscape.work.json.JSONResult) CyColumn(org.cytoscape.model.CyColumn) CyRow(org.cytoscape.model.CyRow) CyJSONUtil(org.cytoscape.util.json.CyJSONUtil) CyIdentifiable(org.cytoscape.model.CyIdentifiable)

Example 12 with JSONResult

use of org.cytoscape.work.json.JSONResult in project cytoscape-impl by cytoscape.

the class ListAvailableTask method getResults.

@SuppressWarnings({ "unchecked" })
@Override
public <R> R getResults(Class<? extends R> type) {
    if (type.equals(JSONResult.class)) {
        JSONResult res = () -> {
            StringBuilder stringBuilder = new StringBuilder("[");
            int count = webAppList.size();
            int index = 0;
            for (WebApp app : webAppList) {
                stringBuilder.append("{\"appName\": \"" + app.getName() + "\",");
                stringBuilder.append("\"description\": " + Utils.quote(app.getDescription()) + ",");
                if (app.getDetails() != null)
                    stringBuilder.append("\"details\": " + Utils.quote(app.getDetails()) + "}");
                else
                    stringBuilder.append("\"details\": \"\"}");
                index++;
                if (index < count)
                    stringBuilder.append(",");
            }
            stringBuilder.append("]");
            // System.out.println(stringBuilder.toString());
            return stringBuilder.toString();
        };
        return (R) res;
    } else if (type.equals(String.class)) {
        List<String> appList = new ArrayList<String>(webAppList.size());
        Map<String, WebApp> appMap = new HashMap<String, WebApp>();
        for (WebApp app : webAppList) {
            appList.add(app.getFullName().toLowerCase());
            appMap.put(app.getFullName().toLowerCase(), app);
        }
        Collections.sort(appList);
        String list = "";
        for (String app : appList) {
            WebApp thisApp = appMap.get(app);
            list += "name: " + thisApp.getFullName() + ", version: " + getVersion(thisApp) + "\n";
        }
        return (R) list;
    } else if (type.equals(List.class)) {
        List<String> list = new ArrayList<>();
        for (WebApp app : webAppList) {
            list.add(app.getFullName());
        }
        return (R) list;
    }
    return null;
}
Also used : JSONResult(org.cytoscape.work.json.JSONResult) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) WebApp(org.cytoscape.app.internal.net.WebApp)

Example 13 with JSONResult

use of org.cytoscape.work.json.JSONResult in project cytoscape-impl by cytoscape.

the class AddTask method getResults.

@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public Object getResults(Class type) {
    List<CyIdentifiable> identifiables = new ArrayList();
    if (nodeList != null)
        identifiables.addAll(nodeList);
    if (edgeList != null)
        identifiables.addAll(edgeList);
    if (type.equals(List.class)) {
        return identifiables;
    } else if (type.equals(String.class)) {
        if (identifiables.size() == 0)
            return "<none>";
        String ret = "";
        if (nodeList != null && nodeList.size() > 0) {
            ret += "Nodes added: \n";
            for (CyNode node : nodeList) {
                ret += "   " + network.getRow(node).get(CyNetwork.NAME, String.class) + "\n";
            }
        }
        if (edgeList != null && edgeList.size() > 0) {
            ret += "Edges added: \n";
            for (CyEdge edge : edgeList) {
                ret += "   " + network.getRow(edge).get(CyNetwork.NAME, String.class) + "\n";
            }
        }
        return ret;
    } else if (type.equals(JSONResult.class)) {
        JSONResult res = () -> {
            if (identifiables == null || identifiables.size() == 0)
                return "{}";
            else {
                CyJSONUtil cyJSONUtil = serviceRegistrar.getService(CyJSONUtil.class);
                String result = "{\"nodes\":";
                if (nodeList == null || nodeList.size() == 0)
                    result += "[]";
                else
                    result += cyJSONUtil.cyIdentifiablesToJson(nodeList);
                result += ", \"edges\":";
                if (edgeList == null || edgeList.size() == 0)
                    result += "[]";
                else
                    result += cyJSONUtil.cyIdentifiablesToJson(edgeList);
                result += "}";
                return result;
            }
        };
        return res;
    }
    return identifiables;
}
Also used : JSONResult(org.cytoscape.work.json.JSONResult) ArrayList(java.util.ArrayList) CyNode(org.cytoscape.model.CyNode) CyEdge(org.cytoscape.model.CyEdge) CyIdentifiable(org.cytoscape.model.CyIdentifiable) CyJSONUtil(org.cytoscape.util.json.CyJSONUtil)

Example 14 with JSONResult

use of org.cytoscape.work.json.JSONResult in project cytoscape-impl by cytoscape.

the class RegisterNetworkTask method getResults.

@Override
public Object getResults(Class type) {
    if (type.equals(List.class)) {
        return views;
    } else if (type.equals(String.class)) {
        if (networks == null)
            return null;
        String res = "";
        if (views != null && views.size() > 0) {
            res += "Views:\n";
            for (CyNetworkView view : views) {
                res += "    " + DataUtils.getViewTitle(view) + " (SUID: " + view.getSUID() + ")" + "\n";
            }
        }
        res += "Networks:\n";
        for (CyNetwork network : networks) {
            res += "    " + DataUtils.getNetworkName(network) + " (SUID: " + network.getSUID() + ")" + "\n";
        }
        return res;
    } else if (type.equals(CyNetwork.class))
        return networks.get(0);
    else if (type.equals(CyNetworkView.class)) {
        if (views != null && views.size() > 0)
            return views.get(0);
        else
            return null;
    } else if (type.equals(JSONResult.class)) {
        JSONResult res = () -> {
            if (networks == null)
                return "{}";
            else if (singleton) {
                // Special case single network
                CyNetwork network = networks.get(0);
                CyNetworkView view = null;
                if (views.size() == 1)
                    view = views.get(0);
                String strRes = jsonNetView(network, view);
                return jsonNetView(network, view);
            } else {
                String strRes = "[";
                Set<CyNetwork> viewNets = new HashSet<>();
                boolean first = true;
                for (CyNetworkView view : views) {
                    CyNetwork net = view.getModel();
                    viewNets.add(net);
                    if (!first)
                        strRes += ",";
                    else
                        first = false;
                    strRes += jsonNetView(net, view);
                }
                for (CyNetwork net : networks) {
                    if (!viewNets.contains(net)) {
                        strRes += jsonNetView(net, null);
                    }
                }
                strRes += "]";
                return strRes;
            }
        };
        return res;
    }
    return null;
}
Also used : JSONResult(org.cytoscape.work.json.JSONResult) CyNetwork(org.cytoscape.model.CyNetwork) CyNetworkView(org.cytoscape.view.model.CyNetworkView) HashSet(java.util.HashSet)

Example 15 with JSONResult

use of org.cytoscape.work.json.JSONResult in project cytoscape-impl by cytoscape.

the class DeselectTask method getResults.

public Object getResults(Class type) {
    List<CyIdentifiable> identifiables = new ArrayList<>();
    if (deselectedNodes != null && deselectedNodes.size() > 0) {
        identifiables.addAll(deselectedNodes);
    }
    if (deselectedEdges != null && deselectedEdges.size() > 0) {
        identifiables.addAll(deselectedEdges);
    }
    if (type.equals(List.class)) {
        return identifiables;
    } else if (type.equals(String.class)) {
        if (identifiables.size() == 0)
            return "<none>";
        String ret = "";
        if (deselectedNodes != null && deselectedNodes.size() > 0) {
            ret += "Nodes deselected: \n";
            for (CyNode node : deselectedNodes) {
                ret += "   " + network.getRow(node).get(CyNetwork.NAME, String.class) + "\n";
            }
        }
        if (deselectedEdges != null && deselectedEdges.size() > 0) {
            ret += "Edges deselected: \n";
            for (CyEdge edge : deselectedEdges) {
                ret += "   " + network.getRow(edge).get(CyNetwork.NAME, String.class) + "\n";
            }
        }
        return ret;
    } else if (type.equals(JSONResult.class)) {
        JSONResult res = () -> {
            if (identifiables == null || identifiables.size() == 0)
                return "{}";
            else {
                String result = "{\"nodes\":";
                if (deselectedNodes == null || deselectedNodes.size() == 0)
                    result += "[]";
                else
                    result += cyJSONUtil.cyIdentifiablesToJson(deselectedNodes);
                result += ", \"edges\":";
                if (deselectedEdges == null || deselectedEdges.size() == 0)
                    result += "[]";
                else
                    result += cyJSONUtil.cyIdentifiablesToJson(deselectedEdges);
                return result + "}";
            }
        };
        return res;
    }
    return identifiables;
}
Also used : JSONResult(org.cytoscape.work.json.JSONResult) ArrayList(java.util.ArrayList) CyNode(org.cytoscape.model.CyNode) CyEdge(org.cytoscape.model.CyEdge) CyIdentifiable(org.cytoscape.model.CyIdentifiable)

Aggregations

JSONResult (org.cytoscape.work.json.JSONResult)19 CyJSONUtil (org.cytoscape.util.json.CyJSONUtil)12 ArrayList (java.util.ArrayList)7 CyIdentifiable (org.cytoscape.model.CyIdentifiable)7 CyEdge (org.cytoscape.model.CyEdge)5 CyNode (org.cytoscape.model.CyNode)5 CyColumn (org.cytoscape.model.CyColumn)3 CyNetwork (org.cytoscape.model.CyNetwork)3 CyRow (org.cytoscape.model.CyRow)3 CyNetworkView (org.cytoscape.view.model.CyNetworkView)3 Gson (com.google.gson.Gson)2 JsonObject (com.google.gson.JsonObject)2 JsonArray (com.google.gson.JsonArray)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 WebApp (org.cytoscape.app.internal.net.WebApp)1 CyTable (org.cytoscape.model.CyTable)1