001/*
002 * Copyright (c) 2014 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2011-04-12 13:56:18 -0700 (Tue, 12 Apr 2011) $' 
007 * '$Revision: 27498 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029package org.kepler.cloudsharing.fs;
030
031import java.io.File;
032import java.io.IOException;
033import java.net.MalformedURLException;
034import java.net.URL;
035
036import org.apache.commons.io.FileUtils;
037import org.kepler.cloudsharing.util.WorkflowResults;
038import org.seedme.client.FileEntry;
039
040import ptolemy.util.MessageHandler;
041
042/** A client to upload workflow results to Dropbox.
043 * 
044 *  @author Daniel Crawl
045 *  @version $Id$
046 */
047public class FileSystemClient {
048
049    /** Set the results. */
050    public void setResults(WorkflowResults results) {
051        _results = results;
052    }
053
054    /** Upload the results to Dropbox. */
055    public void update() {
056        
057        File topDir = new File(_dropboxHomeDirName, _dropboxKeplerDirName);
058        if(!topDir.exists() && !topDir.mkdirs()) {
059            MessageHandler.error("Could not create directories for " + topDir);
060            return;
061        }
062        
063        // set the id to be the title of the results.
064        // TODO better id?
065        // TODO check if missing title
066        _id = _results.get("title").trim().replaceAll("\\s+", "_");
067        File resultsDir = new File(topDir, _id);
068        
069        if(resultsDir.exists()) {
070            // TODO
071        } else if(!resultsDir.mkdir()) {
072            MessageHandler.error("Could not create directory for results: " + resultsDir);
073            return;
074        }
075        
076        // TODO metadata
077        
078        // TODO sharing
079        
080        // copy the files to the dropbox results directory.
081        for(FileEntry entry : _results.files()) {
082            try {
083                FileUtils.copyFileToDirectory(entry.file, resultsDir);
084            } catch (IOException e) {
085                MessageHandler.error("Could not copy " + entry.file, e);
086            }
087        }
088        
089    }
090
091    /** Get the URL for the uploaded results. */
092    public URL getURL() {
093        try {
094            return new URL(DROPBOX_HOME_URL + _dropboxKeplerDirName + _id);
095        } catch (MalformedURLException e) {
096            // TODO Auto-generated catch block
097            e.printStackTrace();
098            return null;
099        }
100    }
101
102    /** The id of the results when added to Dropbox. */
103    private String _id;
104    
105    /** The results. */
106    private WorkflowResults _results;
107    
108    /** The web site prefix for files in Dropbox. */
109    private final static String DROPBOX_HOME_URL = "https://www.dropbox.com/home/";
110    
111    /** The workflow results directory in Dropbox in the file system. */
112    private String _dropboxKeplerDirName = "KeplerData" + File.separator +
113            "WorkflowResults" + File.separator;
114
115    /** The Dropbox directory location in the file system. */
116    private String _dropboxHomeDirName = System.getProperty("user.home") + File.separator +
117            "Dropbox" + File.separator;
118    
119}