com.pureinfo.srm.reports.table.data.pinggu.ParameterSetAction.java Source code

Java tutorial

Introduction

Here is the source code for com.pureinfo.srm.reports.table.data.pinggu.ParameterSetAction.java

Source

/**
 * PureInfo Command-phase2
 * @(#)ParameterSetAction.java   1.0 2008-7-3
 * 
 * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. 
 * All rights reserved, see the license file.
 * 
 * www.pureinfo.com.cn
 */

package com.pureinfo.srm.reports.table.data.pinggu;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Enumeration;
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.force.PureSystem;
import com.pureinfo.force.exception.PureException;
import com.pureinfo.force.io.ClassResourceUtil;

/**
 * <P>
 * Created on 2008-7-3 10:22:29<BR>
 * Last modified on 2008-7-3
 * </P>
 * TODO describe ParameterSetAction here ...
 * 
 * @author jie.sun
 * @version 1.0, 2008-7-3
 * @since Command-phase2 1.0
 */
public class ParameterSetAction extends ActionBase {
    String tempOrgCode;

    String propFileName = "plugin/system/system-pinggu-parameter.properties";

    /**
     * @see com.pureinfo.ark.interaction.ActionBase#executeAction()
     */
    public ActionForward executeAction() throws PureException {
        tempOrgCode = request.getRequiredParameter("tempOrgCode", "code");
        propFileName = ClassResourceUtil.mapFullPath(propFileName, true);

        Properties prop = new Properties();
        InputStream iFile = null;
        FileOutputStream oFile = null;
        try {
            iFile = new FileInputStream(propFileName);
            prop.load(iFile);

            String codeInSystem = prop.getProperty("parameter.org.code");
            if (StringUtils.isEmpty(codeInSystem)) {
                codeInSystem = tempOrgCode;
            } else {
                String[] codeArr = codeInSystem.split(",");
                Arrays.sort(codeArr);
                int index = Arrays.binarySearch(codeArr, tempOrgCode);
                if (index < 0) {
                    codeInSystem += "," + tempOrgCode;
                }
            }
            prop.setProperty("parameter.org.code", codeInSystem);

            Enumeration e = request.getParameterNames();
            while (e.hasMoreElements()) {
                String name = (String) e.nextElement();
                if (!name.startsWith("para.")) {
                    continue;
                }
                String value = request.getParameter(name);

                if (StringUtils.isEmpty(value)) {
                    logger.debug("the value of " + name.substring(5) + " is empty.SKIP.");
                    continue;
                }

                value = value.trim();

                logger.debug("to set property:" + name.substring(5) + "with[" + value + "]");

                prop.setProperty(name.substring(5), value);
            }

            oFile = new FileOutputStream(propFileName, false);

            prop.store(oFile, "parameter for pinggu");

            // to reload properties.
            PureSystem.shutdown();

        } catch (Exception ex) {
            throw new PureException(0, "", ex);
        } finally {
            try {
                if (iFile != null)
                    iFile.close();
                if (oFile != null)
                    oFile.close();
                prop.clear();
            } catch (IOException e) {
                e.printStackTrace(System.err);
            }

        }

        return mapping.findForward("success");
    }

}