Refactor cross function to be more robust & improve diagnostics on fail

This commit is contained in:
Owen Stephens 2019-03-17 13:14:58 +00:00
parent ff888d239b
commit ae5f72a8df
3 changed files with 34 additions and 11 deletions

View File

@ -97,13 +97,13 @@ public class InterProjectModel {
* @param toColumn
* @return
*/
public ProjectJoin getJoin(String fromProject, String fromColumn, String toProject, String toColumn) {
public ProjectJoin getJoin(Long fromProject, String fromColumn, Long toProject, String toColumn) {
String key = fromProject + ";" + fromColumn + ";" + toProject + ";" + toColumn;
if (!_joins.containsKey(key)) {
ProjectJoin join = new ProjectJoin(
ProjectManager.singleton.getProjectID(fromProject),
fromProject,
fromColumn,
ProjectManager.singleton.getProjectID(toProject),
toProject,
toColumn
);

View File

@ -33,6 +33,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package com.google.refine;
import com.google.refine.util.GetProjectIDException;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
@ -383,15 +384,26 @@ public abstract class ProjectManager {
* @param name
* The name of the project
* @return
* The id of the project, or -1 if it cannot be found
* The id of the project
* @throws GetProjectIDException
* If no unique project is found with the given name
*/
public long getProjectID(String 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)) {
return entry.getKey();
id = entry.getKey();
c += 1;
}
}
return -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);
}
}
/**

View File

@ -33,6 +33,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package com.google.refine.expr.functions;
import com.google.refine.util.GetProjectIDException;
import java.util.Properties;
import com.google.refine.InterProjectModel.ProjectJoin;
@ -51,17 +52,27 @@ public class Cross implements Function {
// 1st argument can take either value or cell(for backward compatibility)
Object v = args[0];
Object toProjectName = args[1];
Object toColumnName = args[2];
Object toColumnName = args[2];
Long toProjectID;
if (v != null &&
( v instanceof String || v instanceof WrappedCell ) &&
toProjectName != null && toProjectName instanceof String &&
toColumnName != null && toColumnName instanceof String) {
try {
toProjectID = ProjectManager.singleton.getProjectID((String) toProjectName);
} catch (GetProjectIDException e){
return new EvalError(e.getMessage());
}
ProjectJoin join = ProjectManager.singleton.getInterProjectModel().getJoin(
ProjectManager.singleton.getProjectMetadata(((Project) bindings.get("project")).id).getName(),
// getJoin(Long fromProject, String fromColumn, Long toProject, String toColumn) {
// source project name
(Long) ((Project) bindings.get("project")).id,
// source column name
(String) bindings.get("columnName"),
(String) toProjectName,
// target project name
toProjectID,
// target column name
(String) toColumnName
);
if(v instanceof String) {