Search in sources :

Example 16 with Project

use of com.google.refine.model.Project in project OpenRefine by OpenRefine.

the class ColumnAdditionOperation method createRowVisitor.

protected RowVisitor createRowVisitor(Project project, List<CellAtRow> cellsAtRows) throws Exception {
    Column column = project.columnModel.getColumnByName(_baseColumnName);
    Evaluable eval = MetaParser.parse(_expression);
    Properties bindings = ExpressionUtils.createBindings(project);
    return new RowVisitor() {

        int cellIndex;

        Properties bindings;

        List<CellAtRow> cellsAtRows;

        Evaluable eval;

        public RowVisitor init(int cellIndex, Properties bindings, List<CellAtRow> cellsAtRows, Evaluable eval) {
            this.cellIndex = cellIndex;
            this.bindings = bindings;
            this.cellsAtRows = cellsAtRows;
            this.eval = eval;
            return this;
        }

        @Override
        public void start(Project project) {
        // nothing to do
        }

        @Override
        public void end(Project project) {
        // nothing to do
        }

        @Override
        public boolean visit(Project project, int rowIndex, Row row) {
            Cell cell = row.getCell(cellIndex);
            Cell newCell = null;
            ExpressionUtils.bind(bindings, row, rowIndex, _baseColumnName, cell);
            Object o = eval.evaluate(bindings);
            if (o != null) {
                if (o instanceof Cell) {
                    newCell = (Cell) o;
                } else if (o instanceof WrappedCell) {
                    newCell = ((WrappedCell) o).cell;
                } else {
                    Serializable v = ExpressionUtils.wrapStorable(o);
                    if (ExpressionUtils.isError(v)) {
                        if (_onError == OnError.SetToBlank) {
                            return false;
                        } else if (_onError == OnError.KeepOriginal) {
                            v = cell != null ? cell.value : null;
                        }
                    }
                    if (v != null) {
                        newCell = new Cell(v, null);
                    }
                }
            }
            if (newCell != null) {
                cellsAtRows.add(new CellAtRow(rowIndex, newCell));
            }
            return false;
        }
    }.init(column.getCellIndex(), bindings, cellsAtRows, eval);
}
Also used : Serializable(java.io.Serializable) Properties(java.util.Properties) Evaluable(com.google.refine.expr.Evaluable) Project(com.google.refine.model.Project) CellAtRow(com.google.refine.model.changes.CellAtRow) WrappedCell(com.google.refine.expr.WrappedCell) Column(com.google.refine.model.Column) Row(com.google.refine.model.Row) CellAtRow(com.google.refine.model.changes.CellAtRow) RowVisitor(com.google.refine.browsing.RowVisitor) Cell(com.google.refine.model.Cell) WrappedCell(com.google.refine.expr.WrappedCell)

Example 17 with Project

use of com.google.refine.model.Project in project OpenRefine by OpenRefine.

the class ReconCopyAcrossColumnsOperation method createHistoryEntry.

@Override
protected HistoryEntry createHistoryEntry(final Project project, final long historyEntryID) throws Exception {
    Engine engine = createEngine(project);
    final Column fromColumn = project.columnModel.getColumnByName(_fromColumnName);
    final List<Column> toColumns = new ArrayList<Column>(_toColumnNames.length);
    for (String c : _toColumnNames) {
        Column toColumn = project.columnModel.getColumnByName(c);
        if (toColumn != null) {
            toColumns.add(toColumn);
        }
    }
    final Set<Recon.Judgment> judgments = new HashSet<Recon.Judgment>(_judgments.length);
    for (String j : _judgments) {
        judgments.add(Recon.stringToJudgment(j));
    }
    final List<CellChange> cellChanges = new ArrayList<CellChange>(project.rows.size());
    if (fromColumn != null && toColumns.size() > 0) {
        final Map<Object, Recon> cellValueToRecon = new HashMap<Object, Recon>();
        FilteredRows filteredRows = engine.getAllFilteredRows();
        try {
            filteredRows.accept(project, new RowVisitor() {

                @Override
                public void start(Project project) {
                // nothing to do
                }

                @Override
                public void end(Project project) {
                // nothing to do
                }

                @Override
                public boolean visit(Project project, int rowIndex, Row row) {
                    Cell cell = row.getCell(fromColumn.getCellIndex());
                    if (cell != null && cell.value != null && cell.recon != null) {
                        if (judgments.contains(cell.recon.judgment)) {
                            cellValueToRecon.put(cell.value, cell.recon);
                        }
                    }
                    return false;
                }
            });
            filteredRows.accept(project, new RowVisitor() {

                @Override
                public void start(Project project) {
                // nothing to do
                }

                @Override
                public void end(Project project) {
                // nothing to do
                }

                @Override
                public boolean visit(Project project, int rowIndex, Row row) {
                    for (Column column : toColumns) {
                        int cellIndex = column.getCellIndex();
                        Cell cell = row.getCell(cellIndex);
                        if (cell != null && cell.value != null) {
                            Recon reconToCopy = cellValueToRecon.get(cell.value);
                            boolean judged = cell.recon != null && cell.recon.judgment != Judgment.None;
                            if (reconToCopy != null && (!judged || _applyToJudgedCells)) {
                                Cell newCell = new Cell(cell.value, reconToCopy);
                                CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, newCell);
                                cellChanges.add(cellChange);
                            }
                        }
                    }
                    return false;
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    String description = "Copy " + cellChanges.size() + " recon judgments from column " + _fromColumnName + " to " + StringUtils.join(_toColumnNames);
    return new HistoryEntry(historyEntryID, project, description, this, new MassChange(cellChanges, false));
}
Also used : HashMap(java.util.HashMap) CellChange(com.google.refine.model.changes.CellChange) ArrayList(java.util.ArrayList) FilteredRows(com.google.refine.browsing.FilteredRows) MassChange(com.google.refine.model.changes.MassChange) Project(com.google.refine.model.Project) Column(com.google.refine.model.Column) HistoryEntry(com.google.refine.history.HistoryEntry) Row(com.google.refine.model.Row) Recon(com.google.refine.model.Recon) RowVisitor(com.google.refine.browsing.RowVisitor) Cell(com.google.refine.model.Cell) Engine(com.google.refine.browsing.Engine) Judgment(com.google.refine.model.Recon.Judgment) HashSet(java.util.HashSet)

Example 18 with Project

use of com.google.refine.model.Project in project OpenRefine by OpenRefine.

the class ProjectManagerTests method canSaveAllModified.

// TODO test ensureProjectSave in race condition
@Test
public void canSaveAllModified() {
    // 5 minute difference
    whenGetSaveTimes(project, metadata);
    registerProject(project, metadata);
    // add a second project to the cache
    Project project2 = spy(new ProjectStub(2));
    ProjectMetadata metadata2 = mock(ProjectMetadata.class);
    // not modified since the last save but within 30 seconds flush limit
    whenGetSaveTimes(project2, metadata2, 10);
    registerProject(project2, metadata2);
    // check that the two projects are not the same
    Assert.assertFalse(project.id == project2.id);
    SUT.save(true);
    verifySaved(project, metadata);
    verifySaved(project2, metadata2);
    verify(SUT, times(1)).saveWorkspace();
}
Also used : Project(com.google.refine.model.Project) ProjectStub(com.google.refine.model.ProjectStub) ProjectMetadata(com.google.refine.ProjectMetadata) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 19 with Project

use of com.google.refine.model.Project in project OpenRefine by OpenRefine.

the class RefineTest method createCSVProject.

/**
 * Helper to create a project from a CSV encoded as a file. Not much control is given on the import options, because
 * this method is intended to be a quick way to create a project for a test. For more control over the import, just
 * call the importer directly.
 *
 * The projects created via this method and their importing jobs will be disposed of at the end of each test.
 *
 * @param projectName
 *            the name of the project to create
 * @param input
 *            the content of the file, encoded as a CSV (with "," as a separator)
 * @return
 */
protected Project createCSVProject(String projectName, String input) {
    Project project = new Project();
    ProjectMetadata metadata = new ProjectMetadata();
    metadata.setName(projectName);
    ObjectNode options = mock(ObjectNode.class);
    prepareImportOptions(options, ",", -1, 0, 0, 1, false, false);
    ImportingJob job = ImportingManager.createJob();
    SeparatorBasedImporter importer = new SeparatorBasedImporter();
    List<Exception> exceptions = new ArrayList<Exception>();
    importer.parseOneFile(project, metadata, job, "filesource", new StringReader(input), -1, options, exceptions);
    project.update();
    ProjectManager.singleton.registerProject(project, metadata);
    projects.add(project);
    importingJobs.add(job);
    return project;
}
Also used : Project(com.google.refine.model.Project) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) ImportingJob(com.google.refine.importing.ImportingJob) SeparatorBasedImporter(com.google.refine.importers.SeparatorBasedImporter) IOException(java.io.IOException) ModelException(com.google.refine.model.ModelException) ParsingException(com.google.refine.expr.ParsingException)

Example 20 with Project

use of com.google.refine.model.Project in project OpenRefine by OpenRefine.

the class RefineTest method createProjectWithColumns.

protected Project createProjectWithColumns(String projectName, String... columnNames) throws IOException, ModelException {
    Project project = new Project();
    ProjectMetadata pm = new ProjectMetadata();
    pm.setName(projectName);
    ProjectManager.singleton.registerProject(project, pm);
    if (columnNames != null) {
        for (String columnName : columnNames) {
            int index = project.columnModel.allocateNewCellIndex();
            Column column = new Column(index, columnName);
            project.columnModel.addColumn(index, column, true);
        }
    }
    return project;
}
Also used : Project(com.google.refine.model.Project) Column(com.google.refine.model.Column)

Aggregations

Project (com.google.refine.model.Project)146 Properties (java.util.Properties)46 IOException (java.io.IOException)40 Engine (com.google.refine.browsing.Engine)37 Test (org.testng.annotations.Test)37 ServletException (javax.servlet.ServletException)35 ProjectMetadata (com.google.refine.ProjectMetadata)33 BeforeMethod (org.testng.annotations.BeforeMethod)31 Cell (com.google.refine.model.Cell)29 Row (com.google.refine.model.Row)29 RefineTest (com.google.refine.RefineTest)25 Column (com.google.refine.model.Column)25 BeforeTest (org.testng.annotations.BeforeTest)18 Process (com.google.refine.process.Process)17 AbstractOperation (com.google.refine.model.AbstractOperation)14 StringWriter (java.io.StringWriter)13 RowVisitor (com.google.refine.browsing.RowVisitor)11 ArrayList (java.util.ArrayList)11 File (java.io.File)10 WrappedCell (com.google.refine.expr.WrappedCell)9