package com.novoda.downloadmanager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

final class DownloadBatchFactory {

    private static final boolean NOTIFICATION_NOT_SEEN = false;

    private DownloadBatchFactory() {
        // non instantiable factory class
    }

    static DownloadBatch newInstance(Batch batch,
                                     FileOperations fileOperations,
                                     DownloadsBatchPersistence downloadsBatchPersistence,
                                     DownloadsFilePersistence downloadsFilePersistence,
                                     CallbackThrottle callbackThrottle,
                                     ConnectionChecker connectionChecker) {
        DownloadBatchTitle downloadBatchTitle = DownloadBatchTitleCreator.createFrom(batch);
        DownloadBatchId downloadBatchId = batch.downloadBatchId();
        long downloadedDateTimeInMillis = System.currentTimeMillis();

        List<DownloadFile> downloadFiles = new ArrayList<>(batch.batchFiles().size());

        for (BatchFile batchFile : batch.batchFiles()) {
            String networkAddress = batchFile.networkAddress();

            InternalFileSize fileSize = InternalFileSizeCreator.unknownFileSize();

            FilePersistenceCreator filePersistenceCreator = fileOperations.filePersistenceCreator();
            FilePersistence filePersistence = filePersistenceCreator.create();

            String basePath = filePersistence.basePath().path();
            FilePath filePath = FilePathCreator.create(basePath, relativePathFrom(batchFile));
            FileName fileName = FileNameExtractor.extractFrom(filePath.path());

            DownloadFileId downloadFileId = downloadFileIdFrom(batch, batchFile);
            InternalDownloadFileStatus downloadFileStatus = new LiteDownloadFileStatus(
                    downloadBatchId,
                    downloadFileId,
                    InternalDownloadFileStatus.Status.QUEUED,
                    fileSize,
                    filePath
            );

            FileDownloader fileDownloader = fileOperations.fileDownloader();
            FileSizeRequester fileSizeRequester = fileOperations.fileSizeRequester();

            DownloadFile downloadFile = new DownloadFile(
                    downloadBatchId,
                    downloadFileId,
                    networkAddress,
                    downloadFileStatus,
                    fileName,
                    filePath,
                    fileSize,
                    fileDownloader,
                    fileSizeRequester,
                    filePersistence,
                    downloadsFilePersistence
            );
            downloadFiles.add(downloadFile);
        }

        InternalDownloadBatchStatus liteDownloadBatchStatus = new LiteDownloadBatchStatus(
                downloadBatchId,
                downloadBatchTitle,
                downloadedDateTimeInMillis,
                DownloadBatchStatus.Status.QUEUED,
                NOTIFICATION_NOT_SEEN
        );

        return new DownloadBatch(
                liteDownloadBatchStatus,
                downloadFiles,
                new HashMap<>(),
                downloadsBatchPersistence,
                callbackThrottle,
                connectionChecker
        );
    }

    private static String relativePathFrom(BatchFile batchFile) {
        String fileNameFromNetworkAddress = FileNameExtractor.extractFrom(batchFile.networkAddress()).name();
        return batchFile.relativePath().or(fileNameFromNetworkAddress);
    }

    private static DownloadFileId downloadFileIdFrom(Batch batch, BatchFile batchFile) {
        String rawId = batch.downloadBatchId().rawId() + batchFile.networkAddress();
        return batchFile.downloadFileId().or(DownloadFileIdCreator.createFrom(rawId));
    }

}

@@ -30,16 +30,37 @@
 nager;%0A%0A
+import java.io.File;%0A
 import j
@@ -79,16 +79,16 @@
 ayList;%0A
-
 import j
@@ -1596,16 +1596,33 @@
 sePath, 
+prependBatchIdTo(
 relative
@@ -1640,16 +1640,34 @@
 tchFile)
+, downloadBatchId)
 );%0A     
@@ -3366,16 +3366,16 @@
 Checker%0A
-
         
@@ -3376,32 +3376,206 @@
       );%0A    %7D%0A%0A
+    private static String prependBatchIdTo(String filePath, DownloadBatchId downloadBatchId) %7B%0A        return downloadBatchId.rawId() + File.separatorChar + filePath;%0A    %7D%0A%0A
     private stat

Prepend batch id to relative file path