Search in sources :

Example 1 with RecordVisitor

use of com.google.refine.browsing.RecordVisitor 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("(");
        }
        JSONWriter jsonWriter = new JSONWriter(writer);
        jsonWriter.object();
        RowWritingVisitor rwv = new RowWritingVisitor(start, limit, jsonWriter, options);
        JSONObject sortingJson = null;
        try {
            String json = request.getParameter("sorting");
            sortingJson = (json == null) ? null : ParsingUtilities.evaluateJsonStringToObject(json);
        } catch (JSONException e) {
        }
        if (engine.getMode() == Mode.RowBased) {
            FilteredRows filteredRows = engine.getAllFilteredRows();
            RowVisitor visitor = rwv;
            if (sortingJson != null) {
                SortingRowVisitor srv = new SortingRowVisitor(visitor);
                srv.initializeFromJSON(project, sortingJson);
                if (srv.hasCriteria()) {
                    visitor = srv;
                }
            }
            jsonWriter.key("mode");
            jsonWriter.value("row-based");
            jsonWriter.key("rows");
            jsonWriter.array();
            filteredRows.accept(project, visitor);
            jsonWriter.endArray();
            jsonWriter.key("filtered");
            jsonWriter.value(rwv.total);
            jsonWriter.key("total");
            jsonWriter.value(project.rows.size());
        } else {
            FilteredRecords filteredRecords = engine.getFilteredRecords();
            RecordVisitor visitor = rwv;
            if (sortingJson != null) {
                SortingRecordVisitor srv = new SortingRecordVisitor(visitor);
                srv.initializeFromJSON(project, sortingJson);
                if (srv.hasCriteria()) {
                    visitor = srv;
                }
            }
            jsonWriter.key("mode");
            jsonWriter.value("record-based");
            jsonWriter.key("rows");
            jsonWriter.array();
            filteredRecords.accept(project, visitor);
            jsonWriter.endArray();
            jsonWriter.key("filtered");
            jsonWriter.value(rwv.total);
            jsonWriter.key("total");
            jsonWriter.value(project.recordModel.getRecordCount());
        }
        jsonWriter.key("start");
        jsonWriter.value(start);
        jsonWriter.key("limit");
        jsonWriter.value(limit);
        jsonWriter.key("pool");
        pool.write(jsonWriter, options);
        jsonWriter.endObject();
        if (callback != null) {
            writer.write(")");
        }
    } catch (Exception e) {
        respondException(response, e);
    }
}
Also used : JSONWriter(org.json.JSONWriter) SortingRecordVisitor(com.google.refine.sorting.SortingRecordVisitor) JSONException(org.json.JSONException) FilteredRecords(com.google.refine.browsing.FilteredRecords) Properties(java.util.Properties) RecordVisitor(com.google.refine.browsing.RecordVisitor) SortingRecordVisitor(com.google.refine.sorting.SortingRecordVisitor) FilteredRows(com.google.refine.browsing.FilteredRows) ServletException(javax.servlet.ServletException) JSONException(org.json.JSONException) IOException(java.io.IOException) Project(com.google.refine.model.Project) JSONObject(org.json.JSONObject) ImportingJob(com.google.refine.importing.ImportingJob) Pool(com.google.refine.util.Pool) RowVisitor(com.google.refine.browsing.RowVisitor) SortingRowVisitor(com.google.refine.sorting.SortingRowVisitor) Engine(com.google.refine.browsing.Engine) SortingRowVisitor(com.google.refine.sorting.SortingRowVisitor) PrintWriter(java.io.PrintWriter)

Example 2 with RecordVisitor

use of com.google.refine.browsing.RecordVisitor in project OpenRefine by OpenRefine.

the class TemplatingExporter method export.

@Override
public void export(Project project, Properties options, Engine engine, Writer writer) throws IOException {
    String limitString = options.getProperty("limit");
    int limit = limitString != null ? Integer.parseInt(limitString) : -1;
    JSONObject sortingJson = null;
    try {
        String json = options.getProperty("sorting");
        sortingJson = (json == null) ? null : ParsingUtilities.evaluateJsonStringToObject(json);
    } catch (JSONException e) {
    }
    String templateString = options.getProperty("template");
    String prefixString = options.getProperty("prefix");
    String suffixString = options.getProperty("suffix");
    String separatorString = options.getProperty("separator");
    Template template;
    try {
        template = Parser.parse(templateString);
    } catch (ParsingException e) {
        throw new IOException("Missing or bad template", e);
    }
    template.setPrefix(prefixString);
    template.setSuffix(suffixString);
    template.setSeparator(separatorString);
    if (!"true".equals(options.getProperty("preview"))) {
        StringWriter stringWriter = new StringWriter();
        JSONWriter jsonWriter = new JSONWriter(stringWriter);
        try {
            jsonWriter.object();
            jsonWriter.key("template");
            jsonWriter.value(templateString);
            jsonWriter.key("prefix");
            jsonWriter.value(prefixString);
            jsonWriter.key("suffix");
            jsonWriter.value(suffixString);
            jsonWriter.key("separator");
            jsonWriter.value(separatorString);
            jsonWriter.endObject();
        } catch (JSONException e) {
        // ignore
        }
        project.getMetadata().getPreferenceStore().put("exporters.templating.template", stringWriter.toString());
    }
    if (engine.getMode() == Mode.RowBased) {
        FilteredRows filteredRows = engine.getAllFilteredRows();
        RowVisitor visitor = template.getRowVisitor(writer, limit);
        if (sortingJson != null) {
            try {
                SortingRowVisitor srv = new SortingRowVisitor(visitor);
                srv.initializeFromJSON(project, sortingJson);
                if (srv.hasCriteria()) {
                    visitor = srv;
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        filteredRows.accept(project, visitor);
    } else {
        FilteredRecords filteredRecords = engine.getFilteredRecords();
        RecordVisitor visitor = template.getRecordVisitor(writer, limit);
        if (sortingJson != null) {
            try {
                SortingRecordVisitor srv = new SortingRecordVisitor(visitor);
                srv.initializeFromJSON(project, sortingJson);
                if (srv.hasCriteria()) {
                    visitor = srv;
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        filteredRecords.accept(project, visitor);
    }
}
Also used : JSONWriter(org.json.JSONWriter) SortingRecordVisitor(com.google.refine.sorting.SortingRecordVisitor) JSONException(org.json.JSONException) IOException(java.io.IOException) FilteredRecords(com.google.refine.browsing.FilteredRecords) SortingRecordVisitor(com.google.refine.sorting.SortingRecordVisitor) RecordVisitor(com.google.refine.browsing.RecordVisitor) FilteredRows(com.google.refine.browsing.FilteredRows) Template(com.google.refine.templating.Template) JSONObject(org.json.JSONObject) StringWriter(java.io.StringWriter) ParsingException(com.google.refine.expr.ParsingException) RowVisitor(com.google.refine.browsing.RowVisitor) SortingRowVisitor(com.google.refine.sorting.SortingRowVisitor) SortingRowVisitor(com.google.refine.sorting.SortingRowVisitor)

Example 3 with RecordVisitor

use of com.google.refine.browsing.RecordVisitor 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)

Aggregations

RecordVisitor (com.google.refine.browsing.RecordVisitor)3 RowVisitor (com.google.refine.browsing.RowVisitor)3 SortingRecordVisitor (com.google.refine.sorting.SortingRecordVisitor)3 SortingRowVisitor (com.google.refine.sorting.SortingRowVisitor)3 Engine (com.google.refine.browsing.Engine)2 FilteredRecords (com.google.refine.browsing.FilteredRecords)2 FilteredRows (com.google.refine.browsing.FilteredRows)2 IOException (java.io.IOException)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 JSONWriter (org.json.JSONWriter)2 ParsingException (com.google.refine.expr.ParsingException)1 HistoryEntry (com.google.refine.history.HistoryEntry)1 ImportingJob (com.google.refine.importing.ImportingJob)1 Project (com.google.refine.model.Project)1 RowReorderChange (com.google.refine.model.changes.RowReorderChange)1 Template (com.google.refine.templating.Template)1 Pool (com.google.refine.util.Pool)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1