<commit_before>package org.cytoscape.task.internal.quickstart;

import java.util.List;

import javax.swing.JOptionPane;

import org.cytoscape.model.CyColumn;
import org.cytoscape.model.CyNetwork;
import org.cytoscape.model.CyRow;
import org.cytoscape.model.CyTable;
import org.cytoscape.model.CyTableEntry;
import org.cytoscape.work.AbstractTask;
import org.cytoscape.work.TaskMonitor;

public class MergeDataTask extends AbstractTask {
	
	private final QuickStartState state;
	private final ImportTaskUtil util;
	
	MergeDataTask(final QuickStartState state, final ImportTaskUtil util) {
		this.state = state;
		this.util = util;
	}

	@Override
	public void run(TaskMonitor taskMonitor) throws Exception {
		taskMonitor.setProgress(0.0);
		// Step 1: Create new column for the network.
		final IDType type = state.getIDType();
		if(type == null)
			throw new IllegalStateException("ID type is unknown.");
		
		final String columnName = type.getDisplayName();
		final CyTable table = state.getImportedTable();
		final CyNetwork net = util.getTargetNetwork();
		final CyTable nodeTable = net.getDefaultNodeTable();
		taskMonitor.setProgress(0.2);
		// "Copy" Name column to ID type name.
		final CyColumn pKey = table.getPrimaryKey();
		table.addVirtualColumn(columnName, pKey.getName(), table, pKey.getName(), false);
		taskMonitor.setProgress(0.5);
		nodeTable.addVirtualColumn(columnName, pKey.getName(), table, pKey.getName(), false);
				
		taskMonitor.setStatusMessage("Finished!");
		taskMonitor.setProgress(1.0);		
	}
	
	private String generateReport(final CyTable table, final String columnName) {

		final StringBuilder builder = new StringBuilder();
		
		builder.append("Data sets loaded:\n  Network: " + util.getTargetNetwork().getRow(util.getTargetNetwork()).get(CyTableEntry.NAME, String.class));
		builder.append("\n  Data Table: " + table.getTitle());
		builder.append("\n\n  ID Type: " + columnName);
		builder.append("\n  Matched entries: " + checkMatching(columnName, table.getColumn(columnName).getType()));
		
		// TODO: Should be done in mapping. 
		return builder.toString();
	}
	
	private int checkMatching(final String columnName, final Class<?> type) {
		final CyNetwork net = util.getTargetNetwork();
		final CyTable nodeTable = net.getDefaultNodeTable();
		
		final List<CyRow> rows = nodeTable.getAllRows();
		int matched = 0;
		
		for(CyRow row: rows) {
			Object val = row.get(columnName, type);
			if(val != null)
				matched++;
		}
		
		return matched;
	}<commit_msg>Fixed bug#810<commit_after>package org.cytoscape.task.internal.quickstart;

import java.util.List;

import javax.swing.JOptionPane;

import org.cytoscape.model.CyColumn;
import org.cytoscape.model.CyNetwork;
import org.cytoscape.model.CyRow;
import org.cytoscape.model.CyTable;
import org.cytoscape.model.CyIdentifiable;
import org.cytoscape.work.AbstractTask;
import org.cytoscape.work.TaskMonitor;

public class MergeDataTask extends AbstractTask {
	
	private final QuickStartState state;
	private final ImportTaskUtil util;
	
	MergeDataTask(final QuickStartState state, final ImportTaskUtil util) {
		this.state = state;
		this.util = util;
	}

	@Override
	public void run(TaskMonitor taskMonitor) throws Exception {
		taskMonitor.setProgress(0.0);
		// Step 1: Create new column for the network.
		final IDType type = state.getIDType();
		if(type == null)
			throw new IllegalStateException("ID type is unknown.");
		
		final String columnName = type.getDisplayName();
		final CyTable table = state.getImportedTable();
		final CyNetwork net = util.getTargetNetwork();
		final CyTable nodeTable = net.getDefaultNodeTable();
		taskMonitor.setProgress(0.2);
		// "Copy" Name column to ID type name.
		final CyColumn pKey = table.getPrimaryKey();
		table.addVirtualColumn(columnName, pKey.getName(), table, pKey.getName(), false);
		taskMonitor.setProgress(0.5);
		nodeTable.addVirtualColumn(columnName, pKey.getName(), table, pKey.getName(), false);
				
		taskMonitor.setStatusMessage("Finished!");
		taskMonitor.setProgress(1.0);		
	}
	
	private String generateReport(final CyTable table, final String columnName) {

		final StringBuilder builder = new StringBuilder();
		
		builder.append("Data sets loaded:\n  Network: " + util.getTargetNetwork().getRow(util.getTargetNetwork()).get(CyNetwork.NAME, String.class));
		builder.append("\n  Data Table: " + table.getTitle());
		builder.append("\n\n  ID Type: " + columnName);
		builder.append("\n  Matched entries: " + checkMatching(columnName, table.getColumn(columnName).getType()));
		
		// TODO: Should be done in mapping. 
		return builder.toString();
	}
	
	private int checkMatching(final String columnName, final Class<?> type) {
		final CyNetwork net = util.getTargetNetwork();
		final CyTable nodeTable = net.getDefaultNodeTable();
		
		final List<CyRow> rows = nodeTable.getAllRows();
		int matched = 0;
		
		for(CyRow row: rows) {
			Object val = row.get(columnName, type);
			if(val != null)
				matched++;
		}
		
		return matched;
	}
{'fraction_non_alphanumeric': 0.11751751751751752, 'fraction_numerical': 0.004604604604604604, 'mean_word_length': 4.65158371040724, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '263316', 'n_tokens_mistral': 1673, 'n_tokens_neox': 1491, 'n_words': 375}