class LogStorageInfos {

    public static S3LogLocation buildLogLocation(Task task, S3LogLocation defaultLogLocation) {
        S3Bucket customS3Bucket = findCustomS3Bucket(task).orElse(null);
        String customPathPrefix = findCustomPathPrefix(task).orElse(null);

        if (customS3Bucket == null && customPathPrefix == null) {
            return defaultLogLocation;
        }

        String fullPrefix = customPathPrefix == null
                ? defaultLogLocation.getKey()
                : buildPathPrefix(customPathPrefix, defaultLogLocation.getKey());


        if (customS3Bucket == null) {
            return new S3LogLocation(
                    defaultLogLocation.getAccountName(),
                    defaultLogLocation.getAccountId(),
                    defaultLogLocation.getRegion(),
                    defaultLogLocation.getBucket(),
                    fullPrefix
            );
        }

        return customS3Bucket.getS3Account()
                .map(account ->
                        new S3LogLocation(
                                account.getAccountId(),
                                account.getAccountId(),
                                account.getRegion(),
                                customS3Bucket.getBucketName(),
                                fullPrefix
                        ))
                .orElseGet(() ->
                        new S3LogLocation(
                                defaultLogLocation.getAccountName(),
                                defaultLogLocation.getAccountId(),
                                defaultLogLocation.getRegion(),
                                customS3Bucket.getBucketName(),
                                fullPrefix
                        ));
    }

}