Search in sources :

Example 1 with GeneratedResource

use of org.activityinfo.server.generated.GeneratedResource in project activityinfo by bedatadriven.

the class ExportAuditLogExecutor method execute.

@Override
public ExportResult execute(ExportAuditLog descriptor) throws IOException {
    UserDatabaseDTO db = dispatcher.execute(new GetSchema()).getDatabaseById(descriptor.getDatabaseId());
    GeneratedResource export = storageProvider.create("text/csv;charset=UTF-8", String.format("AuditLog_%d_%s.csv", db.getId(), Filenames.timestamp()));
    try (CsvWriter writer = new CsvWriter(new OutputStreamWriter(export.openOutputStream(), Charsets.UTF_8))) {
        AuditLogWriter logWriter = new AuditLogWriter(entityManager.get(), db, writer);
        for (ActivityDTO activityDTO : db.getActivities()) {
            logWriter.writeForm(catalog, activityDTO.getFormId());
        }
    }
    return new ExportResult(export.getDownloadUri());
}
Also used : CsvWriter(org.activityinfo.server.endpoint.rest.CsvWriter) UserDatabaseDTO(org.activityinfo.legacy.shared.model.UserDatabaseDTO) OutputStreamWriter(java.io.OutputStreamWriter) GeneratedResource(org.activityinfo.server.generated.GeneratedResource) ActivityDTO(org.activityinfo.legacy.shared.model.ActivityDTO) GetSchema(org.activityinfo.legacy.shared.command.GetSchema) ExportResult(org.activityinfo.model.job.ExportResult)

Example 2 with GeneratedResource

use of org.activityinfo.server.generated.GeneratedResource in project activityinfo by bedatadriven.

the class ExportFormExecutor method execute.

@Override
public ExportResult execute(ExportFormJob descriptor) throws IOException {
    TableModel tableModel = descriptor.getTableModel();
    GeneratedResource export = storageProvider.create(XlsTableWriter.EXCEL_MIME_TYPE, "Export.xls");
    TableViewModel viewModel = new TableViewModel(formSource, tableModel);
    EffectiveTableModel effectiveTableModel = viewModel.getEffectiveTable().waitFor();
    ColumnSet columnSet = effectiveTableModel.getColumnSet().waitFor();
    XlsTableWriter writer = new XlsTableWriter();
    writer.addSheet(effectiveTableModel, columnSet);
    try (OutputStream out = export.openOutputStream()) {
        writer.write(out);
    }
    return new ExportResult(export.getDownloadUri());
}
Also used : EffectiveTableModel(org.activityinfo.analysis.table.EffectiveTableModel) XlsTableWriter(org.activityinfo.io.xls.XlsTableWriter) TableViewModel(org.activityinfo.analysis.table.TableViewModel) OutputStream(java.io.OutputStream) ColumnSet(org.activityinfo.model.query.ColumnSet) GeneratedResource(org.activityinfo.server.generated.GeneratedResource) EffectiveTableModel(org.activityinfo.analysis.table.EffectiveTableModel) TableModel(org.activityinfo.model.analysis.TableModel) ExportResult(org.activityinfo.model.job.ExportResult)

Example 3 with GeneratedResource

use of org.activityinfo.server.generated.GeneratedResource in project activityinfo by bedatadriven.

the class ChartRendererJC method renderToUrl.

public String renderToUrl(PivotChartReportElement element, boolean includeTitle, StorageProvider istorageProvider, int width, int height, int dpi) throws IOException {
    try {
        Chart chart = createChart(element, includeTitle, width, height, dpi);
        GeneratedResource storage = istorageProvider.create(null, "activityinfo.jpg");
        JPEGEncoder.encode(chart, 0.75f, storage.openOutputStream());
        return storage.getDownloadUri();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
Also used : GeneratedResource(org.activityinfo.server.generated.GeneratedResource) AxisChart(org.krysalis.jcharts.axisChart.AxisChart) Chart(org.krysalis.jcharts.Chart) ChartDataException(org.krysalis.jcharts.chartData.ChartDataException) ReportModelException(org.activityinfo.legacy.shared.exception.ReportModelException) IOException(java.io.IOException)

Example 4 with GeneratedResource

use of org.activityinfo.server.generated.GeneratedResource in project activityinfo by bedatadriven.

the class RenderElementHandler method execute.

@Override
public CommandResult execute(RenderElement cmd, User user) throws CommandException {
    try {
        Renderer renderer = rendererFactory.get(cmd.getFormat());
        GeneratedResource storage = storageProvider.create(renderer.getMimeType(), cmd.getFilename() + renderer.getFileSuffix());
        LOGGER.fine("Rendering element: " + cmd + "\nURL: " + storage.getDownloadUri());
        try (OutputStream out = storage.openOutputStream()) {
            generator.generateElement(user, cmd.getElement(), new Filter(), new DateRange());
            renderer.render(cmd.getElement(), out);
        }
        return new UrlResult(storage.getDownloadUri());
    } catch (Exception e) {
        throw new RuntimeException("Exception generating export", e);
    }
}
Also used : DateRange(org.activityinfo.legacy.shared.reports.model.DateRange) Filter(org.activityinfo.legacy.shared.command.Filter) OutputStream(java.io.OutputStream) Renderer(org.activityinfo.server.report.renderer.Renderer) GeneratedResource(org.activityinfo.server.generated.GeneratedResource) UrlResult(org.activityinfo.legacy.shared.command.result.UrlResult) CommandException(org.activityinfo.legacy.shared.exception.CommandException)

Example 5 with GeneratedResource

use of org.activityinfo.server.generated.GeneratedResource in project activityinfo by bedatadriven.

the class ExportSitesServlet method doPost.

/**
 * Initiates an export to Excel task. A token is send back to the client as plain text
 * that can be use to poll the status of the export and retrieve the result.
 */
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    // Create a unique key from which the user can retrieve the file from GCS
    GeneratedResource export = storageProvider.create(XlsTableWriter.EXCEL_MIME_TYPE, fileName());
    TaskOptions options = TaskOptions.Builder.withUrl(ExportSitesTask.END_POINT);
    for (Map.Entry<String, String[]> entry : req.getParameterMap().entrySet()) {
        options.param(entry.getKey(), entry.getValue()[0]);
    }
    options.param("userId", Integer.toString(authenticatedUserProvider.get().getId()));
    options.param("userEmail", authenticatedUserProvider.get().getEmail());
    options.param("exportId", export.getId());
    options.param("filename", fileName());
    options.retryOptions(RetryOptions.Builder.withTaskRetryLimit(3));
    QueueFactory.getDefaultQueue().add(options);
    LOGGER.info("Enqueued export with id " + export.getId() + " on behalf of " + authenticatedUserProvider.get().getEmail());
    resp.setStatus(HttpServletResponse.SC_ACCEPTED);
    resp.getOutputStream().print(export.getId());
}
Also used : TaskOptions(com.google.appengine.api.taskqueue.TaskOptions) GeneratedResource(org.activityinfo.server.generated.GeneratedResource) Map(java.util.Map)

Aggregations

GeneratedResource (org.activityinfo.server.generated.GeneratedResource)7 OutputStream (java.io.OutputStream)4 Filter (org.activityinfo.legacy.shared.command.Filter)2 ExportResult (org.activityinfo.model.job.ExportResult)2 TaskOptions (com.google.appengine.api.taskqueue.TaskOptions)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Map (java.util.Map)1 EffectiveTableModel (org.activityinfo.analysis.table.EffectiveTableModel)1 TableViewModel (org.activityinfo.analysis.table.TableViewModel)1 XlsTableWriter (org.activityinfo.io.xls.XlsTableWriter)1 AuthenticatedUser (org.activityinfo.legacy.shared.AuthenticatedUser)1 GetSchema (org.activityinfo.legacy.shared.command.GetSchema)1 UrlResult (org.activityinfo.legacy.shared.command.result.UrlResult)1 CommandException (org.activityinfo.legacy.shared.exception.CommandException)1 ReportModelException (org.activityinfo.legacy.shared.exception.ReportModelException)1 ActivityDTO (org.activityinfo.legacy.shared.model.ActivityDTO)1 UserDatabaseDTO (org.activityinfo.legacy.shared.model.UserDatabaseDTO)1 MapContent (org.activityinfo.legacy.shared.reports.content.MapContent)1 DateRange (org.activityinfo.legacy.shared.reports.model.DateRange)1