Search in sources :

Example 1 with DataExportException

use of org.motechproject.mds.exception.csv.DataExportException in project motech by motech.

the class PdfTableWriter method close.

@Override
public void close() {
    try {
        float[] relativeWidths = getRelativeWidths();
        List<Integer> lastColumnsForPages = calculateLastColumnsForPages(relativeWidths);
        resizeColumns(relativeWidths, lastColumnsForPages);
        setTableContentOffset(relativeWidths[0]);
        dataTable.setWidths(relativeWidths);
        dataTable.setLockedWidth(true);
        dataTable.setTotalWidth(calculateTotalTableWidth(relativeWidths));
        writeTable(lastColumnsForPages);
        pdfDocument.close();
    } catch (DocumentException e) {
        throw new DataExportException("Unable to add a table to the PDF file", e);
    } finally {
        pdfWriter.close();
    }
}
Also used : DocumentException(com.itextpdf.text.DocumentException) DataExportException(org.motechproject.mds.exception.csv.DataExportException)

Example 2 with DataExportException

use of org.motechproject.mds.exception.csv.DataExportException in project motech by motech.

the class AbstractMdsExporter method exportData.

protected long exportData(EntityInfo entityInfo, TableWriter writer, String lookupName, QueryParams params, List<String> headers, Map<String, Object> lookupFields, CsvExportCustomizer exportCustomizer) {
    final MotechDataService dataService = DataServiceHelper.getDataService(bundleContext, entityInfo.getClassName());
    final Map<String, FieldDto> fieldMap = new HashMap<>();
    for (FieldDto field : entityInfo.getFieldDtos()) {
        fieldMap.put(exportCustomizer.exportDisplayName(field), field);
    }
    // we must respect field ordering
    String[] orderedHeaders = orderHeaders(entityInfo.getAdvancedSettings().getBrowsing(), headers == null ? fieldsToHeaders(entityInfo.getFieldDtos(), exportCustomizer) : headers.toArray(new String[headers.size()]), entityInfo.getFieldDtos(), exportCustomizer);
    try {
        writer.writeHeader(orderedHeaders);
        long rowsExported = 0;
        Map<String, String> row = new HashMap<>();
        List<Object> instances = StringUtils.isBlank(lookupName) ? dataService.retrieveAll(params) : mdsLookupService.findMany(entityInfo.getClassName(), lookupName, lookupFields, params);
        for (Object instance : instances) {
            buildCsvRow(row, fieldMap, instance, orderedHeaders, exportCustomizer);
            writer.writeRow(row, orderedHeaders);
            rowsExported++;
        }
        return rowsExported;
    } catch (IOException e) {
        throw new DataExportException("IO Error when writing data", e);
    }
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) DataExportException(org.motechproject.mds.exception.csv.DataExportException) MotechDataService(org.motechproject.mds.service.MotechDataService) FieldDto(org.motechproject.mds.dto.FieldDto)

Aggregations

DataExportException (org.motechproject.mds.exception.csv.DataExportException)2 DocumentException (com.itextpdf.text.DocumentException)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 FieldDto (org.motechproject.mds.dto.FieldDto)1 MotechDataService (org.motechproject.mds.service.MotechDataService)1