Example usage for org.apache.commons.io FileUtils copyFileToDirectory

List of usage examples for org.apache.commons.io FileUtils copyFileToDirectory

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils copyFileToDirectory.

Prototype

public static void copyFileToDirectory(File srcFile, File destDir, boolean preserveFileDate)
        throws IOException 

Source Link

Document

Copies a file to a directory optionally preserving the file date.

Usage

From source file:org.jwebsocket.plugins.reporting.ReportingPlugIn.java

/**
 * Upload a report template/* w  w  w.  j  av  a 2 s  .  c  om*/
 *
 * @param aConnector
 * @param aToken
 * @throws Exception
 */
@Role(name = NS_REPORTING + ".uploadTemplate")
@RequirePlugIn(id = "jws.filesystem")
public void uploadTemplateAction(WebSocketConnector aConnector, Token aToken) throws Exception {
    String lTemplatePath = aToken.getString("templatePath");
    Assert.notNull(lTemplatePath, "The 'templatePath' argument cannot be null!");
    Assert.isTrue(lTemplatePath.endsWith(".jrxml"), "Invalid template extension!");

    String lUserPath = getUserHome(aConnector);
    File lTemplate = new File(lUserPath + File.separator + lTemplatePath);
    Assert.isTrue(lTemplate.exists() && lTemplate.canWrite(), "The target file does not exists!");

    FileUtils.copyFileToDirectory(lTemplate, new File(mJasperReportService.getSettings().getReportFolder()),
            true);
    lTemplate.delete();
    // sending default acknowledge response
    sendToken(aConnector, createResponse(aToken));
}

From source file:org.jwebsocket.plugins.reporting.service.JasperReportService.java

/**
 * Save the report in the desire format. Generate an array of bytes to
 * transform the report in a base64 String.
 *
 * @param aUserHome/*  w  w w.  j  a v  a2s.co m*/
 * @param aReportName
 * @param aParams
 * @param aFields
 * @param aConnection
 * @param aFormat
 * @return
 * @throws Exception
 */
@Override
public String generateReport(String aUserHome, String aReportName, Map<String, Object> aParams,
        List<Map<String, Object>> aFields, Connection aConnection, String aFormat) throws Exception {

    // getting the report template path to compile
    String lTemplatePath = getReportTemplatePath(aReportName);
    // a JasperReport object
    JasperReport lJasperReport;
    // searching for JasperReport object in cache to improve performance
    if (mCache.containsKey(lTemplatePath)) {
        // initializing the JasperReport object from cache
        lJasperReport = mCache.get(lTemplatePath);
    } else {
        // compiling the JasperReport object using the report template path
        lJasperReport = JasperCompileManager.compileReport(lTemplatePath);
        mCache.put(lTemplatePath, lJasperReport);
    }
    // JasperPrint Object
    JasperPrint lJasperPrint;
    if (null != aConnection) {
        lJasperPrint = JasperFillManager.fillReport(lJasperReport, aParams, aConnection);
    } else {
        Assert.notNull(aFields, "The 'fields' arguments cannot be null!");
        JRBeanCollectionDataSource lReportDataSource = new JRBeanCollectionDataSource(aFields);
        lJasperPrint = JasperFillManager.fillReport(lJasperReport, aParams, lReportDataSource);
    }
    // getting the directory for the report 
    String lOutputDir = mSettings.getOutputFolder().replace("${USER_HOME}", aUserHome);
    FileUtils.forceMkdir(new File(lOutputDir));

    // the final zip file path
    String lFinalPath = "";

    String lDestFile = lOutputDir + File.separator + aReportName;
    // generating the report
    if (ReportFormats.PDF.equals(aFormat)) {
        lDestFile = lDestFile + ".pdf";
        JasperExportManager.exportReportToPdfFile(lJasperPrint, lDestFile);
        lFinalPath = lDestFile;
    } else if (ReportFormats.HTML.equals(aFormat)) {
        lDestFile = lDestFile + ".html";
        JasperExportManager.exportReportToHtmlFile(lJasperPrint, lDestFile);

        // getting the 'report_name'.html page and the report_name_folder_files
        File lFilesDirectory = new File(lDestFile + "_files");
        File lFilePage = new File(lDestFile);

        // moving resulting files to a unique directory to get zip
        File lReportZipFolder = new File(lOutputDir + File.separator + aReportName);
        FileUtils.forceMkdir(lReportZipFolder);
        FileUtils.copyDirectoryToDirectory(lFilesDirectory, lReportZipFolder);
        FileUtils.copyFileToDirectory(lFilePage, lReportZipFolder, true);

        // deleting the resulting files of reporting
        FileUtils.forceDelete(lFilesDirectory);
        FileUtils.forceDelete(lFilePage);

        // using the zip method of FileUtils
        String[] lFiles = new String[] { lReportZipFolder.getPath() };
        Tools.zip(lFiles, lOutputDir + File.separator + aReportName + ".zip");
        FileUtils.deleteDirectory(lReportZipFolder);

        lFinalPath = lOutputDir + aReportName + ".zip";
    } else {
        throw new Exception("The given format is not supported!");
    }
    return lFinalPath.replace(aUserHome, "");
}