/*
 * The Exomiser - A tool to annotate and prioritize genomic variants
 *
 * Copyright (c) 2016-2017 Queen Mary University of London.
 * Copyright (c) 2012-2016 Charité Universitätsmedizin Berlin and Genome Research Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.monarchinitiative.exomiser.data.phenotype.io;

import org.apache.commons.io.FileUtils;
import org.monarchinitiative.exomiser.data.phenotype.resources.ResourceOperationStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 *
 * @author Jules Jacobsen <jules.jacobsen@sanger.ac.uk>
 */
public class FileDownloadUtils {

    private static final Logger logger = LoggerFactory.getLogger(FileDownloadUtils.class);

    private FileDownloadUtils() {
        //this class should not be instantiated
    }

    /**
     * Fetches the file from the source URL and writes it out to the destination
     * file. 
     * @param source
     * @param destination
     * @return
     */
    public static ResourceOperationStatus fetchFile(URL source, File destination) {
        ResourceOperationStatus status = ResourceOperationStatus.FAILURE;

        try {
            logger.info("Creating new file: {}", destination.getAbsolutePath());
            logger.info("Transferring data from: {}", source);
            destination.createNewFile();
            destination.setWritable(true);
//            logger.info("Protocol: {}", source.getProtocol());
//            SocketAddress socketAddress = new InetSocketAddress("wwwcache.sanger.ac.uk", 3128);
//            Proxy proxy = new Proxy(Proxy.Type.HTTP, socketAddress);
            FileUtils.copyURLToFile(source, destination, 2500, 15000);
        } catch (IOException ex) {
            logger.error("Unable to copy file from external resource due to error: ", ex);
            return ResourceOperationStatus.FAILURE;
        }

        //always the optimist
        if (destination.length() == 0 ) {
            logger.info("{} is empty - deleting file.", destination.getAbsolutePath());
            destination.delete();
        } else {
            status = ResourceOperationStatus.SUCCESS;
        }

        return status;
    }
}

@@ -1390,16 +1390,44 @@
 net.URL;
+%0Aimport java.nio.file.Files;
 %0A%0A/**%0A *
@@ -1962,82 +1962,8 @@
 n) %7B
-%0A        ResourceOperationStatus status = ResourceOperationStatus.FAILURE;
 %0A%0A  
@@ -2071,112 +2071,224 @@
     
-logger.info(%22Transferring data from: %7B%7D%22, source);%0A            destination.createNewFile();%0A            
+if (!destination.createNewFile()) %7B%0A                logger.error(%22Unable to create new file %7B%7D%22, destination.getAbsolutePath());%0A                return ResourceOperationStatus.FAILURE;%0A            %7D%0A            if (!
 dest
@@ -2312,20 +2312,24 @@
 le(true)
-;%0A//
+) %7B%0A    
         
@@ -2343,54 +2343,95 @@
 ger.
-info(%22Protocol: %7B%7D%22, source.getProtocol
+error(%22Unable to set file %7B%7D permissions as writable%22, destination.getAbsolutePath
 ());%0A
-//
     
@@ -2442,160 +2442,126 @@
     
-SocketAddress socketAddress = new InetSocketAddress(%22wwwcache.sanger.ac.uk%22, 3128);%0A//            Proxy proxy = new Proxy(Proxy.Type.HTTP, socketAddress
+    return ResourceOperationStatus.FAILURE;%0A            %7D%0A            logger.info(%22Transferring data from: %7B%7D%22, source
 );%0A 
@@ -2823,38 +2823,8 @@
  %7D%0A%0A
-        //always the optimist%0A
     
@@ -2965,38 +2965,77 @@
     
-destination.delete();%0A
+try %7B%0A                Files.delete(destination.toPath());%0A   
         
 %7D el
@@ -3034,37 +3034,152 @@
     
+ 
 %7D 
-else %7B%0A            status =
+catch (IOException e) %7B%0A                logger.error(%22Unable to delete empty file %7B%7D%22, destination.getAbsolutePath());%0A                return
  Res
@@ -3203,17 +3203,21 @@
 tus.
-SUCCESS;%0A
+FAILURE;%0A    
     
@@ -3214,32 +3214,41 @@
 ;%0A            %7D%0A
+        %7D
 %0A        return 
@@ -3251,14 +3251,39 @@
 urn 
-status
+ResourceOperationStatus.SUCCESS
 ;%0A  

Clean up FileDownloadUtils