Package org.boco.seamUtility.upload

Source Code of org.boco.seamUtility.upload.FileManagement

/***************************************************************************
* Copyright (c) 2004 - 2008  Fabrizio Boco fabboco@users.sourceforge.net  *
*                                                                         *
*                                                                         *
*   This is free software; you can redistribute it and/or                 *
*   modify it under the terms of the GNU Library General Public           *
*   License (version 2.1) as published by the Free Software Foundation    *
*                                                                         *
*   This library  is distributed in the hope that it will be useful,      *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU Library General Public License for more details.                  *
*                                                                         *
*   You should have received a copy of the GNU Library General Public     *
*   License along with this library; see the file COPYING.LIB. If not,    *
*   write to the Free Software Foundation, Inc., 59 Temple Place,         *
*   Suite 330, Boston, MA  02111-1307, USA                                *
*                                                                         *
***************************************************************************/

/**
- $Header: /usr/local/cvslocalrepository/SeamUtility/src/org/boco/seamUtility/upload/Attic/FileManagement.java,v 1.1.2.3 2008/04/22 06:14:36 fab Exp $
- $Author: fab $
- $Revision: 1.1.2.3 $
- $Date: 2008/04/22 06:14:36 $
- $Log: FileManagement.java,v $
- Revision 1.1.2.3  2008/04/22 06:14:36  fab
- Aggiornamento indirizzo di posta
-
- Revision 1.1.2.2  2008/04/19 11:14:39  fab
- Aggiornamento riferimenti licenza e documentazione
-
- Revision 1.1.2.1  2008/03/04 08:41:45  fab
- *** empty log message ***
-
- Revision 1.1  2007/09/30 07:45:40  fab
- Nuova versione iniziale del 30/09/2007
-
- Revision 1.5  2007/07/20 17:01:51  fab
- Fix per SEAM 2
-
- Revision 1.4  2007/07/17 19:37:58  fab
- Inserito il campo checkUpdate per gestire la modifica del file allegato
-
- Revision 1.3  2007/07/17 13:53:48  fab
- Inserito il campo checkDelete per gestire la rimozione del file allegato
-
- Revision 1.2  2007/04/30 15:59:34  dev
- Gestione degli errori nel download
- La proprietà data è resa LAZY ma non credo che abbia effetto alcuno !
-
- Revision 1.1  2007/04/22 10:56:53  dev
- Supporto per la gestione dei file in upload/download
-
**/

package org.boco.seamUtility.upload;

import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.persistence.Basic;
import javax.persistence.Embeddable;
import javax.persistence.FetchType;
import javax.persistence.Transient;
import javax.servlet.http.HttpServletResponse;

import org.jboss.seam.faces.FacesMessages;

@Embeddable
public class FileManagement implements Serializable
{

  private static final long  serialVersionUID  = 1L;
  private byte[]          data;
  private String          fileName;
  private String          contentType;
  private Boolean        checkDelete=false;
  private Boolean        checkUpdate=false;

  public String getContentType()
  {
    return contentType;
  }

  public void setContentType(String contentType)
  {
    this.contentType = contentType;
  }

  @Basic(fetch = FetchType.LAZY)
  public byte[] getData()
  {
    return data;
  }

  public void setData(byte[] data)
  {
    this.data = data;
  }

  public String getFileName()
  {
    return fileName;
  }

  public void setFileName(String fileName)
  {
    this.fileName = fileName;
  }

  public void downloadFile(FacesContext facesContext)
  {
    ExternalContext context = facesContext.getExternalContext();

    if (data == null)
    {
      FacesMessages messages = FacesMessages.instance();

      FacesMessage message = new FacesMessage();
      message.setDetail("MANCA IL FILE");
      message.setSummary("MANCA IL FILE");
      message.setSeverity(FacesMessage.SEVERITY_INFO);

      messages.add(message);
      return;
    }

    HttpServletResponse response = (HttpServletResponse) context.getResponse();
    response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
    response.setContentLength((int) data.length);
    response.setContentType(contentType);

    try
    {
      OutputStream out = response.getOutputStream();

      //      // Copy the contents of the file to the output stream
      //      byte[] buf = new byte[1024];
      //      int count;
      //      while ((count = in.read(buf)) >= 0)
      //      {
      //        out.write(buf, 0, count);
      //      }
      //      in.close();

      out.write(data, 0, data.length);

      out.flush();
      out.close();
      facesContext.responseComplete();
    }
    catch (IOException ex)
    {
      System.out.println("Error in downloadFile: " + ex.getMessage());
      ex.printStackTrace();
    }
  }

  @Transient
  public Boolean getCheckDelete()
  {
    return checkDelete;
  }

 
  public void setCheckDelete(Boolean delete)
  {
    this.checkDelete = delete;
  }

  @Transient
  public Boolean getCheckUpdate()
  {
    return checkUpdate;
  }

 
  public void setCheckUpdate(Boolean update)
  {
    this.checkUpdate = update;
  }
}
TOP

Related Classes of org.boco.seamUtility.upload.FileManagement

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.