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);
}
}
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);
}
}
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);
}
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);
}
}
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);
}
}
Aggregations