Search in sources :

Example 1 with SortingConfig

use of com.google.refine.sorting.SortingConfig 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;
    String sortingJson = options.getProperty("sorting");
    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"))) {
        TemplateConfig config = new TemplateConfig(templateString, prefixString, suffixString, separatorString);
        project.getMetadata().getPreferenceStore().put("exporters.templating.template", ParsingUtilities.defaultWriter.writeValueAsString(config));
    }
    if (engine.getMode() == Mode.RowBased) {
        FilteredRows filteredRows = engine.getAllFilteredRows();
        RowVisitor visitor = template.getRowVisitor(writer, limit);
        if (sortingJson != null) {
            SortingConfig sorting = SortingConfig.reconstruct(sortingJson);
            SortingRowVisitor srv = new SortingRowVisitor(visitor);
            srv.initializeFromConfig(project, sorting);
            if (srv.hasCriteria()) {
                visitor = srv;
            }
        }
        filteredRows.accept(project, visitor);
    } else {
        FilteredRecords filteredRecords = engine.getFilteredRecords();
        RecordVisitor visitor = template.getRecordVisitor(writer, limit);
        if (sortingJson != null) {
            SortingConfig sorting = SortingConfig.reconstruct(sortingJson);
            SortingRecordVisitor srv = new SortingRecordVisitor(visitor);
            srv.initializeFromConfig(project, sorting);
            if (srv.hasCriteria()) {
                visitor = srv;
            }
        }
        filteredRecords.accept(project, visitor);
    }
}
Also used : SortingRecordVisitor(com.google.refine.sorting.SortingRecordVisitor) 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) ParsingException(com.google.refine.expr.ParsingException) RowVisitor(com.google.refine.browsing.RowVisitor) SortingRowVisitor(com.google.refine.sorting.SortingRowVisitor) SortingRowVisitor(com.google.refine.sorting.SortingRowVisitor) SortingConfig(com.google.refine.sorting.SortingConfig)

Example 2 with SortingConfig

use of com.google.refine.sorting.SortingConfig 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);
    }
}
Also used : SortingRecordVisitor(com.google.refine.sorting.SortingRecordVisitor) IOException(java.io.IOException) FilteredRecords(com.google.refine.browsing.FilteredRecords) RecordVisitor(com.google.refine.browsing.RecordVisitor) SortingRecordVisitor(com.google.refine.sorting.SortingRecordVisitor) FilteredRows(com.google.refine.browsing.FilteredRows) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) Project(com.google.refine.model.Project) ImportingJob(com.google.refine.importing.ImportingJob) Pool(com.google.refine.util.Pool) RowVisitor(com.google.refine.browsing.RowVisitor) SortingRowVisitor(com.google.refine.sorting.SortingRowVisitor) Cell(com.google.refine.model.Cell) Engine(com.google.refine.browsing.Engine) SortingRowVisitor(com.google.refine.sorting.SortingRowVisitor) PrintWriter(java.io.PrintWriter) SortingConfig(com.google.refine.sorting.SortingConfig)

Example 3 with SortingConfig

use of com.google.refine.sorting.SortingConfig in project OpenRefine by OpenRefine.

the class ReorderRowsCommand method createOperation.

@Override
protected AbstractOperation createOperation(Project project, HttpServletRequest request, EngineConfig engineConfig) throws Exception {
    String mode = request.getParameter("mode");
    SortingConfig sorting = null;
    try {
        String json = request.getParameter("sorting");
        sorting = (json == null) ? null : SortingConfig.reconstruct(json);
    } catch (IOException e) {
    // ignore
    }
    return new RowReorderOperation(Engine.stringToMode(mode), sorting);
}
Also used : IOException(java.io.IOException) RowReorderOperation(com.google.refine.operations.row.RowReorderOperation) SortingConfig(com.google.refine.sorting.SortingConfig)

Example 4 with SortingConfig

use of com.google.refine.sorting.SortingConfig in project OpenRefine by OpenRefine.

the class RowReorderOperationTests method testSortEmptyString.

@Test
public void testSortEmptyString() throws Exception {
    String sortingJson = "{\"criteria\":[{\"column\":\"key\",\"valueType\":\"number\",\"reverse\":false,\"blankPosition\":2,\"errorPosition\":1}]}";
    SortingConfig sortingConfig = SortingConfig.reconstruct(sortingJson);
    project.rows.get(1).cells.set(0, new Cell("", null));
    AbstractOperation op = new RowReorderOperation(Mode.RowBased, sortingConfig);
    Process process = op.createProcess(project, new Properties());
    process.performImmediate();
    Assert.assertEquals("h", project.rows.get(0).cells.get(1).value);
    Assert.assertEquals("f", project.rows.get(1).cells.get(1).value);
    Assert.assertEquals("b", project.rows.get(2).cells.get(1).value);
    Assert.assertEquals("d", project.rows.get(3).cells.get(1).value);
}
Also used : AbstractOperation(com.google.refine.model.AbstractOperation) Process(com.google.refine.process.Process) Properties(java.util.Properties) Cell(com.google.refine.model.Cell) SortingConfig(com.google.refine.sorting.SortingConfig) RefineTest(com.google.refine.RefineTest) Test(org.testng.annotations.Test)

Aggregations

SortingConfig (com.google.refine.sorting.SortingConfig)4 IOException (java.io.IOException)3 FilteredRecords (com.google.refine.browsing.FilteredRecords)2 FilteredRows (com.google.refine.browsing.FilteredRows)2 RecordVisitor (com.google.refine.browsing.RecordVisitor)2 RowVisitor (com.google.refine.browsing.RowVisitor)2 Cell (com.google.refine.model.Cell)2 SortingRecordVisitor (com.google.refine.sorting.SortingRecordVisitor)2 SortingRowVisitor (com.google.refine.sorting.SortingRowVisitor)2 RefineTest (com.google.refine.RefineTest)1 Engine (com.google.refine.browsing.Engine)1 ParsingException (com.google.refine.expr.ParsingException)1 ImportingJob (com.google.refine.importing.ImportingJob)1 AbstractOperation (com.google.refine.model.AbstractOperation)1 Project (com.google.refine.model.Project)1 RowReorderOperation (com.google.refine.operations.row.RowReorderOperation)1 Process (com.google.refine.process.Process)1 Template (com.google.refine.templating.Template)1 Pool (com.google.refine.util.Pool)1 PrintWriter (java.io.PrintWriter)1