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);
}
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));
}
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\" }");
}
}
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);
}
}
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));
}
Aggregations