use of com.google.refine.model.Project in project OpenRefine by OpenRefine.
the class CustomizableTabularExporterUtilities method exportRows.
public static void exportRows(final Project project, final Engine engine, Properties params, final TabularSerializer serializer) {
String optionsString = (params != null) ? params.getProperty("options") : null;
JsonNode optionsTemp = null;
if (optionsString != null) {
try {
optionsTemp = ParsingUtilities.mapper.readTree(optionsString);
} catch (IOException e) {
// Ignore and keep options null.
}
}
final JsonNode options = optionsTemp;
final boolean outputColumnHeaders = options == null ? true : JSONUtilities.getBoolean(options, "outputColumnHeaders", true);
final boolean outputEmptyRows = options == null ? false : JSONUtilities.getBoolean(options, "outputBlankRows", true);
final int limit = options == null ? -1 : JSONUtilities.getInt(options, "limit", -1);
final List<String> columnNames;
final Map<String, CellFormatter> columnNameToFormatter = new HashMap<String, CustomizableTabularExporterUtilities.CellFormatter>();
List<JsonNode> columnOptionArray = options == null ? null : JSONUtilities.getArray(options, "columns");
if (columnOptionArray == null) {
List<Column> columns = project.columnModel.columns;
columnNames = new ArrayList<String>(columns.size());
for (Column column : columns) {
String name = column.getName();
columnNames.add(name);
columnNameToFormatter.put(name, new CellFormatter());
}
} else {
int count = columnOptionArray.size();
columnNames = new ArrayList<String>(count);
for (int i = 0; i < count; i++) {
JsonNode columnOptions = columnOptionArray.get(i);
if (columnOptions != null) {
String name = JSONUtilities.getString(columnOptions, "name", null);
if (name != null) {
columnNames.add(name);
try {
columnNameToFormatter.put(name, ParsingUtilities.mapper.treeToValue(columnOptions, ColumnOptions.class));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}
}
}
RowVisitor visitor = new RowVisitor() {
int rowCount = 0;
@Override
public void start(Project project) {
serializer.startFile(options);
if (outputColumnHeaders) {
List<CellData> cells = new ArrayList<TabularSerializer.CellData>(columnNames.size());
for (String name : columnNames) {
cells.add(new CellData(name, name, name, null));
}
serializer.addRow(cells, true);
}
}
@Override
public boolean visit(Project project, int rowIndex, Row row) {
List<CellData> cells = new ArrayList<TabularSerializer.CellData>(columnNames.size());
int nonNullCount = 0;
for (String columnName : columnNames) {
Column column = project.columnModel.getColumnByName(columnName);
CellFormatter formatter = columnNameToFormatter.get(columnName);
CellData cellData = formatter.format(project, column, row.getCell(column.getCellIndex()));
cells.add(cellData);
if (cellData != null) {
nonNullCount++;
}
}
if (nonNullCount > 0 || outputEmptyRows) {
serializer.addRow(cells, false);
rowCount++;
}
return limit > 0 && rowCount >= limit;
}
@Override
public void end(Project project) {
serializer.endFile();
}
};
FilteredRows filteredRows = engine.getAllFilteredRows();
filteredRows.accept(project, visitor);
}
use of com.google.refine.model.Project in project OpenRefine by OpenRefine.
the class ReconClearOneCellCommand method doPost.
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (!hasValidCSRFToken(request)) {
respondCSRFError(response);
return;
}
try {
Project project = getProject(request);
int rowIndex = Integer.parseInt(request.getParameter("row"));
int cellIndex = Integer.parseInt(request.getParameter("cell"));
ClearOneCellProcess process = new ClearOneCellProcess(project, "Clear one cell's recon data", rowIndex, cellIndex);
HistoryEntry historyEntry = project.processManager.queueProcess(process);
if (historyEntry != null) {
/*
* If the process is done, write back the cell's data so that the
* client side can update its UI right away.
*/
Pool pool = new Pool();
if (process.newCell != null && process.newCell.recon != null) {
pool.pool(process.newCell.recon);
}
respondJSON(response, new CellResponse(historyEntry, process.newCell, pool));
} else {
respond(response, "{ \"code\" : \"pending\" }");
}
} catch (Exception e) {
respondException(response, e);
}
}
use of com.google.refine.model.Project in project OpenRefine by OpenRefine.
the class ReconJudgeOneCellCommand method doPost.
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (!hasValidCSRFToken(request)) {
respondCSRFError(response);
return;
}
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"));
Judgment judgment = Recon.stringToJudgment(request.getParameter("judgment"));
ReconCandidate match = null;
String id = request.getParameter("id");
if (id != null) {
String scoreString = request.getParameter("score");
match = new ReconCandidate(id, request.getParameter("name"), request.getParameter("types").split(","), scoreString != null ? Double.parseDouble(scoreString) : 100);
}
JudgeOneCellProcess process = new JudgeOneCellProcess(project, "Judge one cell's recon result", judgment, rowIndex, cellIndex, match, request.getParameter("identifierSpace"), request.getParameter("schemaSpace"));
HistoryEntry historyEntry = project.processManager.queueProcess(process);
if (historyEntry != null) {
/*
* If the process is done, write back the cell's data so that the
* client side can update its UI right away.
*/
Pool pool = new Pool();
if (process.newCell != null && process.newCell.recon != null) {
pool.pool(process.newCell.recon);
}
respondJSON(response, new ReconClearOneCellCommand.CellResponse(historyEntry, process.newCell, pool));
} else {
respond(response, "{ \"code\" : \"pending\" }");
}
} catch (Exception e) {
respondException(response, e);
}
}
use of com.google.refine.model.Project in project OpenRefine by OpenRefine.
the class GetRowsCommand method internalRespond.
protected void internalRespond(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
Project project = null;
// This command also supports retrieving rows for an importing job.
String importingJobID = request.getParameter("importingJobID");
if (importingJobID != null) {
long jobID = Long.parseLong(importingJobID);
ImportingJob job = ImportingManager.getJob(jobID);
if (job != null) {
project = job.project;
}
}
if (project == null) {
project = getProject(request);
}
Engine engine = getEngine(request, project);
String callback = request.getParameter("callback");
int start = Math.min(project.rows.size(), Math.max(0, getIntegerParameter(request, "start", 0)));
int limit = Math.min(project.rows.size() - start, Math.max(0, getIntegerParameter(request, "limit", 20)));
Pool pool = new Pool();
/* Properties options = new Properties();
options.put("project", project);
options.put("reconCandidateOmitTypes", true);
options.put("pool", pool); */
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", callback == null ? "application/json" : "text/javascript");
PrintWriter writer = response.getWriter();
if (callback != null) {
writer.write(callback);
writer.write("(");
}
RowWritingVisitor rwv = new RowWritingVisitor(start, limit);
SortingConfig sortingConfig = null;
try {
String sortingJson = request.getParameter("sorting");
if (sortingJson != null) {
sortingConfig = SortingConfig.reconstruct(sortingJson);
}
} catch (IOException e) {
}
if (engine.getMode() == Mode.RowBased) {
FilteredRows filteredRows = engine.getAllFilteredRows();
RowVisitor visitor = rwv;
if (sortingConfig != null) {
SortingRowVisitor srv = new SortingRowVisitor(visitor);
srv.initializeFromConfig(project, sortingConfig);
if (srv.hasCriteria()) {
visitor = srv;
}
}
filteredRows.accept(project, visitor);
} else {
FilteredRecords filteredRecords = engine.getFilteredRecords();
RecordVisitor visitor = rwv;
if (sortingConfig != null) {
SortingRecordVisitor srv = new SortingRecordVisitor(visitor);
srv.initializeFromConfig(project, sortingConfig);
if (srv.hasCriteria()) {
visitor = srv;
}
}
filteredRecords.accept(project, visitor);
}
// Pool all the recons occuring in the rows seen
for (WrappedRow wr : rwv.results) {
for (Cell c : wr.row.cells) {
if (c != null && c.recon != null) {
pool.pool(c.recon);
}
}
}
JsonResult result = new JsonResult(engine.getMode(), rwv.results, rwv.total, engine.getMode() == Mode.RowBased ? project.rows.size() : project.recordModel.getRecordCount(), start, limit, pool);
ParsingUtilities.defaultWriter.writeValue(writer, result);
if (callback != null) {
writer.write(")");
}
// metadata refresh for row mode and record mode
if (project.getMetadata() != null) {
project.getMetadata().setRowCount(project.rows.size());
}
} catch (Exception e) {
respondException(response, e);
}
}
use of com.google.refine.model.Project in project OpenRefine by OpenRefine.
the class CancelProcessesCommand method doPost.
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (request == null) {
throw new IllegalArgumentException("parameter 'request' should not be null");
}
if (response == null) {
throw new IllegalArgumentException("parameter 'request' should not be null");
}
if (!hasValidCSRFToken(request)) {
respondCSRFError(response);
return;
}
try {
Project project = getProject(request);
project.getProcessManager().cancelAll();
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
response.getWriter().write("{ \"code\" : \"ok\" }");
} catch (Exception e) {
respondException(response, e);
}
}
Aggregations