Search in sources :

Example 1 with GetProjectIDException

use of com.google.refine.util.GetProjectIDException 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 GetProjectIDException

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

the class ProjectManager method getProjectID.

/**
 * Tries to find the project id when given a project name
 * Requires that all project metadata exists has been loaded to memory from the data store
 * @param name
 *     The name of the project
 * @return
 *     The id of the project
 * @throws GetProjectIDException
 *     If no unique project is found with the given name
 */
public long getProjectID(String name) throws GetProjectIDException {
    Integer c = 0;
    Long id = 0L;
    for (Entry<Long, ProjectMetadata> entry : _projectsMetadata.entrySet()) {
        if (entry.getValue().getName().equals(name)) {
            id = entry.getKey();
            c += 1;
        }
    }
    if (c == 1) {
        return id;
    } else if (c == 0) {
        throw new GetProjectIDException("Unable to find project with name: " + name);
    } else {
        throw new GetProjectIDException(c + " projects found with name: " + name);
    }
}
Also used : GetProjectIDException(com.google.refine.util.GetProjectIDException)

Aggregations

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