wizardconfig.java

来自「Java Pattern Oriented Framework (Jt) 是为了」· Java 代码 · 共 449 行

JAVA
449
字号
package Jt.wizard;

import java.io.File;

import org.apache.struts.action.ActionForm;

import Jt.JtContext;
import Jt.JtFactory;
import Jt.JtHashTable;
import Jt.JtIterator;
import Jt.JtMessage;
import Jt.JtObject;
import Jt.DAO.JtDAOAdapter;
import Jt.DAO.JtDAOFileAdapter;
import Jt.wizard.struts.CRUDHelper;
import Jt.wizard.struts.form.PropertiesForm;


/**
 * Configuration information for the JtWizard
 */

public class WizardConfig extends JtObject {
    private static final long serialVersionUID = 1L;
    public static final String JtCLASS_NAME = WizardConfig.class.getName(); 
    public static final String GET_STRUTS_CONFIG_PATH = "GET_STRUTS_CONFIG_PATH";
    
    private String webappsPath = "/temp/webapps";
    private String configFile = "struts-config.xml";
    private String webAppPath = null;
    private String appName = "Test";
    private String configPath = null;
    private String workingDirectory = "/temp";
    
    private JtFactory factory = new JtFactory ();
    private JtDAOFileAdapter adapter;
    private CRUDHelper helper;
    
    transient private boolean _initted = false;
    
    public WizardConfig () {
        
    }
    
    private String concatenatePath (String path, String subpath) {
        char separator = File.separatorChar;
        File file;


        if (path == null)
            return null;
        if (subpath == null)
            return path;

        file = new File (path);


        return (file.getAbsolutePath() + separator + subpath);

    }
    
      
    public String getConfigPath() {
        //String temp;
        
        //if (!_initted)
        //    initializeWizard ();
 
        /*
        webappsPath = concatenatePath (workingDirectory, "webapps");

        webAppPath = concatenatePath (webappsPath, appName);
        
        temp = concatenatePath (webAppPath, "WEB-INF");
        configPath = concatenatePath (temp,  configFile);
        */
        
        return configPath;
    }
    public void setConfigPath(String configPath) {
        //this.configPath = configPath; void
    }
    
    public String getConfigFile() {
        
        //if (!_initted)
        //    initializeWizard ();
        
        return configFile;
    }


    public void setConfigFile(String configFile) {
        this.configFile = configFile;
    }


    public String getWorkingDirectory() {
        return workingDirectory;
    }


    public void setWorkingDirectory(String workingDirectory) {
        this.workingDirectory = workingDirectory;
    }


    public String getWebAppPath() {
        
        //if (!_initted)
        //    initializeWizard ();
/*        
        webappsPath = concatenatePath (workingDirectory, "webapps");
        
        webAppPath = concatenatePath (webappsPath, appName);
*/        
        return webAppPath;
    }
    
    public void setWebAppPath(String webappPath) {
        // this.webAppPath = webappPath; void
    }
    
    
    public String getWebappsPath() {
        
        //if (!_initted)
        //    initializeWizard ();
        
        return webappsPath;
    }
    
    public void setWebappsPath(String webappsPath) {
        //this.webappsPath = webappsPath; void
    }
    
    public String getAppName() {
        return appName;
    }
    public void setAppName(String appName) {
        this.appName = appName;
    }
    
    /*
    private String getStrutsConfigPath () {
        if (configFile == null) {
            return (null);
        }
        return (null);
    }
    */
    
    private void createWizard () {

        //String tmp;       
        //File file;
        JtMessage msg = new JtMessage (JtDAOAdapter.JtCREATE);


        //tmp = concatenatePath (workingDirectory, "WizardConfig.xml");

        //file = new File (tmp);

        //if (!file.exists()) {
        msg.setMsgContent(this);
        //adapter.setPath(tmp);
        factory.sendMessage (adapter, msg);
        // }

    }
    
    private void initializeWizard () {
        String temp;
        
        handleTrace ("initializeWizard ...");
        
        if (configFileExists ())
            readConfigFile ();
        
        webappsPath = concatenatePath (workingDirectory, "webapps");

        webAppPath = concatenatePath (webappsPath, appName);
        
        temp = concatenatePath (webAppPath, "WEB-INF");
        configPath = concatenatePath (temp,  configFile);
        
        _initted = true;       
    }
    
    private boolean readConfigFile () {
        JtDAOFileAdapter adapter;
        String path;
        JtMessage msg = new JtMessage ();
        Object obj;
        
        adapter = (JtDAOFileAdapter) factory.createObject (JtDAOFileAdapter.JtCLASS_NAME);
        
        path = this.getWorkingDirectory();
        
        adapter.setPath(concatenatePath (path, "WizardConfig.xml"));


        msg.setMsgId (JtDAOAdapter.JtREAD);


        obj = factory.sendMessage (adapter, msg);

        // Propagate the exception

        if (adapter.getObjException() != null) {
            this.setObjException(adapter.getObjException());
            return (false);
        }
        
        if (obj == null)
            return false;
        
        msg = new JtMessage (JtObject.JtCOPY);
        msg.setMsgContent(obj);
        msg.setMsgData(this);
        
        factory.processMessage (msg);
        
        return true;
    }
    
    
    private PropertiesForm createPropertiesForm () {
        PropertiesForm form;
        
        form = new PropertiesForm ();
        
        form.setAppName(appName);
        form.setConfigFile(configFile);
        form.setWorkingDirectory(workingDirectory);
        
        return (form);
        
    }
    
    private boolean configFileExists () {
        File file;
        String tmp;   
        
        
        tmp = concatenatePath (workingDirectory, "WizardConfig.xml");
        file = new File (tmp);
        
        return (file.exists());
        
    }
    
    private JtHashTable getAttributes (Object obj) {
        JtMessage msg = new JtMessage (JtFactory.JtGET_ATTRIBUTES);

        if (obj == null)
            return (null);
        msg.setMsgContent(obj);

        return ((JtHashTable) factory.processMessage(msg));

    }
    
    

    private void copyFormAttributes (ActionForm form, Object obj) {
        JtHashTable attributes;
        JtIterator iterator;
        String attribute;
        Object value;

        if (obj == null || form == null)
            return;
        attributes = getAttributes (obj);

        if (attributes == null)
            return;
        iterator = (JtIterator) attributes.processMessage(new JtMessage (JtHashTable.JtGET_KEYS));

        if (iterator == null)
            return;

        for (;;) {
            attribute = (String) iterator.processMessage(new JtMessage (JtIterator.JtNEXT));
            if (attribute == null)
                break;
            value = factory.getValue(form,attribute);
            factory.setValue (obj, attribute, value);

        }

    }
    
    
    private Object readConfig (Object data) {
        adapter = new JtDAOFileAdapter ();
        CRUDHelper helper = new CRUDHelper (); //change
        String tmp;   
        File file;
        Object output;
        
        JtMessage msg = new JtMessage (JtDAOAdapter.JtREAD);

        tmp = concatenatePath (workingDirectory, "WizardConfig.xml");
        file = new File (tmp);
        adapter.setPath(tmp);

        if (!file.exists()) {
            return (createPropertiesForm ());
            //createWizard ();
        }       
        
        
        readConfigFile ();
        
        //helper.setClassname(WizardConfig.JtCLASS_NAME);
        //helper.setFilename("WizardConfig.xml");
        //msg.setMsgData (data);
        //output = factory.sendMessage(helper, msg);
        
        //if (helper.getObjException() != null) {
        //    this.setObjException(helper.getObjException());
        //} 
        
        return (createPropertiesForm ());
                
    }
    
    
    private void updateConfig (ActionForm form) {
        JtMessage msg = new JtMessage ();
        String path;
        JtDAOFileAdapter adapter = (JtDAOFileAdapter) factory.createObject (JtDAOFileAdapter.JtCLASS_NAME);

        //Member member = new Member ();


        if (form == null) {
            handleWarning ("WizardConfig.updateObject: form is null.");
            return; // check
        }    

        copyFormAttributes (form, this);

        msg = new JtMessage (JtDAOAdapter.JtUPDATE);   
        msg.setMsgContent(this);

        
        path = this.getWorkingDirectory();
        
        adapter.setPath(concatenatePath (path, "WizardConfig.xml"));

        factory.sendMessage (adapter, msg);

        // Propagate the exception

        if (adapter.getObjException() != null) {
            this.setObjException(adapter.getObjException());
        } 

    }

/*    
    private void updateConfig (Object data) {

        JtMessage msg = new JtMessage (JtDAOAdapter.JtUPDATE);
        
        helper = new CRUDHelper ();
        helper.setClassname(WizardConfig.JtCLASS_NAME);
        helper.setFilename("WizardConfig.xml");
        msg.setMsgData (data);
        
        // Check - propagate exception
        factory.sendMessage(helper, msg);
        
        // Propagate the exception

        if (helper.getObjException() != null) {
            this.setObjException(helper.getObjException());
        } 
    }
*/
    
    /**
     * Process object messages.
     * <ul>
     * <li> JtACTIVATE
     * </ul>
     * @param message message
     */

    public Object processMessage (Object message) {
        //Object data;
        //String content;

        JtMessage e = (JtMessage) message;
        Object data;
        JtContext context;
        ActionForm form = null;

        if (e == null ||  (e.getMsgId() == null))
            return (null);
        
        context = (JtContext) e.getMsgContext();
        if (context != null)
            form = (ActionForm) context.getActionForm();  


        if (e.getMsgId().equals(JtObject.JtINITIALIZE)) {
            
            // pass the form information   

            if (!_initted)
                initializeWizard ();
            return (null);

        }
        

        if (e.getMsgId().equals(JtDAOAdapter.JtREAD)) {
            
            // pass the form information   

            data = e.getMsgData ();

            return (readConfig (form));

        }
        
        if (e.getMsgId().equals(JtDAOAdapter.JtUPDATE)) {
            
            // pass the form information   

            //data = e.getMsgData ();
            if (!_initted)
                initializeWizard ();

            updateConfig (form);
            return (null);

        }


        return (super.processMessage(message));

    }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?