Search in sources :

Example 21 with TDPException

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

the class FileSystemFolderRepository method moveFolderEntry.

@Override
public void moveFolderEntry(FolderEntry folderEntry, String fromId, String toId) {
    Path destinationPath = pathsConverter.toPath(fromId(toId));
    if (Files.notExists(destinationPath)) {
        throw new IllegalArgumentException("destinationPath doesn't exists");
    }
    Path originFilePath = Paths.get(pathsConverter.toPath(fromId(fromId)).toString(), buildFileName(folderEntry));
    if (Files.notExists(originFilePath)) {
        throw new IllegalArgumentException("entry doesn't exists");
    }
    Path destinationFile = Paths.get(destinationPath.toString(), buildFileName(folderEntry));
    try {
        Files.move(originFilePath, destinationFile);
    } catch (IOException e) {
        throw new TDPException(DataSetErrorCodes.UNABLE_TO_MOVE_FOLDER_ENTRY, e);
    }
}
Also used : Path(java.nio.file.Path) TDPException(org.talend.dataprep.exception.TDPException) IOException(java.io.IOException)

Example 22 with TDPException

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

the class FileSystemFolderRepository method addFolder.

@Override
public Folder addFolder(String parentId, String givenPath) {
    // parent path must be set and exists
    FolderPath parentFolderPath = fromId(parentId);
    List<String> pathToAppend = Arrays.stream(givenPath.split(PATH_SEPARATOR.toString())).filter(StringUtils::isNotBlank).collect(Collectors.toList());
    if (parentFolderPath == null || !exists(parentId)) {
        throw new TDPException(UNABLE_TO_ADD_FOLDER, build().put("path", givenPath));
    }
    FolderPath folderPathToCreate = new FolderPath(parentFolderPath, pathToAppend.toArray(new String[pathToAppend.size()]));
    try {
        Path pathToCreate = pathsConverter.toPath(folderPathToCreate);
        Files.createDirectories(pathToCreate);
        return toFolder(pathToCreate, security.getUserId());
    } catch (IOException e) {
        throw new TDPException(UNABLE_TO_ADD_FOLDER, e, build().put("path", givenPath));
    }
}
Also used : TDPException(org.talend.dataprep.exception.TDPException) Path(java.nio.file.Path) IOException(java.io.IOException)

Example 23 with TDPException

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

the class FileSystemFolderRepository method clear.

@Override
public void clear() {
    try {
        FileUtils.deleteDirectory(pathsConverter.getRootFolder().toFile());
        init();
    } catch (IOException e) {
        throw new TDPException(CommonErrorCodes.UNEXPECTED_EXCEPTION, e);
    }
}
Also used : TDPException(org.talend.dataprep.exception.TDPException) IOException(java.io.IOException)

Example 24 with TDPException

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

the class FileSystemUtils method toFolderEntry.

/**
 * Tries to read a file for a dataprep {@link FolderEntry}.
 *
 * @param pathFile the location of the file
 * @return the folder entry or throws an exception if it cannot be read
 */
static FolderEntry toFolderEntry(Path pathFile) {
    FolderEntry folderEntry;
    try (InputStream inputStream = Files.newInputStream(pathFile)) {
        Properties properties = new Properties();
        properties.load(inputStream);
        folderEntry = new FolderEntry();
        folderEntry.setFolderId(properties.getProperty(FOLDER_ID));
        folderEntry.setContentType(FolderContentType.fromName(properties.getProperty(CONTENT_TYPE)));
        folderEntry.setContentId(properties.getProperty(CONTENT_ID));
    } catch (IOException e) {
        throw new TDPException(UNABLE_TO_READ_FOLDER_ENTRY, e, build().put("path", pathFile));
    }
    return folderEntry;
}
Also used : TDPException(org.talend.dataprep.exception.TDPException) InputStream(java.io.InputStream) FolderEntry(org.talend.dataprep.api.folder.FolderEntry) IOException(java.io.IOException) Properties(java.util.Properties)

Example 25 with TDPException

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

the class CSVFormatUtils method compileParameterProperties.

/**
 * Retrieve properties associated with a dataset content and put them in a
 * map with their corresponding key.
 *
 * @param separator the specified separator
 * @param updatedParameters the updated header
 */
Map<String, String> compileParameterProperties(Separator separator, Map<String, String> updatedParameters) {
    List<String> header = separator.getHeaders().stream().map(p -> p.getKey()).collect(Collectors.toList());
    // header
    String jsonHeader;
    try {
        jsonHeader = mapper.writeValueAsString(header);
    } catch (Exception e) {
        throw new TDPException(UNABLE_TO_SERIALIZE_TO_JSON, e);
    }
    updatedParameters.put(HEADER_COLUMNS_PARAMETER, jsonHeader);
    // separator
    updatedParameters.put(SEPARATOR_PARAMETER, String.valueOf(separator.getSeparator()));
    // if no parameter set set take the default one
    updatedParameters.putIfAbsent(TEXT_ENCLOSURE_CHAR, defaultTextEnclosure);
    updatedParameters.putIfAbsent(ESCAPE_CHAR, defaultEscapeChar);
    return updatedParameters;
}
Also used : Value(org.springframework.beans.factory.annotation.Value) List(java.util.List) Component(org.springframework.stereotype.Component) TDPException(org.talend.dataprep.exception.TDPException) UNABLE_TO_SERIALIZE_TO_JSON(org.talend.dataprep.exception.error.CommonErrorCodes.UNABLE_TO_SERIALIZE_TO_JSON) Map(java.util.Map) CSVFormatFamily(org.talend.dataprep.schema.csv.CSVFormatFamily) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Autowired(org.springframework.beans.factory.annotation.Autowired) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Collections(java.util.Collections) Collectors(java.util.stream.Collectors) TDPException(org.talend.dataprep.exception.TDPException) TDPException(org.talend.dataprep.exception.TDPException)

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