Talar97

Upload kontroler

Feb 23rd, 2020
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.04 KB | None | 0 0
  1. package file;
  2.  
  3. import java.sql.Timestamp;
  4.  
  5. import javax.annotation.PostConstruct;
  6. import javax.enterprise.context.RequestScoped;
  7. import javax.faces.application.FacesMessage;
  8. import javax.faces.context.FacesContext;
  9. import javax.inject.Inject;
  10. import javax.inject.Named;
  11.  
  12. import org.primefaces.model.UploadedFile;
  13.  
  14. import com.jsfcourse.login.ClientData;
  15.  
  16. import dao.FileDAO;
  17. import dao.FileListDAO;
  18. import entities.File;
  19. import entities.FileList;
  20.  
  21. @Named
  22. @RequestScoped
  23. public class FileListUploadBB {
  24.     private FileList fileList = new FileList();
  25.     private UploadedFile file;
  26.     private static final String PAGE_STAY_AT_THE_SAME = "/pages/files/upload.xhtml";
  27.    
  28.     @Inject
  29.     FileListDAO fListDAO;
  30.    
  31.     @Inject
  32.     FileDAO fileDAO;
  33.    
  34.     @Inject
  35.     ClientData clientData;
  36.    
  37.     @PostConstruct
  38.     public void init() {
  39.        
  40.     }
  41.    
  42.     public boolean validate() {
  43.         return true;
  44.     }
  45.    
  46.     public String upload() {
  47.         FacesContext ctx = FacesContext.getCurrentInstance();
  48.         if(file == null || file.getFileName() == null) {
  49.             ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
  50.                     "Nie wybraΕ‚eΕ› pliku", null));
  51.         }
  52.         else {
  53.             if(validate()) {
  54.                 File fileToUpload = new File();
  55.                 fileToUpload.setFileBinary(file.getContents());
  56.                 fileToUpload.setFilename(file.getFileName());
  57.                 fileDAO.create(fileToUpload);
  58.                
  59.                 File f = fileDAO.getLastAddedObject();
  60.                 fileList.setFile(f);
  61.                 fileList.setAddedTime(new Timestamp(System.currentTimeMillis()));
  62.                 fileList.setUser(clientData.getClient());
  63.                 fListDAO.create(fileList);
  64.                
  65.                 ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
  66.                         "Utworzono plik!", null));
  67.                
  68.                 fileList = null;
  69.                 file = null;
  70.                 return PAGE_STAY_AT_THE_SAME;
  71.             }
  72.         }
  73.        
  74.         return null;
  75.     }
  76.    
  77.     public FileList getFileList() {
  78.         return fileList;
  79.     }
  80.  
  81.     public void setFileList(FileList fileList) {
  82.         this.fileList = fileList;
  83.     }
  84.  
  85.     public UploadedFile getFile() {
  86.         return file;
  87.     }
  88.  
  89.     public void setFile(UploadedFile file) {
  90.         this.file = file;
  91.     }
  92.    
  93. }
Add Comment
Please, Sign In to add comment