use of org.talend.dataprep.exception.TDPException in project data-prep by Talend.
the class XlsSchemaParser method parse.
/**
* @see SchemaParser#parse(Request)
*/
@Override
public Schema parse(Request request) {
final Marker marker = Markers.dataset(request.getMetadata().getId());
LOGGER.debug(marker, "parsing {} ");
try {
List<Schema.SheetContent> sheetContents = parseAllSheets(request);
Schema result;
if (!sheetContents.isEmpty()) {
// only one sheet
if (sheetContents.size() == 1) {
result = //
Schema.Builder.parserResult().sheetContents(//
sheetContents).draft(//
false).sheetName(//
sheetContents.get(0).getName()).build();
} else {
// multiple sheet, set draft flag on
result = //
Schema.Builder.parserResult().sheetContents(//
sheetContents).draft(//
true).sheetName(//
sheetContents.get(0).getName()).build();
}
} else // nothing to parse
{
throw new TDPException(DataSetErrorCodes.UNABLE_TO_READ_DATASET_CONTENT);
}
return result;
} catch (TDPException e) {
throw e;
} catch (Exception e) {
LOGGER.debug(marker, "IOException 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 FolderRepository method getHierarchy.
/**
* Get the folder hierarchy (list of its parents)
*
* @param folder the folder.
* @return A {@link Iterable} of {@link Folder} containing all its parents starting from home.
*/
default List<Folder> getHierarchy(final Folder folder) {
final List<Folder> hierarchy = new ArrayList<>();
String nextParentId = folder.getParentId();
while (nextParentId != null) {
try {
final Folder parent = this.getFolderById(nextParentId);
hierarchy.add(0, parent);
nextParentId = parent.getParentId();
} catch (final TDPException e) {
// as the user's home child
if (e.getCode().getHttpStatus() == 403) {
hierarchy.add(0, this.getHome());
break;
}
throw e;
}
}
return hierarchy;
}
use of org.talend.dataprep.exception.TDPException in project data-prep by Talend.
the class FileSystemFolderRepository method entries.
@Override
public Stream<FolderEntry> entries(String folderId, FolderContentType contentType) {
FolderPath folderPath = fromId(folderId);
if (folderPath == null) {
throw new TDPException(FOLDER_DOES_NOT_EXIST, build().put("id", folderId));
}
final Path path = pathsConverter.toPath(folderPath);
if (Files.notExists(path)) {
return Stream.empty();
}
try {
return //
Files.list(path).filter(//
pathFound -> !Files.isDirectory(pathFound)).map(//
FileSystemUtils::toFolderEntry).filter(entry -> Objects.equals(contentType, entry.getContentType()));
} catch (IOException e) {
throw new TDPException(UNABLE_TO_LIST_FOLDER_ENTRIES, e, build().put("path", path).put("type", contentType));
}
}
use of org.talend.dataprep.exception.TDPException in project data-prep by Talend.
the class FileSystemFolderRepository method renameFolder.
@Override
public Folder renameFolder(String folderId, String newName) {
final Folder folder = getFolderById(folderId);
if (folder == null) {
throw new IllegalArgumentException("Cannot rename a folder that cannot be found");
}
FolderPath folderToMovePath = FolderPath.deserializeFromString(folder.getPath());
if (folderToMovePath.isRoot()) {
throw new IllegalArgumentException("Cannot rename home folder");
}
if (newName.contains(PATH_SEPARATOR.toString())) {
throw new TDPException(ILLEGAL_FOLDER_NAME, build().put("name", newName));
}
FolderPath targetFolderPath = new FolderPath(folderToMovePath.getParent(), newName);
Path folderPath = pathsConverter.toPath(folderToMovePath);
Path newFolderPath = pathsConverter.toPath(targetFolderPath);
try {
FileUtils.moveDirectory(folderPath.toFile(), newFolderPath.toFile());
} catch (IOException e) {
throw new TDPException(UNABLE_TO_RENAME_FOLDER, e, build().put("path", folder.getPath()));
}
return getFolderById(toId(pathsConverter.toFolderPath(newFolderPath)));
}
use of org.talend.dataprep.exception.TDPException in project data-prep by Talend.
the class FileSystemFolderRepository method addFolderEntry.
@Override
public FolderEntry addFolderEntry(FolderEntry folderEntry, String folderId) {
FolderPath folderPath = fromId(folderId);
if (folderPath == null) {
throw new TDPException(FOLDER_DOES_NOT_EXIST, build().put("id", folderId));
}
// we store the FolderEntry bean content as properties the file name is the name
try {
String fileName = buildFileName(folderEntry);
Path entryFilePath = pathsConverter.toPath(folderPath).resolve(fileName);
// we delete it if exists
Files.deleteIfExists(entryFilePath);
Path parentPath = entryFilePath.getParent();
// check parent path first
if (Files.notExists(parentPath)) {
Files.createDirectories(parentPath);
}
entryFilePath = Files.createFile(entryFilePath);
folderEntry.setFolderId(folderId);
try (OutputStream outputStream = Files.newOutputStream(entryFilePath)) {
writeEntryToStream(folderEntry, outputStream);
}
return folderEntry;
} catch (IOException e) {
throw new TDPException(UNABLE_TO_ADD_FOLDER_ENTRY, e, build().put("path", folderPath));
}
}
Aggregations