Java tutorial
/* * The DecidR Development Team licenses this file to you 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 de.decidr.ui.view; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.HashSet; import org.apache.commons.io.IOUtils; import com.vaadin.terminal.FileResource; import com.vaadin.terminal.Resource; import com.vaadin.ui.Button; import com.vaadin.ui.CustomComponent; import com.vaadin.ui.HorizontalLayout; import com.vaadin.ui.Upload; import com.vaadin.ui.Button.ClickEvent; import com.vaadin.ui.Upload.Receiver; import com.vaadin.ui.Upload.SucceededEvent; import de.decidr.model.acl.permissions.FilePermission; import de.decidr.model.acl.permissions.FileReadPermission; import de.decidr.model.annotations.Reviewed; import de.decidr.model.annotations.Reviewed.State; import de.decidr.model.entities.File; import de.decidr.model.exceptions.TransactionException; import de.decidr.model.facades.FileFacade; import de.decidr.ui.controller.UIDirector; import de.decidr.ui.controller.UploadAction; import de.decidr.ui.main.DecidrUI; import de.decidr.ui.main.ModelFacades; import de.decidr.ui.view.windows.TransactionErrorDialogComponent; /** * This component provides an upload field to upload data for the work item the * user works on. If a file is uploaded, the component is changed into a text * field and a button, so the user can delete the last file he uploaded. * * @author AT */ @Reviewed(reviewers = { "unknown" }, lastRevision = "0", currentReviewState = State.NeedsReview) public class UploadComponent extends CustomComponent implements Upload.SucceededListener { private static final long serialVersionUID = 1L; private Long fileId = null; private Receiver receiver = null; private HorizontalLayout horizontalLayout = null; private Button button = null; private Button download = null; private com.vaadin.ui.Upload upload = null; private FileFacade fileFacade = ModelFacades.getFileFacade(); private UIDirector uiDirector = DecidrUI.getCurrent().getUIDirector(); private SiteFrame siteFrame = uiDirector.getTemplateView(); boolean deleted = false; /** * Constructor with a file ID. The file ID is stored and the init method is * called to initialize the components. * * @param fileId * - The file ID which is stored in the work item. */ public UploadComponent(Long fileId, Receiver receiver) { this.fileId = fileId; this.receiver = receiver; init(); } /** * Initializes the components from the upload component. */ private void init() { // Checks if the file ID is null. This can only happen by the first // time, if there isn't a file ID stored in the work item. If it is // null, the deleted flag is set to true if (fileId == null) { deleted = true; } horizontalLayout = new HorizontalLayout(); this.setCompositionRoot(horizontalLayout); horizontalLayout.removeAllComponents(); // If the file is "deleted", that means if there isn't a file id in the // work item or the user has deleted his last upload then the component // is built with an upload component from vaadin, so that the user is // able to upload a new file. If the file is not deleted or a file id is // stored in the work item then the filename is shown to the user and he // can delete this file and upload a new one if (deleted) { upload = new com.vaadin.ui.Upload("File URI", receiver); horizontalLayout.addComponent(upload); upload.addListener(this); } else { File file; try { file = fileFacade.getFileInfo(fileId); button = new Button("Delete file"); download = new Button("Download file"); download.setStyleName(Button.STYLE_LINK); button.addListener(new Button.ClickListener() { /** * TODO: add comment */ private static final long serialVersionUID = 1L; @Override public void buttonClick(ClickEvent event) { UploadComponent.this.deleted = true; UploadComponent.this.init(); } }); download.addListener(new Button.ClickListener() { /** * TODO: add comment */ private static final long serialVersionUID = 1L; @Override public void buttonClick(ClickEvent event) { try { InputStream in = fileFacade.getFileData(fileId); java.io.File file = java.io.File.createTempFile("decidr-download", ""); OutputStream out = new FileOutputStream(file); IOUtils.copy(in, out); DecidrUI.getCurrent().getMainWindow().showNotification("Successful download", "File successfully downloaded!"); } catch (Exception e) { DecidrUI.getCurrent().getMainWindow().showNotification("File not found", "File couldn't be found!"); } } });// DecidrUI.getCurrent().getMainWindow().addWindow( // new InformationDialogComponent("File " // + event.getFilename() // + " successfully uploaded!", // "Success")); horizontalLayout.setSpacing(true); horizontalLayout.addComponent(download); horizontalLayout.addComponent(button); download.setCaption(file.getFileName()); } catch (TransactionException e) { DecidrUI.getCurrent().getMainWindow().showNotification("File not found", "File couldn't be found! Please upload an other file!"); UploadComponent.this.deleted = true; UploadComponent.this.init(); } } } /** * Return ID of the uploaded file, or <tt>null</tt> if the user hasn't uploaded one. */ public Long getFileId() { if (deleted) { return null; } else { return fileId; } } @Override public void uploadSucceeded(SucceededEvent event) { UploadAction action = (UploadAction) receiver; java.io.File file = action.getFile(); if (file == null) { DecidrUI.getCurrent().getMainWindow().showNotification("Failure", "Illegal Argument: File must not be null!"); } else { FileInputStream fis; try { fis = new FileInputStream(file); HashSet<Class<? extends FilePermission>> filePermission = new HashSet<Class<? extends FilePermission>>(); filePermission.add(FileReadPermission.class); if (fileId != null) { fileFacade.replaceFile(fileId, fis, file.length(), event.getFilename(), event.getMIMEType()); } else { fileId = fileFacade.createFile(fis, file.length(), event.getFilename(), event.getMIMEType(), true, filePermission); } DecidrUI.getCurrent().getMainWindow().setData(fileId); // That message box is just annoying. ~wf // DecidrUI.getCurrent().getMainWindow().addWindow( // new InformationDialogComponent("File " // + event.getFilename() // + " successfully uploaded!", // "Success")); // Refreshes the header with the new logo // FIXME: This really shouldn't be done here. if ((siteFrame.getContent() instanceof TenantSettingsComponent) && (siteFrame.getHeader() instanceof Header)) { TenantSettingsComponent tsc = (TenantSettingsComponent) siteFrame.getContent(); Header header = (Header) siteFrame.getHeader(); Resource resource = new FileResource(file, getApplication()); tsc.getLogoEmbedded().setSource(resource); header.getDecidrLogo().setSource(resource); tsc.requestRepaint(); header.requestRepaint(); } UploadComponent.this.deleted = false; UploadComponent.this.init(); } catch (FileNotFoundException e) { DecidrUI.getCurrent().getMainWindow().showNotification("File not found", "File couldn't be found!"); } catch (TransactionException e) { DecidrUI.getCurrent().getMainWindow().addWindow(new TransactionErrorDialogComponent(e)); } } } }