Search in sources :

Example 86 with Project

use of com.google.refine.model.Project in project OpenRefine by OpenRefine.

the class SaveWikibaseSchemaCommand method doPost.

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    if (!hasValidCSRFToken(request)) {
        respondCSRFError(response);
        return;
    }
    try {
        Project project = getProject(request);
        String jsonString = request.getParameter("schema");
        if (jsonString == null) {
            respondError(response, "No Wikibase schema provided.");
            return;
        }
        WikibaseSchema schema = ParsingUtilities.mapper.readValue(jsonString, WikibaseSchema.class);
        AbstractOperation op = new SaveWikibaseSchemaOperation(schema);
        Process process = op.createProcess(project, new Properties());
        performProcessAndRespond(request, response, project, process);
    } catch (IOException e) {
        // We do not use respondException here because this is an expected
        // exception which happens every time a user tries to save an incomplete
        // schema - the exception should not be logged.
        respondError(response, String.format("Wikibase schema could not be parsed: ", e.getMessage()));
    } catch (Exception e) {
        // This is an unexpected exception, so we log it.
        respondException(response, e);
    }
}
Also used : Project(com.google.refine.model.Project) AbstractOperation(com.google.refine.model.AbstractOperation) SaveWikibaseSchemaOperation(org.openrefine.wikidata.operations.SaveWikibaseSchemaOperation) WikibaseSchema(org.openrefine.wikidata.schema.WikibaseSchema) Process(com.google.refine.process.Process) IOException(java.io.IOException) Properties(java.util.Properties) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 87 with Project

use of com.google.refine.model.Project in project OpenRefine by OpenRefine.

the class DatabaseImportController method doCreateProject.

/**
 * doCreateProject
 * @param request
 * @param response
 * @param parameters
 */
private void doCreateProject(HttpServletRequest request, HttpServletResponse response, Properties parameters) throws ServletException, IOException {
    if (logger.isDebugEnabled()) {
        logger.debug("DatabaseImportController::doCreateProject:::{}", parameters.getProperty("jobID"));
    }
    long jobID = Long.parseLong(parameters.getProperty("jobID"));
    final ImportingJob job = ImportingManager.getJob(jobID);
    if (job == null) {
        HttpUtilities.respond(response, "error", "No such import job");
        return;
    }
    final DatabaseQueryInfo databaseQueryInfo = getQueryInfo(request);
    if (databaseQueryInfo == null) {
        HttpUtilities.respond(response, "error", "Invalid or missing Query Info");
    }
    job.updating = true;
    try {
        final ObjectNode optionObj = ParsingUtilities.evaluateJsonStringToObjectNode(request.getParameter("options"));
        final List<Exception> exceptions = new LinkedList<Exception>();
        job.setState("creating-project");
        final Project project = new Project();
        new Thread() {

            @Override
            public void run() {
                ProjectMetadata pm = new ProjectMetadata();
                pm.setName(JSONUtilities.getString(optionObj, "projectName", "Untitled"));
                pm.setEncoding(JSONUtilities.getString(optionObj, "encoding", "UTF-8"));
                try {
                    parseCreate(databaseQueryInfo, project, pm, job, -1, optionObj, exceptions);
                } catch (DatabaseServiceException e) {
                    logger.error("DatabaseImportController::doCreateProject:::run{}", e);
                // throw new RuntimeException("DatabaseServiceException::", e);
                }
                if (!job.canceled) {
                    if (exceptions.size() > 0) {
                        job.setError(exceptions);
                    } else {
                        // update all internal models, indexes, caches, etc.
                        project.update();
                        ProjectManager.singleton.registerProject(project, pm);
                        job.setState("created-project");
                        job.setProjectID(project.id);
                    // logger.info("DatabaseImportController::doCreateProject:::run::projectID :{}", project.id);
                    }
                    job.touch();
                    job.updating = false;
                }
            }
        }.start();
        HttpUtilities.respond(response, "ok", "done");
    } catch (IOException e) {
        throw new ServletException(e);
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) LinkedList(java.util.LinkedList) ServletException(javax.servlet.ServletException) Project(com.google.refine.model.Project) ProjectMetadata(com.google.refine.ProjectMetadata) ImportingJob(com.google.refine.importing.ImportingJob) DatabaseQueryInfo(com.google.refine.extension.database.model.DatabaseQueryInfo)

Example 88 with Project

use of com.google.refine.model.Project in project OpenRefine by OpenRefine.

the class GetColumnsInfoCommand method doGet.

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Type", "application/json");
        Project project = getProject(request);
        JsonGenerator writer = ParsingUtilities.mapper.getFactory().createGenerator(response.getWriter());
        writer.writeStartArray();
        for (Column column : project.columnModel.columns) {
            writer.writeStartObject();
            write(project, column, writer);
            writer.writeEndObject();
        }
        writer.writeEndArray();
        writer.flush();
        writer.close();
    } catch (Exception e) {
        e.printStackTrace();
        respondException(response, e);
    }
}
Also used : Project(com.google.refine.model.Project) Column(com.google.refine.model.Column) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ParsingException(com.google.refine.expr.ParsingException)

Example 89 with Project

use of com.google.refine.model.Project in project OpenRefine by OpenRefine.

the class PreviewExpressionCommand method doPost.

/**
 * The command uses POST but does not actually modify any state so it does
 * not require CSRF.
 */
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        Project project = getProject(request);
        int cellIndex = Integer.parseInt(request.getParameter("cellIndex"));
        String columnName = cellIndex < 0 ? "" : project.columnModel.getColumnByCellIndex(cellIndex).getName();
        String expression = request.getParameter("expression");
        String rowIndicesString = request.getParameter("rowIndices");
        if (rowIndicesString == null) {
            respondJSON(response, new PreviewResult("error", "No row indices specified", null));
            return;
        }
        boolean repeat = "true".equals(request.getParameter("repeat"));
        int repeatCount = 10;
        if (repeat) {
            String repeatCountString = request.getParameter("repeatCount");
            try {
                repeatCount = Math.max(Math.min(Integer.parseInt(repeatCountString), 10), 0);
            } catch (Exception e) {
            }
        }
        List<Integer> rowIndices = ParsingUtilities.mapper.readValue(rowIndicesString, new TypeReference<List<Integer>>() {
        });
        int length = rowIndices.size();
        try {
            Evaluable eval = MetaParser.parse(expression);
            List<ExpressionValue> evaluated = new ArrayList<>();
            Properties bindings = ExpressionUtils.createBindings(project);
            for (int i = 0; i < length; i++) {
                Object result = null;
                int rowIndex = rowIndices.get(i);
                if (rowIndex >= 0 && rowIndex < project.rows.size()) {
                    Row row = project.rows.get(rowIndex);
                    Cell cell = row.getCell(cellIndex);
                    try {
                        ExpressionUtils.bind(bindings, row, rowIndex, columnName, cell);
                        result = eval.evaluate(bindings);
                        if (repeat) {
                            for (int r = 0; r < repeatCount && ExpressionUtils.isStorable(result); r++) {
                                Cell newCell = new Cell((Serializable) result, (cell != null) ? cell.recon : null);
                                ExpressionUtils.bind(bindings, row, rowIndex, columnName, newCell);
                                Object newResult = eval.evaluate(bindings);
                                if (ExpressionUtils.isError(newResult)) {
                                    break;
                                } else if (ExpressionUtils.sameValue(result, newResult)) {
                                    break;
                                } else {
                                    result = newResult;
                                }
                            }
                        }
                    } catch (Exception e) {
                    // ignore
                    }
                }
                if (result == null) {
                    evaluated.add(null);
                } else if (ExpressionUtils.isError(result)) {
                    evaluated.add(new ErrorMessage(((EvalError) result).message));
                } else {
                    StringBuffer sb = new StringBuffer();
                    writeValue(sb, result, false);
                    evaluated.add(new SuccessfulEvaluation(sb.toString()));
                }
            }
            respondJSON(response, new PreviewResult(evaluated));
        } catch (ParsingException e) {
            respondJSON(response, new PreviewResult("error", e.getMessage(), "parser"));
        } catch (Exception e) {
            respondJSON(response, new PreviewResult("error", e.getMessage(), "other"));
        }
    } catch (Exception e) {
        respondException(response, e);
    }
}
Also used : ArrayList(java.util.ArrayList) Properties(java.util.Properties) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ParsingException(com.google.refine.expr.ParsingException) Project(com.google.refine.model.Project) Evaluable(com.google.refine.expr.Evaluable) ParsingException(com.google.refine.expr.ParsingException) ArrayList(java.util.ArrayList) List(java.util.List) WrappedRow(com.google.refine.expr.WrappedRow) Row(com.google.refine.model.Row) Cell(com.google.refine.model.Cell) WrappedCell(com.google.refine.expr.WrappedCell)

Example 90 with Project

use of com.google.refine.model.Project in project OpenRefine by OpenRefine.

the class GetHistoryCommand method doGet.

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Project project = getProject(request);
    respondJSON(response, project.history);
}
Also used : Project(com.google.refine.model.Project)

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