Search in sources :

Example 1 with LookupException

use of com.google.refine.util.LookupException in project OpenRefine by OpenRefine.

the class Cross method call.

@Override
public Object call(Properties bindings, Object[] args) {
    if (1 <= args.length && args.length <= 3) {
        // 1st argument can take either value or cell(for backward compatibility)
        Object v = args[0];
        // if 2nd argument is omitted or set to "", use the current project name
        Object targetProjectName = "";
        boolean isCurrentProject = false;
        if (args.length < 2 || args[1].equals("")) {
            isCurrentProject = true;
        } else {
            targetProjectName = args[1];
        }
        // if 3rd argument is omitted or set to "", use the index column
        Object targetColumnName = args.length < 3 || args[2].equals("") ? INDEX_COLUMN_NAME : args[2];
        long targetProjectID;
        ProjectLookup lookup;
        if (v != null && targetProjectName instanceof String && targetColumnName instanceof String) {
            try {
                targetProjectID = isCurrentProject ? ((Project) bindings.get("project")).id : ProjectManager.singleton.getProjectID((String) targetProjectName);
            } catch (GetProjectIDException e) {
                return new EvalError(e.getMessage());
            }
            try {
                lookup = ProjectManager.singleton.getLookupCacheManager().getLookup(targetProjectID, (String) targetColumnName);
            } catch (LookupException e) {
                return new EvalError(e.getMessage());
            }
            if (v instanceof WrappedCell) {
                return lookup.getRows(((WrappedCell) v).cell.value);
            } else {
                return lookup.getRows(v);
            }
        }
    }
    return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a cell or value, a project name to look up (optional), and a column name in that project (optional)");
}
Also used : Project(com.google.refine.model.Project) LookupException(com.google.refine.util.LookupException) ProjectLookup(com.google.refine.LookupCacheManager.ProjectLookup) WrappedCell(com.google.refine.expr.WrappedCell) GetProjectIDException(com.google.refine.util.GetProjectIDException) EvalError(com.google.refine.expr.EvalError)

Example 2 with LookupException

use of com.google.refine.util.LookupException in project OpenRefine by OpenRefine.

the class LookupCacheManager method computeLookup.

protected void computeLookup(ProjectLookup lookup) throws LookupException {
    if (lookup.targetProjectID < 0) {
        return;
    }
    Project targetProject = ProjectManager.singleton.getProject(lookup.targetProjectID);
    ProjectMetadata targetProjectMetadata = ProjectManager.singleton.getProjectMetadata(lookup.targetProjectID);
    if (targetProject == null) {
        return;
    }
    // if this is a lookup on the index column
    if (lookup.targetColumnName.equals(Cross.INDEX_COLUMN_NAME)) {
        for (int r = 0; r < targetProject.rows.size(); r++) {
            lookup.valueToRowIndices.put(String.valueOf(r), Collections.singletonList(r));
        }
        // return directly
        return;
    }
    Column targetColumn = targetProject.columnModel.getColumnByName(lookup.targetColumnName);
    if (targetColumn == null) {
        throw new LookupException("Unable to find column " + lookup.targetColumnName + " in project " + targetProjectMetadata.getName());
    }
    // We can't use for-each here, because we'll need the row index when creating WrappedRow
    int count = targetProject.rows.size();
    for (int r = 0; r < count; r++) {
        Row targetRow = targetProject.rows.get(r);
        Object value = targetRow.getCellValue(targetColumn.getCellIndex());
        if (ExpressionUtils.isNonBlankData(value)) {
            String valueStr = value.toString();
            lookup.valueToRowIndices.putIfAbsent(valueStr, new ArrayList<>());
            lookup.valueToRowIndices.get(valueStr).add(r);
        }
    }
}
Also used : Project(com.google.refine.model.Project) LookupException(com.google.refine.util.LookupException) Column(com.google.refine.model.Column) WrappedRow(com.google.refine.expr.WrappedRow) Row(com.google.refine.model.Row)

Aggregations

Project (com.google.refine.model.Project)2 LookupException (com.google.refine.util.LookupException)2 ProjectLookup (com.google.refine.LookupCacheManager.ProjectLookup)1 EvalError (com.google.refine.expr.EvalError)1 WrappedCell (com.google.refine.expr.WrappedCell)1 WrappedRow (com.google.refine.expr.WrappedRow)1 Column (com.google.refine.model.Column)1 Row (com.google.refine.model.Row)1 GetProjectIDException (com.google.refine.util.GetProjectIDException)1