⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 codegenframeworkfacetselectdelegate.java

📁 Eclipse代码生成器,支持Eclipse3.3 WTP版本,该项目实现了Spring + hibernate + webService + struts 框架文件的自动生成,有这方面需求的个人公司
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.newegg.eclipse.soaw.facet.codegen.ui.command;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.wst.command.internal.env.core.common.StatusUtils;
import org.eclipse.wst.common.environment.IStatusHandler;
import org.eclipse.wst.common.environment.NullStatusHandler;
import org.eclipse.wst.common.project.facet.core.IDelegate;
import org.eclipse.wst.common.project.facet.core.IProjectFacetVersion;

import com.newegg.eclipse.soaw.facet.codegen.ui.CodeGenUIMessages;
import com.newegg.eclipse.soaw.facet.codegen.ui.beans.ClassDataModel;
import com.newegg.eclipse.soaw.facet.codegen.ui.beans.CodeGenFrameworkFacetSelectConfig;
import com.newegg.eclipse.soaw.facet.codegen.ui.command.common.GenFilesCommand;
import com.newegg.eclipse.soaw.facet.codegen.ui.command.common.ImportThirdPartCommand;
import com.newegg.eclipse.soaw.facet.codegen.ui.util.Constants;
import com.newegg.eclipse.soaw.facet.codegen.ui.util.Utils;
import com.newegg.eclipse.soaw.facet.codegen.ui.util.ValidataTool;

public class CodeGenFrameworkFacetSelectDelegate implements IDelegate
{
	private GenFilesCommand genFilesCommand;
	private Map<String, String> classVariables = new HashMap<String, String>();
	private Map<String, String> configVariables = new HashMap<String, String>();

	public void execute(final IProject pj, final IProjectFacetVersion fv, final Object config,
			final IProgressMonitor monitor) throws CoreException
	{
		IStatusHandler handler = new NullStatusHandler();
		try
		{
			final CodeGenFrameworkFacetSelectConfig cfg = (CodeGenFrameworkFacetSelectConfig) config;
			monitor.beginTask("Copying jars from third part plug in...", 9);
			copyThirdPartJars(pj, cfg);
			monitor.worked(1);

			genFilesCommand = new GenFilesCommand(pj, monitor, handler);
			initAllClassName(cfg);
			monitor.setTaskName("Starting generate factory class...");
			genFactoryClass();
			monitor.worked(1);

			monitor.setTaskName("Starting generate exception classes...");
			genExceptionClasses();
			monitor.worked(1);

			monitor.setTaskName("Starting generate timeLogger class...");
			genTimeLoggerClass();
			monitor.worked(1);

			monitor.setTaskName("Starting generate dao classes...");
			genDaoClasses();
			monitor.worked(1);

			monitor.setTaskName("Starting generate service classes...");
			genServiceClasses();
			monitor.worked(1);

			monitor.setTaskName("Starting generate web service class...");
			genWebServiceClasses();
			monitor.worked(1);

			monitor.setTaskName("Starting generate spring config files...");
			genSpringConfigXml(pj);
			monitor.worked(1);

			monitor.setTaskName("Starting generate properties config files...");
			genPropertiesConfigFiles();
			monitor.worked(1);
			
			pj.refreshLocal(IResource.DEPTH_INFINITE, null);
		}
		// catch (CoreException e)
		// {
		// IStatus status = StatusUtils.errorStatus("Auto generate bussiness framework failed.\n",
		// e);
		// handler.reportError(status);
		// }
		finally
		{
			monitor.done();
		}
	}

	/**
	 * Initialization fields.
	 * 
	 * @param cfg
	 */
	private void initAllClassName(final CodeGenFrameworkFacetSelectConfig cfg)
	{
		classVariables.put(Constants.BASE_PACKAGE_NAME, cfg.getBasePackageName());

		String basePackage = Utils.getValidPackage(cfg.getBasePackageName(), "");

		classVariables.put(Constants.PACKAGE_DAO_NAME, basePackage + CodeGenUIMessages.PACKAGE_DAO_NAME);
		classVariables.put(Constants.PACKAGE_DAO_IMPL_NAME, basePackage + CodeGenUIMessages.PACKAGE_DAO_IMPL_NAME);
		classVariables.put(Constants.PACKAGE_SERVICE_NAME, basePackage + CodeGenUIMessages.PACKAGE_SERVICE_NAME);
		classVariables.put(Constants.PACKAGE_SERVICE_IMPL_NAME, basePackage
				+ CodeGenUIMessages.PACKAGE_SERVICE_IMPL_NAME);
		classVariables.put(Constants.PACKAGE_EXCEPTION_NAME, basePackage + CodeGenUIMessages.PACKAGE_EXCEPTION_NAME);
		classVariables.put(Constants.PACKAGE_MODEL_NAME, basePackage + CodeGenUIMessages.PACKAGE_MODEL_NAME);
		classVariables.put(Constants.PACKAGE_UTIL_NAME, basePackage + CodeGenUIMessages.PACKAGE_UTIL_NAME);
		classVariables.put(Constants.PACKAGE_WEBSERVICE_NAME, basePackage + CodeGenUIMessages.PACKAGE_WEBSERVICE_NAME);

		classVariables.put(Constants.CLASS_FACTORY_NAME, CodeGenUIMessages.CLASS_FACTORY_NAME);
		classVariables.put(Constants.CLASS_TIMELOGGER_NAME, CodeGenUIMessages.CLASS_TIMELOGGER_NAME);
		classVariables.put(Constants.CLASS_BUSINESS_EXCEPTION_NAME, CodeGenUIMessages.CLASS_BUSINESS_EXCEPTION_NAME);
		classVariables.put(Constants.CLASS_SYSTEM_EXCEPTION_NAME, CodeGenUIMessages.CLASS_SYSTEM_EXCEPTION_NAME);
		classVariables.put(Constants.CLASS_MODEL_SAMPLE_NAME, CodeGenUIMessages.CLASS_MODEL_SAMPLE_NAME);
		classVariables.put(Constants.CLASS_ISAMPLE_DAO_NAME, CodeGenUIMessages.CLASS_ISAMPLE_DAO_NAME);
		classVariables.put(Constants.CLASS_SAMPLE_DAO_IMPL_NAME, CodeGenUIMessages.CLASS_SAMPLE_DAO_IMPL_NAME);
		classVariables.put(Constants.CLASS_ISAMPLE_SERVICE_NAME, CodeGenUIMessages.CLASS_ISAMPLE_SERVICE_NAME);
		classVariables.put(Constants.CLASS_SAMPLE_SERVICE_IMPL_NAME, CodeGenUIMessages.CLASS_SAMPLE_SERVICE_IMPL_NAME);
		classVariables.put(Constants.CLASS_SAMPLE_WEB_SERVICE_NAME, CodeGenUIMessages.CLASS_SAMPLE_WEB_SERVICE_NAME);

		configVariables.put(Constants.FOLDER_CONFIG_NAME, CodeGenUIMessages.FOLDER_CONFIG_NAME);
		configVariables.put(Constants.FOLDER_CONFIG_SPRING_NAME, CodeGenUIMessages.FOLDER_CONFIG_SPRING_NAME);

		configVariables.put(Constants.CONFIG_SPRING_APPLICATIONCONTEXT_DB_NAME,
				CodeGenUIMessages.CONFIG_SPRING_APPLICATIONCONTEXT_DB_NAME);
		configVariables.put(Constants.CONFIG_SPRING_APPLICATIONCONTEXT_DAO_NAME,
				CodeGenUIMessages.CONFIG_SPRING_APPLICATIONCONTEXT_DAO_NAME);
		configVariables.put(Constants.CONFIG_SPRING_APPLICATIONCONTEXT_SERVICE_NAME,
				CodeGenUIMessages.CONFIG_SPRING_APPLICATIONCONTEXT_SERVICE_NAME);
		configVariables.put(Constants.CONFIG_PROPERTIES_DATABASE_NAME,
				CodeGenUIMessages.CONFIG_PROPERTIES_DATABASE_NAME);
		configVariables.put(Constants.CONFIG_PROPERTIES_LOG4J_NAME, CodeGenUIMessages.CONFIG_PROPERTIES_LOG4J_NAME);
		configVariables.put(Constants.CONFIG_PROPERTIES_SPY_NAME, CodeGenUIMessages.CONFIG_PROPERTIES_SPY_NAME);

		configVariables.put(Constants.SPRING_CONFIG_NAME, Utils
				.getPackageRelativePath(CodeGenUIMessages.FOLDER_CONFIG_SPRING_NAME)
				+ Constants.FILE_SPERATOR
				+ CodeGenUIMessages.CONFIG_SPRING_APPLICATIONCONTEXT_ALL_NAME
				+ Constants.DEFAULT_XML_FILE_SUFFIX);

		configVariables.put(Constants.CONFIG_PROPERTIES_DATABASE_JDBC_DRIVERNAME, cfg.getDb_driverClassName());
		String databaseDriverDialect = Utils.getDialectNameByKey(cfg.getDatabaseType());
		configVariables.put(Constants.CONFIG_PROPERTIES_HIBERNATE_JDBC_DRIVER_DIALECT, databaseDriverDialect);
		configVariables.put(Constants.CONFIG_PROPERTIES_DATABASE_JDBC_URL, cfg.getDb_url());
		configVariables.put(Constants.CONFIG_PROPERTIES_DATABASE_JDBC_USERNAME, cfg.getDb_userName());
		configVariables.put(Constants.CONFIG_PROPERTIES_DATABASE_JDBC_PASSWORD, cfg.getDb_password());
	}

	/**
	 * Generate properties config files.
	 * 
	 * @throws CoreException
	 */
	private void genPropertiesConfigFiles() throws CoreException
	{
		String currentTemplateName = Constants.TEMPLATE_PROPERTIES_DATABASE_NAME;
		Map<String, String> dependClasses = new HashMap<String, String>();
		dependClasses.put("jdbc_driverClassName", configVariables
				.get(Constants.CONFIG_PROPERTIES_DATABASE_JDBC_DRIVERNAME));
		dependClasses.put("jdbc_url", configVariables.get(Constants.CONFIG_PROPERTIES_DATABASE_JDBC_URL));
		dependClasses.put("jdbc_username", configVariables.get(Constants.CONFIG_PROPERTIES_DATABASE_JDBC_USERNAME));
		dependClasses.put("jdbc_password", configVariables.get(Constants.CONFIG_PROPERTIES_DATABASE_JDBC_PASSWORD));
		ClassDataModel classDataModel = new ClassDataModel(Constants.DEFAULT_PROPERTIES_FILE_SUFFIX, dependClasses,
				configVariables.get(Constants.CONFIG_PROPERTIES_DATABASE_NAME), "");
		executeGenCodeGen(classDataModel, currentTemplateName);

		currentTemplateName = Constants.TEMPLATE_PROPERTIES_LOG4J_NAME;
		classDataModel = new ClassDataModel(Constants.DEFAULT_PROPERTIES_FILE_SUFFIX, null, configVariables
				.get(Constants.CONFIG_PROPERTIES_LOG4J_NAME), "");
		executeGenCodeGen(classDataModel, currentTemplateName);

		currentTemplateName = Constants.TEMPLATE_PROPERTIES_SPY_NAME;
		dependClasses = new HashMap<String, String>();
		dependClasses.put("databaseDriverName", configVariables
				.get(Constants.CONFIG_PROPERTIES_DATABASE_JDBC_DRIVERNAME));
		classDataModel = new ClassDataModel(Constants.DEFAULT_PROPERTIES_FILE_SUFFIX, dependClasses, configVariables
				.get(Constants.CONFIG_PROPERTIES_SPY_NAME), "");
		executeGenCodeGen(classDataModel, currentTemplateName);
	}

	/**
	 * Generate spring config files.
	 * 
	 * @param pj
	 * @throws CoreException
	 */
	private void genSpringConfigXml(final IProject pj) throws CoreException
	{
		String currentTemplateName = Constants.TEMPLATE_SPRING_APPLICATIONCONTEXT_DAO_NAME;
		Map<String, String> dependClasses = new HashMap<String, String>();
		dependClasses.put("ISampleDaoName", classVariables.get(Constants.CLASS_ISAMPLE_DAO_NAME));
		dependClasses.put("SampleDaoImplPackage", classVariables.get(Constants.PACKAGE_DAO_IMPL_NAME));
		dependClasses.put("SampleDaoImplName", classVariables.get(Constants.CLASS_SAMPLE_DAO_IMPL_NAME));
		ClassDataModel classDataModel = new ClassDataModel(Constants.DEFAULT_XML_FILE_SUFFIX, dependClasses,
				configVariables.get(Constants.CONFIG_SPRING_APPLICATIONCONTEXT_DAO_NAME), configVariables
						.get(Constants.FOLDER_CONFIG_SPRING_NAME));
		executeGenCodeGen(classDataModel, currentTemplateName);

		currentTemplateName = Constants.TEMPLATE_SPRING_APPLICATIONCONTEXT_SERVICE_NAME;
		dependClasses = new HashMap<String, String>();
		dependClasses.put("ISampleServiceName", classVariables.get(Constants.CLASS_ISAMPLE_SERVICE_NAME));
		dependClasses.put("SampleServiceImplPackage", classVariables.get(Constants.PACKAGE_SERVICE_IMPL_NAME));
		dependClasses.put("SampleServiceImplName", classVariables.get(Constants.CLASS_SAMPLE_SERVICE_IMPL_NAME));
		classDataModel = new ClassDataModel(Constants.DEFAULT_XML_FILE_SUFFIX, dependClasses, configVariables
				.get(Constants.CONFIG_SPRING_APPLICATIONCONTEXT_SERVICE_NAME), configVariables
				.get(Constants.FOLDER_CONFIG_SPRING_NAME));
		executeGenCodeGen(classDataModel, currentTemplateName);

		currentTemplateName = Constants.TEMPLATE_SPRING_APPLICATIONCONTEXT_DB_NAME;
		dependClasses = new HashMap<String, String>();
		dependClasses.put("description", pj.getName() + " system");
		dependClasses.put("databaseConfig", configVariables.get(Constants.CONFIG_PROPERTIES_DATABASE_NAME)
				+ Constants.DEFAULT_PROPERTIES_FILE_SUFFIX);

⌨️ 快捷键说明

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