Search in sources :

Example 21 with Project

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

the class SortingRecordVisitor method end.

@Override
public void end(Project project) {
    _visitor.start(project);
    Collections.sort(_records, new Comparator<Record>() {

        Project project;

        Comparator<Record> init(Project project) {
            this.project = project;
            return this;
        }

        @Override
        public int compare(Record o1, Record o2) {
            return SortingRecordVisitor.this.compare(project, o1, o1.recordIndex, o2, o2.recordIndex);
        }
    }.init(project));
    for (Record record : _records) {
        _visitor.visit(project, record);
    }
    _visitor.end(project);
}
Also used : Project(com.google.refine.model.Project) Record(com.google.refine.model.Record) Comparator(java.util.Comparator)

Example 22 with Project

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

the class TimeRangeFacetTests method serializeTimeRangeFacet.

@Test
public void serializeTimeRangeFacet() throws JsonParseException, JsonMappingException, IOException {
    Project project = createCSVProject("my column\n" + "placeholder\n" + "nontime\n" + "placeholder\n" + "placeholder\n");
    project.rows.get(0).cells.set(0, new Cell(OffsetDateTime.parse("2018-01-03T08:09:10Z"), null));
    project.rows.get(2).cells.set(0, new Cell(OffsetDateTime.parse("2008-01-03T03:04:05Z"), null));
    project.rows.get(3).cells.set(0, new Cell(OffsetDateTime.parse("2012-04-05T02:00:01Z"), null));
    Engine engine = new Engine(project);
    TimeRangeFacetConfig config = ParsingUtilities.mapper.readValue(configJson, TimeRangeFacetConfig.class);
    TimeRangeFacet facet = config.apply(project);
    facet.computeChoices(project, engine.getAllFilteredRows());
    TestUtils.isSerializedTo(facet, facetJson);
}
Also used : Project(com.google.refine.model.Project) Cell(com.google.refine.model.Cell) TimeRangeFacet(com.google.refine.browsing.facets.TimeRangeFacet) Engine(com.google.refine.browsing.Engine) TimeRangeFacetConfig(com.google.refine.browsing.facets.TimeRangeFacet.TimeRangeFacetConfig) RefineTest(com.google.refine.RefineTest) Test(org.testng.annotations.Test)

Example 23 with Project

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

the class ExportRowsCommand method doPost.

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    ProjectManager.singleton.setBusy(true);
    try {
        Project project = getProject(request);
        Engine engine = getEngine(request, project);
        Properties params = getRequestParameters(request);
        String format = params.getProperty("format");
        Exporter exporter = ExporterRegistry.getExporter(format);
        if (exporter == null) {
            exporter = new CsvExporter('\t');
        }
        String contentType = params.getProperty("contentType");
        if (contentType == null) {
            contentType = exporter.getContentType();
        }
        response.setHeader("Content-Type", contentType);
        String preview = params.getProperty("preview");
        if (!"true".equals(preview)) {
            String path = request.getPathInfo();
            String filename = path.substring(path.lastIndexOf('/') + 1);
            PercentEscaper escaper = new PercentEscaper("", false);
            filename = escaper.escape(filename);
            response.setHeader("Content-Disposition", "attachment; filename=" + filename + "; filename*=utf-8' '" + filename);
        }
        if (exporter instanceof WriterExporter) {
            String encoding = params.getProperty("encoding");
            response.setCharacterEncoding(encoding != null ? encoding : "UTF-8");
            Writer writer = encoding == null ? response.getWriter() : new OutputStreamWriter(response.getOutputStream(), encoding);
            ((WriterExporter) exporter).export(project, params, engine, writer);
            writer.close();
        } else if (exporter instanceof StreamExporter) {
            response.setCharacterEncoding("UTF-8");
            OutputStream stream = response.getOutputStream();
            ((StreamExporter) exporter).export(project, params, engine, stream);
            stream.close();
        // } else if (exporter instanceof UrlExporter) {
        // ((UrlExporter) exporter).export(project, options, engine);
        } else {
            // TODO: Should this use ServletException instead of respondException?
            respondException(response, new RuntimeException("Unknown exporter type"));
        }
    } catch (Exception e) {
        // Use generic error handling rather than our JSON handling
        logger.info("error:{}", e.getMessage());
        if (e instanceof SqlExporterException) {
            response.sendError(HttpStatus.SC_BAD_REQUEST, e.getMessage());
        }
        throw new ServletException(e);
    } finally {
        ProjectManager.singleton.setBusy(false);
    }
}
Also used : WriterExporter(com.google.refine.exporters.WriterExporter) StreamExporter(com.google.refine.exporters.StreamExporter) OutputStream(java.io.OutputStream) Properties(java.util.Properties) WriterExporter(com.google.refine.exporters.WriterExporter) Exporter(com.google.refine.exporters.Exporter) StreamExporter(com.google.refine.exporters.StreamExporter) CsvExporter(com.google.refine.exporters.CsvExporter) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) SqlExporterException(com.google.refine.exporters.sql.SqlExporterException) PercentEscaper(com.google.common.net.PercentEscaper) ServletException(javax.servlet.ServletException) Project(com.google.refine.model.Project) SqlExporterException(com.google.refine.exporters.sql.SqlExporterException) CsvExporter(com.google.refine.exporters.CsvExporter) OutputStreamWriter(java.io.OutputStreamWriter) Engine(com.google.refine.browsing.Engine) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 24 with Project

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

the class SetProjectMetadataCommand method doPost.

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    if (!hasValidCSRFToken(request)) {
        respondCSRFError(response);
        return;
    }
    Project project = request.getParameter("project") != null ? getProject(request) : null;
    String metaName = request.getParameter("name");
    String valueString = request.getParameter("value");
    ProjectMetadata meta = null;
    if (project == null) {
        respond(response, "{ \"code\" : \"error\", \"message\" : \"Project cannot be found\" }");
        return;
    }
    meta = project.getMetadata();
    try {
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Type", "application/json");
        meta.setAnyField(metaName, valueString);
        ProjectManager.singleton.saveMetadata(meta, project.id);
        respond(response, "{ \"code\" : \"ok\" }");
    } catch (Exception e) {
        respondException(response, e);
    }
}
Also used : Project(com.google.refine.model.Project) ProjectMetadata(com.google.refine.ProjectMetadata) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 25 with Project

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

the class PreviewExtendDataCommand 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 rowIndicesString = request.getParameter("rowIndices");
        if (rowIndicesString == null) {
            respond(response, "{ \"code\" : \"error\", \"message\" : \"No row indices specified\" }");
            return;
        }
        String jsonString = request.getParameter("extension");
        DataExtensionConfig config = DataExtensionConfig.reconstruct(jsonString);
        List<Integer> rowIndices = ParsingUtilities.mapper.readValue(rowIndicesString, new TypeReference<List<Integer>>() {
        });
        int length = rowIndices.size();
        Column column = project.columnModel.getColumnByName(columnName);
        int cellIndex = column.getCellIndex();
        // get the endpoint to extract data from
        String endpoint = null;
        ReconConfig cfg = column.getReconConfig();
        if (cfg != null && cfg instanceof StandardReconConfig) {
            StandardReconConfig scfg = (StandardReconConfig) cfg;
            endpoint = scfg.service;
        } else {
            respond(response, "{ \"code\" : \"error\", \"message\" : \"This column has not been reconciled with a standard service.\" }");
            return;
        }
        List<String> topicNames = new ArrayList<String>();
        List<String> topicIds = new ArrayList<String>();
        Set<String> ids = new HashSet<String>();
        for (int i = 0; i < length; i++) {
            int rowIndex = rowIndices.get(i);
            if (rowIndex >= 0 && rowIndex < project.rows.size()) {
                Row row = project.rows.get(rowIndex);
                Cell cell = row.getCell(cellIndex);
                if (cell != null && cell.recon != null && cell.recon.match != null) {
                    topicNames.add(cell.recon.match.name);
                    topicIds.add(cell.recon.match.id);
                    ids.add(cell.recon.match.id);
                } else {
                    topicNames.add(null);
                    topicIds.add(null);
                    ids.add(null);
                }
            }
        }
        Map<String, ReconCandidate> reconCandidateMap = new HashMap<String, ReconCandidate>();
        ReconciledDataExtensionJob job = new ReconciledDataExtensionJob(config, endpoint);
        Map<String, DataExtension> map = job.extend(ids, reconCandidateMap);
        List<List<Object>> rows = new ArrayList<>();
        for (int r = 0; r < topicNames.size(); r++) {
            String id = topicIds.get(r);
            String topicName = topicNames.get(r);
            if (id != null && map.containsKey(id)) {
                DataExtension ext = map.get(id);
                boolean first = true;
                if (ext.data.length > 0) {
                    for (Object[] row : ext.data) {
                        List<Object> jsonRow = new ArrayList<>();
                        if (first) {
                            jsonRow.add(topicName);
                            first = false;
                        } else {
                            jsonRow.add(null);
                        }
                        for (Object cell : row) {
                            jsonRow.add(cell);
                        }
                        rows.add(jsonRow);
                    }
                    continue;
                }
            }
            List<Object> supplement = new ArrayList<>();
            if (id != null) {
                supplement.add(new ReconCandidate(id, topicName, new String[0], 100));
            } else {
                supplement.add("<not reconciled>");
            }
            rows.add(supplement);
        }
        respondJSON(response, new PreviewResponse(job.columns, rows));
    } catch (Exception e) {
        respondException(response, e);
    }
}
Also used : ReconciledDataExtensionJob(com.google.refine.model.recon.ReconciledDataExtensionJob) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DataExtension(com.google.refine.model.recon.ReconciledDataExtensionJob.DataExtension) Column(com.google.refine.model.Column) ReconConfig(com.google.refine.model.recon.ReconConfig) StandardReconConfig(com.google.refine.model.recon.StandardReconConfig) ArrayList(java.util.ArrayList) List(java.util.List) Cell(com.google.refine.model.Cell) ReconCandidate(com.google.refine.model.ReconCandidate) HashSet(java.util.HashSet) DataExtensionConfig(com.google.refine.model.recon.ReconciledDataExtensionJob.DataExtensionConfig) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) Project(com.google.refine.model.Project) StandardReconConfig(com.google.refine.model.recon.StandardReconConfig) Row(com.google.refine.model.Row)

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