Search in sources :

Example 26 with Project

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);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) CellData(com.google.refine.exporters.TabularSerializer.CellData) FilteredRows(com.google.refine.browsing.FilteredRows) Project(com.google.refine.model.Project) Column(com.google.refine.model.Column) Row(com.google.refine.model.Row) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) RowVisitor(com.google.refine.browsing.RowVisitor)

Example 27 with Project

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);
    }
}
Also used : Project(com.google.refine.model.Project) HistoryEntry(com.google.refine.history.HistoryEntry) Pool(com.google.refine.util.Pool) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 28 with Project

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);
    }
}
Also used : Project(com.google.refine.model.Project) HistoryEntry(com.google.refine.history.HistoryEntry) Pool(com.google.refine.util.Pool) ReconCandidate(com.google.refine.model.ReconCandidate) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) Judgment(com.google.refine.model.Recon.Judgment)

Example 29 with Project

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);
    }
}
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 30 with Project

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);
    }
}
Also used : Project(com.google.refine.model.Project) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Aggregations

Project (com.google.refine.model.Project)146 Properties (java.util.Properties)46 IOException (java.io.IOException)40 Engine (com.google.refine.browsing.Engine)37 Test (org.testng.annotations.Test)37 ServletException (javax.servlet.ServletException)35 ProjectMetadata (com.google.refine.ProjectMetadata)33 BeforeMethod (org.testng.annotations.BeforeMethod)31 Cell (com.google.refine.model.Cell)29 Row (com.google.refine.model.Row)29 RefineTest (com.google.refine.RefineTest)25 Column (com.google.refine.model.Column)25 BeforeTest (org.testng.annotations.BeforeTest)18 Process (com.google.refine.process.Process)17 AbstractOperation (com.google.refine.model.AbstractOperation)14 StringWriter (java.io.StringWriter)13 RowVisitor (com.google.refine.browsing.RowVisitor)11 ArrayList (java.util.ArrayList)11 File (java.io.File)10 WrappedCell (com.google.refine.expr.WrappedCell)9