Search in sources :

Example 91 with TDPException

use of org.talend.dataprep.exception.TDPException in project data-prep by Talend.

the class XlsSchemaParser method parseAllSheetsOldFormat.

/**
 * Parse all xls sheets for old excel document type
 *
 * @param request the xls request.
 * @return The parsed sheets request.
 */
private List<Schema.SheetContent> parseAllSheetsOldFormat(Request request) {
    final Marker marker = Markers.dataset(request.getMetadata().getId());
    try {
        InputStream inputStream = request.getContent();
        if (!inputStream.markSupported()) {
            inputStream = new PushbackInputStream(inputStream, 8);
        }
        Workbook hssfWorkbook = WorkbookFactory.create(inputStream);
        List<Schema.SheetContent> schemas;
        try {
            if (hssfWorkbook == null) {
                throw new IOException("could not open " + request.getMetadata().getId() + " as an excel file");
            }
            int sheetNumber = hssfWorkbook.getNumberOfSheets();
            if (sheetNumber < 1) {
                LOGGER.debug(marker, "has not sheet to read");
                return Collections.emptyList();
            }
            schemas = new ArrayList<>();
            for (int i = 0; i < sheetNumber; i++) {
                Sheet sheet = hssfWorkbook.getSheetAt(i);
                if (sheet.getLastRowNum() < 1) {
                    LOGGER.debug(marker, "sheet '{}' do not have rows skip ip", sheet.getSheetName());
                    continue;
                }
                List<ColumnMetadata> columnsMetadata = parsePerSheet(// 
                sheet, // 
                request.getMetadata().getId(), hssfWorkbook.getCreationHelper().createFormulaEvaluator());
                String sheetName = sheet.getSheetName();
                // update XlsSerializer if this default sheet naming change!!!
                schemas.add(new Schema.SheetContent(sheetName == null ? "sheet-" + i : sheetName, columnsMetadata));
            }
        } finally {
            hssfWorkbook.close();
        }
        return schemas;
    } catch (Exception e) {
        LOGGER.debug(marker, "Exception during parsing xls request :" + e.getMessage(), e);
        throw new TDPException(CommonErrorCodes.UNEXPECTED_EXCEPTION, e);
    }
}
Also used : ColumnMetadata(org.talend.dataprep.api.dataset.ColumnMetadata) PushbackInputStream(java.io.PushbackInputStream) InputStream(java.io.InputStream) Schema(org.talend.dataprep.schema.Schema) Marker(org.slf4j.Marker) IOException(java.io.IOException) TDPException(org.talend.dataprep.exception.TDPException) IOException(java.io.IOException) TDPException(org.talend.dataprep.exception.TDPException) PushbackInputStream(java.io.PushbackInputStream) StreamingSheet(org.talend.dataprep.schema.xls.streaming.StreamingSheet)

Example 92 with TDPException

use of org.talend.dataprep.exception.TDPException in project data-prep by Talend.

the class XlsSerializer method serialize.

/**
 * @see Serializer#serialize(InputStream, DataSetMetadata, long)
 */
@Override
public InputStream serialize(InputStream givenInputStream, DataSetMetadata metadata, long limit) {
    try {
        PipedInputStream pipe = new PipedInputStream();
        PipedOutputStream jsonOutput = new PipedOutputStream(pipe);
        // override the parameter in case it needs to be wrapped in a buffered inputstream
        InputStream inputStream = givenInputStream;
        if (!inputStream.markSupported()) {
            inputStream = new BufferedInputStream(inputStream);
        }
        inputStream.mark(Integer.MAX_VALUE);
        boolean newExcelFormat = XlsUtils.isNewExcelFormat(inputStream);
        inputStream.reset();
        Runnable runnable = // 
        newExcelFormat ? serializeNew(inputStream, metadata, limit, jsonOutput) : serializeOld(inputStream, metadata, limit, jsonOutput);
        // Serialize asynchronously for better performance (especially if caller doesn't consume all, see sampling).
        executor.execute(runnable);
        return pipe;
    } catch (IOException e) {
        throw new TDPException(CommonErrorCodes.UNABLE_TO_SERIALIZE_TO_JSON, e);
    }
}
Also used : TDPException(org.talend.dataprep.exception.TDPException) XlsRunnable(org.talend.dataprep.schema.xls.serialization.XlsRunnable) XlsxStreamRunnable(org.talend.dataprep.schema.xls.serialization.XlsxStreamRunnable)

Example 93 with TDPException

use of org.talend.dataprep.exception.TDPException in project data-prep by Talend.

the class SortAndOrderHelper method getDataSetMetadataComparator.

/**
 * Return a dataset metadata comparator from the given parameters.
 *
 * @param sortKey the sort key. If null, default to {@link Sort#NAME}.
 * @param orderKey the order key to use. If null, default to {@link Order#ASC}.
 * @return a dataset metadata comparator from the given parameters.
 */
public static Comparator<DataSetMetadata> getDataSetMetadataComparator(Sort sortKey, Order orderKey) {
    Comparator<Comparable> comparisonOrder = getOrderComparator(orderKey);
    // Select comparator for sort (either by name or date)
    Function<DataSetMetadata, Comparable> keyExtractor;
    if (sortKey == null) {
        // default to NAME sort
        keyExtractor = dataSetMetadata -> dataSetMetadata.getName().toUpperCase();
    } else {
        switch(sortKey) {
            // In case of API call error, default to NAME sort
            case DATASET_NAME:
            case NB_STEPS:
            case NAME:
                keyExtractor = dataSetMetadata -> dataSetMetadata.getName().toUpperCase();
                break;
            case AUTHOR:
                keyExtractor = dataSetMetadata -> {
                    // in order to just call a method to retrieve the author name
                    if (dataSetMetadata instanceof UserDataSetMetadata) {
                        Owner owner = ((UserDataSetMetadata) dataSetMetadata).getOwner();
                        return (owner != null) ? StringUtils.upperCase(owner.getDisplayName()) : StringUtils.EMPTY;
                    }
                    return dataSetMetadata.getAuthor();
                };
                break;
            case CREATION_DATE:
            case DATE:
                keyExtractor = DataSetMetadata::getCreationDate;
                break;
            case LAST_MODIFICATION_DATE:
                keyExtractor = DataSetMetadata::getLastModificationDate;
                break;
            case NB_RECORDS:
                keyExtractor = metadata -> metadata.getContent().getNbRecords();
                break;
            default:
                // this should not be possible
                throw new TDPException(ILLEGAL_SORT_FOR_LIST, build().put("sort", sortKey));
        }
    }
    return Comparator.comparing(keyExtractor, comparisonOrder);
}
Also used : TDPException(org.talend.dataprep.exception.TDPException) UserDataSetMetadata(org.talend.dataprep.dataset.service.UserDataSetMetadata) Owner(org.talend.dataprep.api.share.Owner) DataSetMetadata(org.talend.dataprep.api.dataset.DataSetMetadata) UserDataSetMetadata(org.talend.dataprep.dataset.service.UserDataSetMetadata)

Example 94 with TDPException

use of org.talend.dataprep.exception.TDPException in project data-prep by Talend.

the class ExportAPI method export.

@RequestMapping(value = "/api/export", method = GET)
@ApiOperation(value = "Export a dataset", consumes = APPLICATION_FORM_URLENCODED_VALUE, notes = "Export a dataset or a preparation to file. The file type is provided in the request body.")
public ResponseEntity<StreamingResponseBody> export(@ApiParam(value = "Export configuration") @Valid final ExportParameters parameters) {
    try {
        Map<String, String> arguments = new HashMap<>();
        final Enumeration<String> names = HttpRequestContext.parameters();
        while (names.hasMoreElements()) {
            final String paramName = names.nextElement();
            if (StringUtils.contains(paramName, ExportFormat.PREFIX)) {
                final String paramValue = HttpRequestContext.parameter(paramName);
                arguments.put(paramName, StringUtils.isNotEmpty(paramValue) ? paramValue : StringUtils.EMPTY);
            }
        }
        parameters.getArguments().putAll(arguments);
        final String exportName = getExportNameAndConsolidateParameters(parameters);
        parameters.setExportName(exportName);
        LOG.info("New Export {}", parameters);
        final GenericCommand<InputStream> command = getCommand(Export.class, parameters);
        return CommandHelper.toStreaming(command);
    } catch (TDPException e) {
        throw e;
    } catch (Exception e) {
        throw new TDPException(APIErrorCodes.UNABLE_TO_EXPORT_CONTENT, e);
    }
}
Also used : TDPException(org.talend.dataprep.exception.TDPException) HashMap(java.util.HashMap) InputStream(java.io.InputStream) TDPException(org.talend.dataprep.exception.TDPException) IOException(java.io.IOException) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 95 with TDPException

use of org.talend.dataprep.exception.TDPException in project data-prep by Talend.

the class FolderAPI method renameFolder.

@RequestMapping(value = "/api/folders/{id}/name", method = PUT)
@ApiOperation(value = "Rename a Folder")
@Timed
public void renameFolder(@PathVariable final String id, @RequestBody final String newName) {
    if (StringUtils.isEmpty(id) || StringUtils.isEmpty(newName)) {
        throw new TDPException(APIErrorCodes.UNABLE_TO_RENAME_FOLDER);
    }
    try {
        final HystrixCommand<Void> renameFolder = getCommand(RenameFolder.class, id, newName);
        renameFolder.execute();
    } catch (Exception e) {
        throw new TDPException(APIErrorCodes.UNABLE_TO_RENAME_FOLDER, e);
    }
}
Also used : TDPException(org.talend.dataprep.exception.TDPException) TDPException(org.talend.dataprep.exception.TDPException) IOException(java.io.IOException) EOFException(java.io.EOFException) Timed(org.talend.dataprep.metrics.Timed) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

TDPException (org.talend.dataprep.exception.TDPException)123 IOException (java.io.IOException)43 InputStream (java.io.InputStream)25 DataSetMetadata (org.talend.dataprep.api.dataset.DataSetMetadata)21 Test (org.junit.Test)17 ApiOperation (io.swagger.annotations.ApiOperation)16 Timed (org.talend.dataprep.metrics.Timed)14 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)13 DataSet (org.talend.dataprep.api.dataset.DataSet)13 ServiceBaseTest (org.talend.ServiceBaseTest)11 StringEntity (org.apache.http.entity.StringEntity)10 JsonParser (com.fasterxml.jackson.core.JsonParser)9 URISyntaxException (java.net.URISyntaxException)9 HttpPost (org.apache.http.client.methods.HttpPost)9 Autowired (org.springframework.beans.factory.annotation.Autowired)9 ColumnMetadata (org.talend.dataprep.api.dataset.ColumnMetadata)9 List (java.util.List)8 URIBuilder (org.apache.http.client.utils.URIBuilder)8 Marker (org.slf4j.Marker)8 ErrorCode (org.talend.daikon.exception.error.ErrorCode)8