com.pureinfo.srm.srm2rpms.action.ExportAction.java Source code

Java tutorial

Introduction

Here is the source code for com.pureinfo.srm.srm2rpms.action.ExportAction.java

Source

/**
 * PureInfo Quake
 * 
 * @(#)ExportAction.java 1.0 Jun 28, 2006
 * 
 * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. All rights
 * reserved, see the license file.
 * 
 * www.pureinfo.com.cn
 */

package com.pureinfo.srm.srm2rpms.action;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForward;

import com.pureinfo.ark.interaction.ActionBase;
import com.pureinfo.dolphin.config.DolphinConfigHelper;
import com.pureinfo.dolphin.config.DolphinEnvironment;
import com.pureinfo.force.exception.PureException;
import com.pureinfo.force.io.ClassResourceUtil;
import com.pureinfo.force.io.IOUtil;
import com.pureinfo.srm.srm2rpms.BuildInfo;

public class ExportAction extends ActionBase {

    public ActionForward executeAction() throws PureException {
        if (BuildInfo.isUpdated()) {
            //0. to get the parameter
            String[] tables = request.getParameterValues("data");
            if (tables == null || tables.length == 0) {
                throw new PureException(PureException.INVALID_REQUEST, "");
            }

            try {
                //1. to read database info
                Properties props = DolphinConfigHelper.lookupConnectionProviderCfgByName("Export");
                String sDBName = props.getProperty(DolphinEnvironment.DATABASE);
                String sPassword = props.getProperty(DolphinEnvironment.PASSWORD);
                String sPort = props.getProperty("port");

                //2. to execute command to dump sql script
                String sPath = ClassResourceUtil.mapFullPath("mysqldump.exe", true);
                String sCommand = sPath.substring(1) + " -u root -p" + sPassword + " -P " + sPort
                        + " --compatible=mysql323 " + sDBName;
                sCommand += " " + StringUtils.join(tables, ' ');
                logger.debug("do dump sql script: " + sCommand);

                Process process = Runtime.getRuntime().exec(sCommand);
                InputStream is = process.getInputStream();

                //3. to download
                response.setContentType("application/rpms");
                response.setHeader("Content-Disposition", "attachment; filename=srm-export.rpms");
                OutputStream os = response.getOutputStream();
                IOUtil.transfer(is, os);
                is.close();
                os.close();
            } catch (Exception ex) {
                throw new PureException(0, "dump sql script from school", ex);
            }
        }
        return null;
    }

}