Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package coolmapplugin.util; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Set; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; /** * * @author Keqiang Li */ public class CMCyCommunicationUtil { public static final String TABLETYPE_NODE = "defaultnode"; public static List<Long> getSUIDForNames(String networkSUID, Set<String> nodeNames) { String jsonStringRows = CyRESTAPI.getTableRows(networkSUID, TABLETYPE_NODE); return iterRowAndMatchNodeName(jsonStringRows, nodeNames); } private static List<Long> iterRowAndMatchNodeName(String jsonStringRows, Set<String> nodeNames) { LinkedList<Long> matchedNodeSUIDs = new LinkedList<>(); try { JSONArray jsonRows = new JSONArray(jsonStringRows); for (int i = 0; i < jsonRows.length(); ++i) { JSONObject jsonRow = jsonRows.getJSONObject(i); String nodeName = jsonRow.getString("name"); if (nodeNames.contains(nodeName)) { matchedNodeSUIDs.add(jsonRow.getLong("SUID")); } } } catch (JSONException e) { return null; } return matchedNodeSUIDs; } /** * Convenient method to select nodes by SUID * * @param networkSUID in which network the nodes to be set * @param nodeSUIDs SUIDs of the nodes * @param isSelected select or deselect the nodes * @return if succeed in selecting */ public static boolean setNodesSelectedBySUIDs(String networkSUID, List<Object> nodeSUIDs, boolean isSelected) { return setNodesSelectedByColumn(networkSUID, "SUID", "SUID", nodeSUIDs, isSelected); } /** * Convenient method to select nodes by node names * * @param networkSUID in which network the nodes to be set * @param nodeNames names of the nodes * @param isSelected select or deselect the nodes * @return if succeed in selecting */ public static boolean setNodesSelectedByNodeNames(String networkSUID, List<Object> nodeNames, boolean isSelected) { return setNodesSelectedByColumn(networkSUID, "name", "name", nodeNames, isSelected); } /** * method used to select nodes by specifying which column will be used to * map to the input data key * * @param networkSUID networkSUID in which network the nodes to be set * @param key the key in the table used by mapping the dataKey * @param dataKey the key in the input data used by mapping the key * @param dataKeyValues the values for the data key * @param isSelected select or deselect the nodes * @return if succeed in selecting */ public static boolean setNodesSelectedByColumn(String networkSUID, String key, String dataKey, List<Object> dataKeyValues, boolean isSelected) { JSONArray jsonNodesToBeUpdated = new JSONArray(); for (Object dataKeyValue : dataKeyValues) { JSONObject nodeToBeUpdated = new JSONObject(); try { nodeToBeUpdated.put(dataKey, dataKeyValue); nodeToBeUpdated.put("selected", isSelected); jsonNodesToBeUpdated.put(nodeToBeUpdated); } catch (JSONException e) { return false; } } JSONObject jsonBody = new JSONObject(); try { jsonBody.put("data", jsonNodesToBeUpdated); jsonBody.put("dataKey", dataKey); jsonBody.put("key", key); } catch (JSONException e) { return false; } return CyRESTAPI.updateTableData(networkSUID, TABLETYPE_NODE, jsonBody.toString()); } /** * @return SUID of the current selected network */ public static Long getSelectedNetwork() { List<Long> selectedNetworks = CyRESTAPI.getNetworks("selected", "true"); if (selectedNetworks != null && selectedNetworks.size() >= 1) { return selectedNetworks.get(0); } return null; } /** * @param networkSUID SUID of the network from which to search all the * selected nodes * @return the list of SUIDs of the selected nodes */ public static List<Long> getSelectedNodes(String networkSUID) { List<Long> selectedNodeSUIDs = new LinkedList<>(); try { JSONArray allNodes = new JSONArray(CyRESTAPI.getTableRows(networkSUID, TABLETYPE_NODE)); for (int i = 0; i < allNodes.length(); ++i) { JSONObject node = allNodes.getJSONObject(i); if (node.getBoolean("selected")) { selectedNodeSUIDs.add(node.getLong("SUID")); } } } catch (JSONException e) { return null; } return selectedNodeSUIDs; } /** * @param networkSUID SUID of the network from which to search all the * selected nodes * @return the list of SUIDs of the selected nodes */ public static Set<String> getSelectedNodeNames(String networkSUID) { Set<String> selectedNodeNames = new HashSet<>(); try { JSONArray allNodes = new JSONArray(CyRESTAPI.getTableRows(networkSUID, TABLETYPE_NODE)); for (int i = 0; i < allNodes.length(); ++i) { JSONObject node = allNodes.getJSONObject(i); if (node.getBoolean("selected")) { selectedNodeNames.add(node.getString("name")); } } } catch (JSONException e) { return null; } return selectedNodeNames; } public static boolean addColumnToNodeTable(String networkSUID, HashMap<String, Double> selectedElements, String columnName) { try { JSONObject jsonBody = new JSONObject(); JSONArray jsonNodesToBeUpdated = new JSONArray(); for (String nodeName : selectedElements.keySet()) { JSONObject jsonNode = new JSONObject(); jsonNode.put("name", nodeName); jsonNode.put(columnName, selectedElements.get(nodeName)); jsonNodesToBeUpdated.put(jsonNode); } jsonBody.put("data", jsonNodesToBeUpdated); jsonBody.put("dataKey", "name"); jsonBody.put("key", "name"); return CyRESTAPI.updateTableData(networkSUID, TABLETYPE_NODE, jsonBody.toString()); } catch (JSONException e) { return false; } } }