Search in sources :

Example 21 with AbstractOperation

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

the class EngineDependentCommand method doPost.

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    if (!hasValidCSRFToken(request)) {
        respondCSRFError(response);
        return;
    }
    try {
        Project project = getProject(request);
        AbstractOperation op = createOperation(project, request, getEngineConfig(request));
        Process process = op.createProcess(project, new Properties());
        performProcessAndRespond(request, response, project, process);
    } catch (Exception e) {
        respondException(response, e);
    }
}
Also used : Project(com.google.refine.model.Project) AbstractOperation(com.google.refine.model.AbstractOperation) Process(com.google.refine.process.Process) Properties(java.util.Properties) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 22 with AbstractOperation

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

the class OperationRegistry method reconstruct.

public static AbstractOperation reconstruct(Project project, JSONObject obj) {
    try {
        String op = obj.getString("op");
        if (!op.contains("/")) {
            // backward compatible
            op = "core/" + op;
        }
        List<Class<? extends AbstractOperation>> classes = s_opNameToClass.get(op);
        if (classes != null && classes.size() > 0) {
            Class<? extends AbstractOperation> klass = classes.get(classes.size() - 1);
            Method reconstruct = klass.getMethod("reconstruct", Project.class, JSONObject.class);
            if (reconstruct != null) {
                return (AbstractOperation) reconstruct.invoke(null, project, obj);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : AbstractOperation(com.google.refine.model.AbstractOperation) Method(java.lang.reflect.Method)

Example 23 with AbstractOperation

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

the class TransposeTests method keyValueComumnize.

@Test
public void keyValueComumnize() throws Exception {
    String input = "ID;Cat;Val\n" + "1;a;1\n" + "1;b;3\n" + "2;b;4\n" + "2;c;5\n" + "3;a;2\n" + "3;b;5\n" + "3;d;3\n";
    prepareOptions(";", -1, 0, 0, 1, false, false);
    List<Exception> exceptions = new ArrayList<Exception>();
    importer.parseOneFile(project, metadata, job, "filesource", new StringReader(input), -1, options, exceptions);
    project.update();
    ProjectManager.singleton.registerProject(project, metadata);
    AbstractOperation op = new KeyValueColumnizeOperation("Cat", "Val", null);
    Process process = op.createProcess(project, new Properties());
    HistoryEntry historyEntry = process.performImmediate();
    // Expected output from the GUI. 
    // ID;a;b;c;d
    // 1;1;3;;
    // 2;;4;5;
    // 3;2;5;;3
    Assert.assertEquals(project.columnModel.columns.size(), 5);
    Assert.assertEquals(project.columnModel.columns.get(0).getName(), "ID");
    Assert.assertEquals(project.columnModel.columns.get(1).getName(), "a");
    Assert.assertEquals(project.columnModel.columns.get(2).getName(), "b");
    Assert.assertEquals(project.columnModel.columns.get(3).getName(), "c");
    Assert.assertEquals(project.columnModel.columns.get(4).getName(), "d");
    Assert.assertEquals(project.rows.size(), 3);
    // The actual row data structure has to leave the columns model untouched for redo/undo purpose.
    // So we have 2 empty columns(column 1,2) on the row level.
    // 1;1;3;;
    Assert.assertEquals(project.rows.get(0).cells.get(0).value, "1");
    Assert.assertEquals(project.rows.get(0).cells.get(3).value, "1");
    Assert.assertEquals(project.rows.get(0).cells.get(4).value, "3");
    // 2;;4;5;
    Assert.assertEquals(project.rows.get(1).cells.get(0).value, "2");
    Assert.assertEquals(project.rows.get(1).cells.get(4).value, "4");
    Assert.assertEquals(project.rows.get(1).cells.get(5).value, "5");
    // 3;2;5;;3
    Assert.assertEquals(project.rows.get(2).cells.get(0).value, "3");
    Assert.assertEquals(project.rows.get(2).cells.get(3).value, "2");
    Assert.assertEquals(project.rows.get(2).cells.get(4).value, "5");
    Assert.assertEquals(project.rows.get(2).cells.get(6).value, "3");
}
Also used : AbstractOperation(com.google.refine.model.AbstractOperation) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) HistoryEntry(com.google.refine.history.HistoryEntry) Process(com.google.refine.process.Process) Properties(java.util.Properties) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) RefineTest(com.google.refine.tests.RefineTest)

Example 24 with AbstractOperation

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

the class OperationTest method testReconstruct.

@Test
public void testReconstruct() throws Exception {
    String json = getJson();
    AbstractOperation op = reconstruct();
    StringWriter writer = new StringWriter();
    ParsingUtilities.defaultWriter.writeValue(writer, op);
    TestUtils.assertEqualAsJson(json, writer.toString());
}
Also used : AbstractOperation(com.google.refine.model.AbstractOperation) StringWriter(java.io.StringWriter) WikidataRefineTest(org.openrefine.wikidata.testing.WikidataRefineTest) Test(org.testng.annotations.Test)

Example 25 with AbstractOperation

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

Aggregations

AbstractOperation (com.google.refine.model.AbstractOperation)42 Process (com.google.refine.process.Process)36 Properties (java.util.Properties)36 Test (org.testng.annotations.Test)27 RefineTest (com.google.refine.RefineTest)25 Project (com.google.refine.model.Project)14 IOException (java.io.IOException)14 BeforeTest (org.testng.annotations.BeforeTest)14 ServletException (javax.servlet.ServletException)13 ArrayList (java.util.ArrayList)4 Column (com.google.refine.model.Column)3 HistoryEntry (com.google.refine.history.HistoryEntry)2 Cell (com.google.refine.model.Cell)2 Row (com.google.refine.model.Row)2 StringReader (java.io.StringReader)2 ProjectMetadata (com.google.refine.ProjectMetadata)1 Change (com.google.refine.history.Change)1 ModelException (com.google.refine.model.ModelException)1 ReconConfig (com.google.refine.model.recon.ReconConfig)1 StandardReconConfig (com.google.refine.model.recon.StandardReconConfig)1