Search in sources :

Example 1 with DatabaseQueryInfo

use of com.google.refine.extension.database.model.DatabaseQueryInfo in project OpenRefine by OpenRefine.

the class DatabaseImportController method doParsePreview.

/**
 * doParsePreview
 * @param request
 * @param response
 * @param parameters
 * @throws ServletException
 * @throws IOException
 * @throws DatabaseServiceException
 */
private void doParsePreview(HttpServletRequest request, HttpServletResponse response, Properties parameters) throws ServletException, IOException, DatabaseServiceException {
    if (logger.isDebugEnabled()) {
        logger.debug("JobID::{}", parameters.getProperty("jobID"));
    }
    long jobID = Long.parseLong(parameters.getProperty("jobID"));
    ImportingJob job = ImportingManager.getJob(jobID);
    if (job == null) {
        HttpUtilities.respond(response, "error", "No such import job");
        return;
    }
    DatabaseQueryInfo databaseQueryInfo = getQueryInfo(request);
    if (databaseQueryInfo == null) {
        HttpUtilities.respond(response, "error", "Invalid or missing Query Info");
    }
    job.updating = true;
    try {
        ObjectNode optionObj = ParsingUtilities.evaluateJsonStringToObjectNode(request.getParameter("options"));
        List<Exception> exceptions = new LinkedList<Exception>();
        job.prepareNewProject();
        parsePreview(databaseQueryInfo, job.project, job.metadata, job, DEFAULT_PREVIEW_LIMIT, optionObj, exceptions);
        Writer w = response.getWriter();
        JsonGenerator writer = ParsingUtilities.mapper.getFactory().createGenerator(w);
        try {
            writer.writeStartObject();
            if (exceptions.size() == 0) {
                // update all internal models, indexes, caches, etc.
                job.project.update();
                writer.writeStringField("status", "ok");
            } else {
                writer.writeStringField("status", "error");
                writer.writeStringField("message", getExceptionString(exceptions));
            }
            writer.writeEndObject();
        } catch (IOException e) {
            throw new ServletException(e);
        } finally {
            writer.flush();
            writer.close();
            w.flush();
            w.close();
        }
    } catch (IOException e) {
        throw new ServletException(e);
    } finally {
        job.touch();
        job.updating = false;
    }
}
Also used : ServletException(javax.servlet.ServletException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ImportingJob(com.google.refine.importing.ImportingJob) DatabaseQueryInfo(com.google.refine.extension.database.model.DatabaseQueryInfo) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) LinkedList(java.util.LinkedList) Writer(java.io.Writer)

Example 2 with DatabaseQueryInfo

use of com.google.refine.extension.database.model.DatabaseQueryInfo 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)

Aggregations

ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 DatabaseQueryInfo (com.google.refine.extension.database.model.DatabaseQueryInfo)2 ImportingJob (com.google.refine.importing.ImportingJob)2 IOException (java.io.IOException)2 LinkedList (java.util.LinkedList)2 ServletException (javax.servlet.ServletException)2 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 ProjectMetadata (com.google.refine.ProjectMetadata)1 Project (com.google.refine.model.Project)1 Writer (java.io.Writer)1