/*
 * 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 edu.harvard.iq.dataverse;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.event.ActionEvent;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import org.primefaces.event.FileUploadEvent;

/**
 *
 * @author gdurand
 */
@ViewScoped
@Named("DatasetPage")
public class DatasetPage implements java.io.Serializable {

    @EJB
    DatasetServiceBean datasetService;
    @EJB
    DataverseServiceBean dataverseService;

    private Dataset dataset = new Dataset();
    private boolean editMode = false;
    private Long ownerId;

    public Dataset getDataset() {
        return dataset;
    }

    public void setDataset(Dataset dataset) {
        this.dataset = dataset;
    }

    public boolean isEditMode() {
        return editMode;
    }

    public void setEditMode(boolean editMode) {
        this.editMode = editMode;
    }

    public Long getOwnerId() {
        return ownerId;
    }

    public void setOwnerId(Long ownerId) {
        this.ownerId = ownerId;
    }

    public void init() {
        if (dataset.getId() != null) { // view mode for a dataset           
            dataset = datasetService.find(dataset.getId());
            ownerId = dataset.getOwner().getId();

        } else if (ownerId != null) { // create mode for a new child dataset
            editMode = true;
            dataset.setOwner(dataverseService.find(ownerId));

        } else {
            throw new RuntimeException("On Dataset page without id or ownerid."); // improve error handling
        }
    }

    public void edit(ActionEvent e) {
        editMode = true;
    }

    public void save(ActionEvent e) {
        dataset.setOwner(dataverseService.find(ownerId));
        dataset = datasetService.save(dataset);
        editMode = false;
    }

    public void cancel(ActionEvent e) {
         // reset values
        dataset = datasetService.find(dataset.getId());
        ownerId = dataset.getOwner().getId();
        editMode = false;
    }
    
    // temporary container for files
    private List files = new ArrayList();

    public List getFiles() {
        return files;
    }

    public void setFiles(List files) {
        this.files = files;
    }
    
    public void handleFileUpload(FileUploadEvent event) {  
        files.add(event.getFile());  
 
    }      

}

@@ -229,45 +229,191 @@
 ava.
-util.ArrayList;%0Aimport java.util.List
+io.IOException;%0Aimport java.nio.file.Files;%0Aimport java.nio.file.Paths;%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0Aimport java.util.logging.Level;%0Aimport java.util.logging.Logger
 ;%0Aim
@@ -577,16 +577,58 @@
 adEvent;
+%0Aimport org.primefaces.model.UploadedFile;
 %0A%0A/**%0A *
@@ -954,16 +954,81 @@
 ownerId;
+%0A    private Map%3CDataFile,UploadedFile%3E newFiles = new HashMap();
 %0A%0A    pu
@@ -1461,32 +1461,36 @@
  ownerId;%0A    %7D%0A
+    
 %0A    public void
@@ -2098,32 +2098,528 @@
 ctionEvent e) %7B%0A
+        // first save any new files%0A        for (DataFile file : newFiles.keySet()) %7B%0A            try %7B%0A                Files.createDirectory(Paths.get(%22files%22,dataset.getTitle()));%0A                Files.copy(newFiles.get(file).getInputstream(), Paths.get(%22files%22,dataset.getTitle(), file.getName()));%0A                newFiles.remove(file);%0A            %7D catch (IOException ex) %7B%0A                Logger.getLogger(DatasetPage.class.getName()).log(Level.SEVERE, null, ex);%0A            %7D%0A        %7D%0A%0A
         dataset.
@@ -2789,17 +2789,16 @@
         
- 
 // reset
@@ -2943,121 +2943,112 @@
   %7D%0A
-    
 %0A    
-// temporary container for files%0A    private List files = new ArrayList();%0A%0A    public List 
+public void handleFileUpload(FileUploadEvent event) %7B%0A        UploadedFile uFile = event.
 getFile
-s
 ()
- %7B
+;
 %0A   
@@ -3056,166 +3056,126 @@
     
-return files;%0A    %7D%0A%0A    public void setFiles(List files) %7B%0A        this.files = files;%0A    %7D
+DataFile dFile = new DataFile( uFile.getFileName(), uFile.getContentType()); 
 %0A    
-%0A
     
-public void handleFileUpload(FileUploadEvent event) %7B  
+dataset.getFiles().add( dFile );
 %0A   
@@ -3183,51 +3183,41 @@
     
-f
+newF
 iles.
-add(event.getFile());  %0A %0A    %7D      
+put(dFile, uFile);%0A    %7D
 %0A%0A%7D%0A

add initial handling of files