Search in sources :

Example 11 with HistoryEntry

use of com.google.refine.history.HistoryEntry in project OpenRefine by OpenRefine.

the class HistoryTests method canAddEntry.

@Test
public void canAddEntry() {
    //local dependencies
    HistoryEntry entry = mock(HistoryEntry.class);
    Project project = mock(Project.class);
    ProjectMetadata projectMetadata = mock(ProjectMetadata.class);
    when(projectManager.getProject(Mockito.anyLong())).thenReturn(project);
    when(projectManager.getProjectMetadata(Mockito.anyLong())).thenReturn(projectMetadata);
    SUT.addEntry(entry);
    verify(projectManager, times(1)).getProject(Mockito.anyLong());
    verify(entry, times(1)).apply(project);
    verify(projectMetadata, times(1)).updateModified();
    Assert.assertEquals(SUT.getLastPastEntries(1).get(0), entry);
}
Also used : Project(com.google.refine.model.Project) ProjectMetadata(com.google.refine.ProjectMetadata) HistoryEntry(com.google.refine.history.HistoryEntry) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) RefineTest(com.google.refine.tests.RefineTest)

Example 12 with HistoryEntry

use of com.google.refine.history.HistoryEntry in project OpenRefine by OpenRefine.

the class RowReorderOperation method createHistoryEntry.

@Override
protected HistoryEntry createHistoryEntry(Project project, long historyEntryID) throws Exception {
    Engine engine = new Engine(project);
    engine.setMode(_mode);
    List<Integer> rowIndices = new ArrayList<Integer>();
    if (_mode == Mode.RowBased) {
        RowVisitor visitor = new IndexingVisitor(rowIndices);
        if (_sorting != null) {
            SortingRowVisitor srv = new SortingRowVisitor(visitor);
            srv.initializeFromJSON(project, _sorting);
            if (srv.hasCriteria()) {
                visitor = srv;
            }
        }
        engine.getAllRows().accept(project, visitor);
    } else {
        RecordVisitor visitor = new IndexingVisitor(rowIndices);
        if (_sorting != null) {
            SortingRecordVisitor srv = new SortingRecordVisitor(visitor);
            srv.initializeFromJSON(project, _sorting);
            if (srv.hasCriteria()) {
                visitor = srv;
            }
        }
        engine.getAllRecords().accept(project, visitor);
    }
    return new HistoryEntry(historyEntryID, project, "Reorder rows", this, new RowReorderChange(rowIndices));
}
Also used : RowReorderChange(com.google.refine.model.changes.RowReorderChange) ArrayList(java.util.ArrayList) SortingRecordVisitor(com.google.refine.sorting.SortingRecordVisitor) HistoryEntry(com.google.refine.history.HistoryEntry) SortingRecordVisitor(com.google.refine.sorting.SortingRecordVisitor) RecordVisitor(com.google.refine.browsing.RecordVisitor) RowVisitor(com.google.refine.browsing.RowVisitor) SortingRowVisitor(com.google.refine.sorting.SortingRowVisitor) Engine(com.google.refine.browsing.Engine) SortingRowVisitor(com.google.refine.sorting.SortingRowVisitor)

Example 13 with HistoryEntry

use of com.google.refine.history.HistoryEntry in project OpenRefine by OpenRefine.

the class Command method performProcessAndRespond.

protected static void performProcessAndRespond(HttpServletRequest request, HttpServletResponse response, Project project, Process process) throws Exception {
    response.setCharacterEncoding("UTF-8");
    response.setHeader("Content-Type", "application/json");
    HistoryEntry historyEntry = project.processManager.queueProcess(process);
    if (historyEntry != null) {
        Writer w = response.getWriter();
        JSONWriter writer = new JSONWriter(w);
        Properties options = new Properties();
        writer.object();
        writer.key("code");
        writer.value("ok");
        writer.key("historyEntry");
        historyEntry.write(writer, options);
        writer.endObject();
        w.flush();
        w.close();
    } else {
        respond(response, "{ \"code\" : \"pending\" }");
    }
}
Also used : JSONWriter(org.json.JSONWriter) HistoryEntry(com.google.refine.history.HistoryEntry) Properties(java.util.Properties) PrintWriter(java.io.PrintWriter) StringWriter(java.io.StringWriter) JSONWriter(org.json.JSONWriter) Writer(java.io.Writer)

Example 14 with HistoryEntry

use of com.google.refine.history.HistoryEntry in project OpenRefine by OpenRefine.

the class EditOneCellCommand method doPost.

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        Project project = getProject(request);
        int rowIndex = Integer.parseInt(request.getParameter("row"));
        int cellIndex = Integer.parseInt(request.getParameter("cell"));
        String type = request.getParameter("type");
        String valueString = request.getParameter("value");
        Serializable value = null;
        if ("number".equals(type)) {
            value = Double.parseDouble(valueString);
        } else if ("boolean".equals(type)) {
            value = "true".equalsIgnoreCase(valueString);
        } else if ("date".equals(type)) {
            value = ParsingUtilities.stringToDate(valueString);
        } else {
            value = valueString;
        }
        EditOneCellProcess process = new EditOneCellProcess(project, "Edit single cell", rowIndex, cellIndex, value);
        HistoryEntry historyEntry = project.processManager.queueProcess(process);
        if (historyEntry != null) {
            /*
                 * If the operation has been done, return the new cell's data
                 * so the client side can update the cell's rendering right away.
                 */
            JSONWriter writer = new JSONWriter(response.getWriter());
            Pool pool = new Pool();
            Properties options = new Properties();
            options.put("pool", pool);
            writer.object();
            writer.key("code");
            writer.value("ok");
            writer.key("historyEntry");
            historyEntry.write(writer, options);
            writer.key("cell");
            process.newCell.write(writer, options);
            writer.key("pool");
            pool.write(writer, options);
            writer.endObject();
        } else {
            respond(response, "{ \"code\" : \"pending\" }");
        }
    } catch (Exception e) {
        respondException(response, e);
    }
}
Also used : JSONWriter(org.json.JSONWriter) Project(com.google.refine.model.Project) Serializable(java.io.Serializable) HistoryEntry(com.google.refine.history.HistoryEntry) Pool(com.google.refine.util.Pool) Properties(java.util.Properties) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 15 with HistoryEntry

use of com.google.refine.history.HistoryEntry in project OpenRefine by OpenRefine.

the class MultiValuedCellJoinOperation method createHistoryEntry.

@Override
protected HistoryEntry createHistoryEntry(Project project, long historyEntryID) throws Exception {
    Column column = project.columnModel.getColumnByName(_columnName);
    if (column == null) {
        throw new Exception("No column named " + _columnName);
    }
    int cellIndex = column.getCellIndex();
    Column keyColumn = project.columnModel.getColumnByName(_keyColumnName);
    if (keyColumn == null) {
        throw new Exception("No key column named " + _keyColumnName);
    }
    int keyCellIndex = keyColumn.getCellIndex();
    List<Row> newRows = new ArrayList<Row>();
    int oldRowCount = project.rows.size();
    for (int r = 0; r < oldRowCount; r++) {
        Row oldRow = project.rows.get(r);
        if (oldRow.isCellBlank(keyCellIndex)) {
            newRows.add(oldRow.dup());
            continue;
        }
        int r2 = r + 1;
        while (r2 < oldRowCount && project.rows.get(r2).isCellBlank(keyCellIndex)) {
            r2++;
        }
        if (r2 == r + 1) {
            newRows.add(oldRow.dup());
            continue;
        }
        StringBuffer sb = new StringBuffer();
        for (int r3 = r; r3 < r2; r3++) {
            Object value = project.rows.get(r3).getCellValue(cellIndex);
            if (ExpressionUtils.isNonBlankData(value)) {
                if (sb.length() > 0) {
                    sb.append(_separator);
                }
                sb.append(value.toString());
            }
        }
        for (int r3 = r; r3 < r2; r3++) {
            Row newRow = project.rows.get(r3).dup();
            if (r3 == r) {
                newRow.setCell(cellIndex, new Cell(sb.toString(), null));
            } else {
                newRow.setCell(cellIndex, null);
            }
            if (!newRow.isEmpty()) {
                newRows.add(newRow);
            }
        }
        // r will be incremented by the for loop anyway
        r = r2 - 1;
    }
    return new HistoryEntry(historyEntryID, project, getBriefDescription(null), this, new MassRowChange(newRows));
}
Also used : MassRowChange(com.google.refine.model.changes.MassRowChange) Column(com.google.refine.model.Column) ArrayList(java.util.ArrayList) HistoryEntry(com.google.refine.history.HistoryEntry) JSONObject(org.json.JSONObject) Row(com.google.refine.model.Row) Cell(com.google.refine.model.Cell) JSONException(org.json.JSONException)

Aggregations

HistoryEntry (com.google.refine.history.HistoryEntry)22 ArrayList (java.util.ArrayList)15 Column (com.google.refine.model.Column)10 Engine (com.google.refine.browsing.Engine)8 JSONException (org.json.JSONException)8 FilteredRows (com.google.refine.browsing.FilteredRows)7 Cell (com.google.refine.model.Cell)7 Row (com.google.refine.model.Row)7 Project (com.google.refine.model.Project)6 Properties (java.util.Properties)6 Change (com.google.refine.history.Change)5 JSONWriter (org.json.JSONWriter)5 JSONObject (org.json.JSONObject)4 RowVisitor (com.google.refine.browsing.RowVisitor)3 MassChange (com.google.refine.model.changes.MassChange)3 MassRowChange (com.google.refine.model.changes.MassRowChange)3 MassRowColumnChange (com.google.refine.model.changes.MassRowColumnChange)3 Pool (com.google.refine.util.Pool)3 IOException (java.io.IOException)3 ServletException (javax.servlet.ServletException)3