Extend cross() function to take either a cell or a value #1204

This commit is contained in:
jackyq2015 2017-06-25 21:04:00 -04:00
parent a510145839
commit f03be76475
4 changed files with 33 additions and 8 deletions

View File

@ -88,6 +88,15 @@ public class InterProjectModel {
protected Map<String, ProjectJoin> _joins = new HashMap<String, ProjectJoin>();
/**
* Compute the ProjectJoin based on combination key, return the cached one from the HashMap if already computed
*
* @param fromProject
* @param fromColumn
* @param toProject
* @param toColumn
* @return
*/
public ProjectJoin getJoin(String fromProject, String fromColumn, String toProject, String toColumn) {
String key = fromProject + ";" + fromColumn + ";" + toProject + ";" + toColumn;
if (!_joins.containsKey(key)) {

View File

@ -47,39 +47,39 @@ import com.google.refine.grel.Function;
import com.google.refine.model.Project;
public class Cross implements Function {
@Override
public Object call(Properties bindings, Object[] args) {
if (args.length == 3) {
// from project is implied
Object wrappedCell = args[0]; // from cell
Object v = args[0]; // from cell
Object toProjectName = args[1];
Object toColumnName = args[2];
if (wrappedCell != null && wrappedCell instanceof WrappedCell &&
if (v != null && v instanceof String &&
toProjectName != null && toProjectName instanceof String &&
toColumnName != null && toColumnName instanceof String) {
ProjectJoin join = ProjectManager.singleton.getInterProjectModel().getJoin(
ProjectManager.singleton.getProjectMetadata(((Project) bindings.get("project")).id).getName(),
((WrappedCell) wrappedCell).columnName,
(String) bindings.get("columnName"),
(String) toProjectName,
(String) toColumnName
);
return join.getRows(((WrappedCell) wrappedCell).cell.value);
return join.getRows((String)v);
}
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a cell, a project name to join with, and a column name in that project");
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a string, a project name to join with, and a column name in that project");
}
@Override
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("TODO");
writer.key("description"); writer.value("join with anothe project by column");
writer.key("params"); writer.value("cell c, string projectName, string columnName");
writer.key("returns"); writer.value("array");
writer.endObject();

View File

@ -37,12 +37,14 @@ import java.io.IOException;
import java.io.LineNumberReader;
import java.io.Writer;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.google.refine.ProjectManager;
import com.google.refine.history.Change;
import com.google.refine.model.Cell;
import com.google.refine.model.Project;
@ -71,6 +73,7 @@ public class MassReconChange implements Change {
protected void switchRecons(Project project, Map<Long, Recon> reconMap) {
synchronized (project) {
HashSet<String> flushedColumn = new HashSet<String>();
for (Row row : project.rows) {
for (int c = 0; c < row.cells.size(); c++) {
Cell cell = row.cells.get(c);
@ -78,6 +81,14 @@ public class MassReconChange implements Change {
Recon recon = cell.recon;
if (reconMap.containsKey(recon.id)) {
// skip the flushing if already done
String columnName = project.columnModel.getColumnByCellIndex(c).getName();
if (!flushedColumn.contains(columnName)) {
ProjectManager.singleton.getInterProjectModel().flushJoinsInvolvingProjectColumn(project.id,
columnName);
flushedColumn.add(columnName);
}
row.setCell(c, new Cell(cell.value, reconMap.get(recon.id)));
}
}

View File

@ -41,6 +41,7 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import com.google.refine.ProjectManager;
import com.google.refine.history.Change;
import com.google.refine.model.Column;
import com.google.refine.model.ColumnGroup;
@ -80,6 +81,8 @@ public class MassRowColumnChange implements Change {
project.rows.clear();
project.rows.addAll(_newRows);
ProjectManager.singleton.getInterProjectModel().flushJoinsInvolvingProject(project.id);
project.update();
}
}
@ -96,6 +99,8 @@ public class MassRowColumnChange implements Change {
project.rows.clear();
project.rows.addAll(_oldRows);
ProjectManager.singleton.getInterProjectModel().flushJoinsInvolvingProject(project.id);
project.update();
}
}