com.pureinfo.srm.weight.action.ProbWeightConfigRadixAction.java Source code

Java tutorial

Introduction

Here is the source code for com.pureinfo.srm.weight.action.ProbWeightConfigRadixAction.java

Source

/**
 * PureInfo Command-PHASE2
 * @(#)ProbWeightConfigRadixAction.java   1.0 2007-11-26
 * 
 * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. 
 * All rights reserved, see the license file.
 * 
 * www.pureinfo.com.cn
 */

package com.pureinfo.srm.weight.action;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;

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

import com.pureinfo.ark.ArkExceptionTypes;
import com.pureinfo.ark.auth2.Auth2Helper;
import com.pureinfo.ark.auth2.domain.IAuth2Mgr;
import com.pureinfo.ark.interaction.ActionBase;
import com.pureinfo.force.PureSystem;
import com.pureinfo.force.exception.PureException;
import com.pureinfo.force.io.ClassResourceUtil;
import com.pureinfo.srm.weight.helper.WeightConstants;
import com.pureinfo.srm.weight.model.WeightConfig;

/**
 * <P>
 * Created on 2007-11-26 06:21:48<BR>
 * Last modified on 2007-11-26
 * </P>
 * ProbWeightConfigRadixAction
 * 
 * @author sunjie
 * @version 1.0, 2007-11-26
 * @since Command-PHASE2 1.0
 */
public class ProbWeightConfigRadixAction extends ActionBase {

    /**
     * @see com.pureinfo.ark.interaction.ActionBase#beforeExecution()
     */
    protected ActionForward beforeExecution() throws PureException {
        IAuth2Mgr authMgr = Auth2Helper.getManager();
        int index = authMgr.indexOfAction("configWeightRadix", false);
        if (!authMgr.hasRight(loginUser, new WeightConfig(), index, false)) {
            throw new PureException(ArkExceptionTypes.AUTH_NO_RIGHT, "");
        }
        return super.beforeExecution();
    }

    /**
     * @see com.pureinfo.ark.interaction.ActionBase#executeAction()
     */
    public ActionForward executeAction() throws PureException {
        String adjustType = request.getRequiredParameter("configType", "config type");
        String sFileName = getPropertiesFileName(adjustType);
        if (sFileName == null) {
            return mapping.findForward("success");
        }

        sFileName = ClassResourceUtil.mapFullPath(sFileName, true);

        PrintWriter pw = null;
        try {
            pw = new PrintWriter(
                    new BufferedWriter(new OutputStreamWriter(new FileOutputStream(sFileName), "iso-8859-1")));
            Enumeration e = request.getParameterNames();
            while (e.hasMoreElements()) {
                String element = (String) e.nextElement();
                if (element.startsWith("weight.value.")) {
                    pw.println(element + "=" + request.getParameter(element).trim());
                    PureSystem.setProperty(element, request.getParameter(element).trim());
                }
            }
        } catch (FileNotFoundException ex) {
            throw new PureException(PureException.UNKNOWN, "", ex);
        } catch (UnsupportedEncodingException ex) {
            throw new PureException(PureException.UNKNOWN, "", ex);
        } finally {
            if (pw != null)
                pw.close();
        }
        request.setAttribute("forward", "../weight/weight-config-radix.jsp");
        return mapping.findForward("success");
    }

    /**
     * @param _sAdjustType
     * @return
     * @throws PureException 
     */
    private String getPropertiesFileName(String _sAdjustType) throws PureException {
        if (StringUtils.isEmpty(_sAdjustType)) {
            throw new PureException(0, "");
        }
        String path = "plugin/system/";
        String fileName = (String) WeightConstants.WeightConfigRadixType.PROP_FILE_NAME_BUNDLE.get(_sAdjustType);
        if (StringUtils.isEmpty(fileName)) {
            throw new PureException(0, "");
        }
        return path + fileName;
    }

}