001/*
002 * Copyright (c) 2014 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2013-09-18 16:45:59 -0700 (Wed, 18 Sep 2013) $' 
007 * '$Revision: 32484 $'
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.util;
030
031import java.io.File;
032import java.util.HashMap;
033import java.util.HashSet;
034import java.util.LinkedList;
035import java.util.List;
036import java.util.Map;
037import java.util.Set;
038
039import org.seedme.client.FileEntry;
040
041/** A collection of results from a workflow execution.
042 * 
043 *  @author Daniel Crawl
044 *  @id $Id$
045 */
046public class WorkflowResults {
047             
048    /** Get the value for a key. */
049    public String get(String key) {
050        return _keyValues.get(key);
051    }
052    
053    /** Get the set of key values. */
054    public Map<String, String> keyValues() {
055        return new HashMap<String,String>(_keyValues);
056    }
057    
058    /** Get who these results are shared with. */
059    public String getShareWith() {
060        return _shareWith;
061    }
062    
063    /** Get the Privacy for these results. */
064    public Privacy getPrivacy() {
065        return _privacy;
066    }
067    
068    /** Get the set of files. */
069    public Set<FileEntry> files() {
070        return new HashSet<FileEntry>(_files);
071    }
072    
073    /** Set a key value for the collection. */
074    public void setKeyValue(String key, String value) {
075        _keyValues.put(key, value);
076    }
077    
078    /** Set the privacy for this collection. */
079    public void setPrivacy(Privacy privacy) {
080        _privacy = privacy;
081    }
082
083    /** Set the temporary directory for the results. The cleanUp() method will try to
084     *  delete this directory.
085     */
086    public void setTempDir(File dir) {
087        _tempDir = dir;
088    }
089    
090    /** Free sources. This method clears the key values, deletes the files and temporary directory. */
091    public void cleanUp() {
092        
093        _keyValues.clear();
094        
095        for(FileEntry entry : files()) {
096            if(entry.file.getParentFile().equals(_tempDir)) {
097                //System.out.println("will delete " + entry.file);
098                if(!entry.file.delete()) {
099                    System.err.println("Could not delete " + entry.file);
100                }
101            }
102        }
103        
104        _files.clear();
105    
106        if(!_tempDir.delete()) {
107            System.err.println("Could not delete " + _tempDir);
108        }
109        
110    }
111        
112    /** Add a file with a specific type to the collection.
113     *  @param file
114     *  @param type The type of file.
115     *  @param title
116     *  @param description
117     */
118    public void addFile(File file, String title, String description) {
119        FileEntry entry = new FileEntry(file.getAbsolutePath());
120        entry.setTitle(title);
121        entry.setDescription(description);
122        _files.add(entry);
123        //System.out.println("addFile " + file + ", " + title + ", " + description);
124    }
125
126    /** Share the collection with a set of emails. */
127    public void shareWith(String emailStr) {
128        _shareWith = emailStr;
129    }
130   
131    /** The privacy setting. */
132    public enum Privacy {
133        PRIVATE,
134        GROUP,
135        PUBLIC;
136    };
137    
138            
139    /** The privacy of the collection. */
140    private Privacy _privacy = Privacy.PRIVATE;
141    
142    /** A list of files to upload. */
143    private final List<FileEntry> _files = new LinkedList<FileEntry>();
144    
145    /** A set of metadata key values. */
146    private final Map<String,String> _keyValues = new HashMap<String,String>();
147    
148    /** A list of email addresses to share the collection with. */
149    private String _shareWith;
150    
151    /** The temporary directory. */
152    private File _tempDir;
153    
154}