Subversion Repositories SmartDukaan

Rev

Rev 5885 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/**
 * 
 */
package in.shop2020.support.controllers;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.dispatcher.StreamResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author mandeep
 * 
 */
public class FileArchiveController extends ActionSupport {
    private static Logger logger = LoggerFactory
            .getLogger(FileArchiveController.class);
    private File file;
    private int fileType;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");

    public StreamResult create() {
        try {
            logger.info("Trying to archive file with type: " + fileType);
            FileUtils.copyFile(file, new File("/InventoryReports/" + fileType
                    + "-" + sdf.format(new Date()) + ".xls"));

            return new StreamResult(new ByteArrayInputStream(
                    ("File uploaded successfully at " + sdf.format(new Date())).getBytes()));

        } catch (IOException e) {
            logger.error("Error archiving file", e);
            return new StreamResult(new ByteArrayInputStream(
                    ("Could not upload file at " + sdf.format(new Date())).getBytes()));            
        }
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public int getFileType() {
        return fileType;
    }

    public void setFileType(int fileType) {
        this.fileType = fileType;
    }
}