/*
 * The Gemma project
 * 
 * Copyright (c) 2006 University of British Columbia
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package ubic.gemma.loader.util.fetcher;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import ubic.gemma.model.common.description.LocalFile;

/**
 * @author pavlidis
 * @version $Id$
 */
public abstract class AbstractFetcher implements Fetcher {

    protected static Log log = LogFactory.getLog( AbstractFetcher.class.getName() );
    protected String localBasePath = null;
    protected String baseDir = null;
    protected boolean success = false;
    protected boolean force = false;

    /**
     * Set to true if downloads should proceed even if the file already exists.
     * 
     * @param force
     */
    public void setForce( boolean force ) {
        this.force = force;
    }

    /**
     * Create a directory according to the current accession number and set path information. If the path cannot be
     * used, we use a temporary directory.
     * 
     * @param accession
     * @return new directory
     * @throws IOException
     */
    protected File mkdir( String accession ) throws IOException {
        File newDir = null;
        File targetPath = null;

        if ( localBasePath != null ) {
            targetPath = new File( localBasePath );
            newDir = new File( targetPath + File.separator + accession );
        }

        if ( localBasePath == null || !targetPath.canRead() ) {
            File tmpDir = new File( System.getProperty( "java.io.tmpdir" ) + File.separator + accession );
            log.warn( "Could not create output directory " + newDir );
            log.warn( "Will use local temporary directory: " + tmpDir.getAbsolutePath() );

            newDir = tmpDir;
        }

        if ( newDir == null ) {
            throw new IOException( "Could not create target directory, was null" );
        }
        if ( !newDir.exists() && !newDir.mkdir() ) {
            throw new IOException( "Could not create target directory " + newDir.getAbsolutePath() );
        }
        if ( !newDir.canWrite() ) {
            throw new IOException( "Cannot write to target directory " + newDir.getAbsolutePath() );
        }

        return newDir;
    }

    /**
     * @param seekFile
     * @return
     */
    protected LocalFile fetchedFile( String seekFile ) {
        return this.fetchedFile( seekFile, seekFile );
    }

    /**
     * @param seekFile
     * @param outputFileName
     * @return
     */
    protected LocalFile fetchedFile( String seekFile, String outputFileName ) {
        LocalFile file = LocalFile.Factory.newInstance();
        file.setVersion( new SimpleDateFormat().format( new Date() ) );
        try {
            file.setRemoteURL( ( new File( seekFile ) ).toURI().toURL() );
            file.setLocalURL( ( new URI( "file://" + outputFileName ) ).toURL() );
        } catch ( MalformedURLException e ) {
            throw new RuntimeException( e );
        } catch ( URISyntaxException e ) {
            throw new RuntimeException( e );
        }
        return file;
    }

    /**
     * @return Returns the localBasePath.
     */
    public String getLocalBasePath() {
        return this.localBasePath;
    }

}

@@ -3194,37 +3194,120 @@
 File
-%0A     * @param outputFileName
+Path Absolute path to the file for download%0A     * @param outputFilePath Absolute path to the download location.
 %0A   
@@ -3378,16 +3378,20 @@
 seekFile
+Path
 , String
@@ -3401,20 +3401,20 @@
 tputFile
-Name
+Path
  ) %7B%0A   
@@ -3605,16 +3605,20 @@
 seekFile
+Path
  ) ).toU
@@ -3673,42 +3673,37 @@
 new 
-URI( %22file://%22 + outputFileName ) 
+File( outputFilePath ).toURI(
 ).to
@@ -3699,32 +3699,34 @@
 .toURI().toURL()
+ )
  );%0A        %7D ca
@@ -3808,96 +3808,8 @@
  );%0A
-        %7D catch ( URISyntaxException e ) %7B%0A            throw new RuntimeException( e );%0A
     

fix url format bug