Search in sources :

Example 16 with TDPException

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);
    }
}
Also used : TDPException(org.talend.dataprep.exception.TDPException) Schema(org.talend.dataprep.schema.Schema) Marker(org.slf4j.Marker) TDPException(org.talend.dataprep.exception.TDPException) IOException(java.io.IOException)

Example 17 with TDPException

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;
}
Also used : TDPException(org.talend.dataprep.exception.TDPException) ArrayList(java.util.ArrayList) Folder(org.talend.dataprep.api.folder.Folder)

Example 18 with TDPException

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));
    }
}
Also used : TDPException(org.talend.dataprep.exception.TDPException) Path(java.nio.file.Path) IOException(java.io.IOException)

Example 19 with TDPException

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)));
}
Also used : TDPException(org.talend.dataprep.exception.TDPException) Path(java.nio.file.Path) IOException(java.io.IOException) Folder(org.talend.dataprep.api.folder.Folder)

Example 20 with TDPException

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));
    }
}
Also used : TDPException(org.talend.dataprep.exception.TDPException) Path(java.nio.file.Path) OutputStream(java.io.OutputStream) IOException(java.io.IOException)

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