Search in sources :

Example 41 with Project

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

the class GuessTypesOfColumnCommand 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 columnName = request.getParameter("columnName");
        String serviceUrl = request.getParameter("service");
        Column column = project.columnModel.getColumnByName(columnName);
        if (column == null) {
            respondJSON(response, new TypesResponse("error", "No such column", null));
        } else {
            List<TypeGroup> typeGroups = guessTypes(project, column, serviceUrl);
            respondJSON(response, new TypesResponse("ok", null, typeGroups));
        }
    } catch (Exception e) {
        respondException(response, e);
    }
}
Also used : Project(com.google.refine.model.Project) Column(com.google.refine.model.Column) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 42 with Project

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

the class PreviewWikibaseSchemaCommand method doPost.

/**
 * This command uses POST but is left CSRF-unprotected since it does not
 * incur a side effect or state change in the backend.
 * The reason why it uses POST is to make sure large schemas and engines
 * can be passed as parameters.
 */
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        Project project = getProject(request);
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Type", "application/json");
        String schemaJson = request.getParameter("schema");
        WikibaseSchema schema = null;
        if (schemaJson != null) {
            try {
                schema = WikibaseSchema.reconstruct(schemaJson);
            } catch (IOException e) {
                respondError(response, "Wikibase schema could not be parsed. Error message: " + e.getMessage());
                return;
            }
        } else {
            schema = (WikibaseSchema) project.overlayModels.get("wikibaseSchema");
        }
        if (schema == null) {
            respondError(response, "No Wikibase schema provided.");
            return;
        }
        Manifest manifest = null;
        String manifestJson = request.getParameter("manifest");
        if (manifestJson != null) {
            try {
                manifest = ManifestParser.parse(manifestJson);
            } catch (ManifestException e) {
                respondError(response, "Wikibase manifest could not be parsed. Error message: " + e.getMessage());
                return;
            }
        }
        if (manifest == null) {
            respondError(response, "No Wikibase manifest provided.");
            return;
        }
        QAWarningStore warningStore = new QAWarningStore();
        // Evaluate project
        Engine engine = getEngine(request, project);
        List<TermedStatementEntityEdit> editBatch = schema.evaluate(project, engine, warningStore);
        // Inspect the edits and generate warnings
        EditInspector inspector = new EditInspector(warningStore, manifest);
        inspector.inspect(editBatch, schema);
        // Dump the first 10 edits, scheduled with the default scheduler
        WikibaseAPIUpdateScheduler scheduler = new WikibaseAPIUpdateScheduler();
        List<TermedStatementEntityEdit> nonNullEdits = scheduler.schedule(editBatch).stream().filter(e -> !e.isNull()).collect(Collectors.toList());
        List<TermedStatementEntityEdit> firstEdits = nonNullEdits.stream().limit(10).collect(Collectors.toList());
        PreviewResults previewResults = new PreviewResults(warningStore.getWarnings(), warningStore.getMaxSeverity(), warningStore.getNbWarnings(), nonNullEdits.size(), firstEdits);
        respondJSON(response, previewResults);
    } catch (Exception e) {
        respondException(response, e);
    }
}
Also used : Project(com.google.refine.model.Project) ServletException(javax.servlet.ServletException) ManifestException(org.openrefine.wikidata.manifests.ManifestException) TermedStatementEntityEdit(org.openrefine.wikidata.updates.TermedStatementEntityEdit) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) EditInspector(org.openrefine.wikidata.qa.EditInspector) Collectors(java.util.stream.Collectors) ManifestParser(org.openrefine.wikidata.manifests.ManifestParser) List(java.util.List) HttpServletRequest(javax.servlet.http.HttpServletRequest) WikibaseSchema(org.openrefine.wikidata.schema.WikibaseSchema) WikibaseAPIUpdateScheduler(org.openrefine.wikidata.updates.scheduler.WikibaseAPIUpdateScheduler) CommandUtilities.respondError(org.openrefine.wikidata.commands.CommandUtilities.respondError) QAWarningStore(org.openrefine.wikidata.qa.QAWarningStore) Command(com.google.refine.commands.Command) Engine(com.google.refine.browsing.Engine) Manifest(org.openrefine.wikidata.manifests.Manifest) IOException(java.io.IOException) Manifest(org.openrefine.wikidata.manifests.Manifest) ManifestException(org.openrefine.wikidata.manifests.ManifestException) ServletException(javax.servlet.ServletException) ManifestException(org.openrefine.wikidata.manifests.ManifestException) IOException(java.io.IOException) Project(com.google.refine.model.Project) EditInspector(org.openrefine.wikidata.qa.EditInspector) WikibaseAPIUpdateScheduler(org.openrefine.wikidata.updates.scheduler.WikibaseAPIUpdateScheduler) TermedStatementEntityEdit(org.openrefine.wikidata.updates.TermedStatementEntityEdit) WikibaseSchema(org.openrefine.wikidata.schema.WikibaseSchema) QAWarningStore(org.openrefine.wikidata.qa.QAWarningStore) Engine(com.google.refine.browsing.Engine)

Example 43 with Project

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

the class GDataImportingController method doCreateProject.

private void doCreateProject(HttpServletRequest request, HttpServletResponse response, Properties parameters) throws ServletException, IOException {
    final String token = TokenCookie.getToken(request);
    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;
    }
    job.updating = true;
    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 {
                GDataImporter.parse(token, project, pm, job, -1, optionObj, exceptions);
            } catch (IOException e) {
                logger.error(ExceptionUtils.getStackTrace(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);
                }
                job.touch();
                job.updating = false;
            }
        }
    }.start();
    HttpUtilities.respond(response, "ok", "done");
}
Also used : Project(com.google.refine.model.Project) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ProjectMetadata(com.google.refine.ProjectMetadata) ImportingJob(com.google.refine.importing.ImportingJob) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) LinkedList(java.util.LinkedList)

Example 44 with Project

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

the class JythonEvaluableTest method testJythonConcurrent.

@Test
public void testJythonConcurrent() {
    Properties props = new Properties();
    Project project = new Project();
    Row row = new Row(2);
    row.setCell(0, new Cell("one", null));
    row.setCell(0, new Cell("1", null));
    props.put("columnName", "number");
    props.put("true", "true");
    props.put("false", "false");
    props.put("rowIndex", "0");
    props.put("value", 1);
    props.put("project", project);
    props.put("call", "number");
    props.put("PI", "3.141592654");
    props.put("cells", new CellTuple(project, row));
    Evaluable eval1 = new JythonEvaluable("a = value\nreturn a * 2");
    Long value1 = (Long) eval1.evaluate(props);
    // create some unrelated evaluable
    new JythonEvaluable("a = value\nreturn a * 10");
    // repeat same previous test
    Long value2 = (Long) eval1.evaluate(props);
    Assert.assertEquals(value1, value2);
}
Also used : Project(com.google.refine.model.Project) Evaluable(com.google.refine.expr.Evaluable) CellTuple(com.google.refine.expr.CellTuple) Row(com.google.refine.model.Row) Properties(java.util.Properties) Cell(com.google.refine.model.Cell) Test(org.testng.annotations.Test)

Example 45 with Project

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

the class SavedConnectionCommandTest method setUp.

@BeforeMethod
public void setUp() throws IOException {
    MockitoAnnotations.initMocks(this);
    File dir = DBExtensionTestUtils.createTempDirectory("OR_DBExtension_Test_WorkspaceDir");
    FileProjectManager.initialize(dir);
    servlet = new RefineDbServletStub();
    ImportingManager.initialize(servlet);
    project = new Project();
    metadata = new ProjectMetadata();
    // job = ImportingManager.createJob();
    metadata.setName("Save DB Config  Test Project");
    ProjectManager.singleton.registerProject(project, metadata);
    SUT = new SavedConnectionCommand();
}
Also used : Project(com.google.refine.model.Project) ProjectMetadata(com.google.refine.ProjectMetadata) RefineDbServletStub(com.google.refine.extension.database.stub.RefineDbServletStub) File(java.io.File) BeforeMethod(org.testng.annotations.BeforeMethod)

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