package com.coolwen.ant.struts;
import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.OutputStreamWriter; import java.io.Writer; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import java.util.Vector;
import net.sf.hibernate.tool.ddl2hbm.JDBCUtil; import net.sf.hibernate.util.StringHelper;
import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.ResultSetHandler; import org.apache.tools.ant.BuildException; import org.apache.velocity.Template; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.VelocityEngine; import org.apache.velocity.context.Context; import org.apache.velocity.runtime.RuntimeConstants; import org.apache.velocity.texen.Generator;
import com.coolwen.ant.hibernate.HibernateTask;
/** * TODO * * @author <a href="mailto:jody@cpgroup.cn>QiaoLu</a> * @Date:2004-7-29 */ public class ActionTask extends HibernateTask { protected static Map template = new HashMap(); protected File coolwen_struts_outpath; protected String coolwen_struts_schem; protected String coolwen_struts_templatepath; protected File coolwen_struts_documentpath; protected String coolwen_struts_package; public List getColumns(List value){ List result = new ArrayList(); for (Iterator iter = value.iterator(); iter.hasNext(); ){ JDBCUtil.Column column = (JDBCUtil.Column)iter.next(); Column newColumn = new Column(); newColumn.setHibernateType(column.hibernateType); newColumn.setJavaType(column.javaType); newColumn.setName(column.name.toLowerCase()); newColumn.setSqlColumnLength(column.sqlColumnLength); newColumn.setSqlDecimalLength(column.sqlDecimalLength); newColumn.setSqlNotNull(column.sqlNotNull); newColumn.setSqlReadOnly(column.sqlReadOnly); newColumn.setSqlType(column.sqlType); result.add(newColumn); } return result; } /* (non-Javadoc) * @see org.apache.tools.ant.Taskexecute() */ public void execute () throws BuildException { List content = new ArrayList(); for(int i = 0 ; i < this.getCoolwen_hibernate_tables().length ; i++){ try { List pkColumns = JDBCUtil.getPrimaryKeyColumns(this.getDataSource().getConnection(), this.getCoolwen_hibernate_catalog(), this.getCoolwen_hibernate_schem(), this.getCoolwen_hibernate_tables()[i]); Set fkColumns = JDBCUtil.getForeignKeyColumns(this.getDataSource().getConnection(), this.getCoolwen_hibernate_catalog(), this.getCoolwen_hibernate_schem(), this.getCoolwen_hibernate_tables()[i]); List columns = JDBCUtil.getTableColumns(this.getDataSource().getConnection(), this.getCoolwen_hibernate_catalog(), this.getCoolwen_hibernate_schem(), this.getCoolwen_hibernate_tables()[i]); ActionTemplateData actiontemplat = new ActionTemplateData (); actiontemplat.setClassname(this.getCoolwen_hibernate_tables()[i]); actiontemplat.setPackagename(this.getCoolwen_hibernate_package()); actiontemplat.setColumns(this.getColumns(columns)); actiontemplat.setFkColumns(fkColumns); actiontemplat.setPkColumns(this.getColumns(pkColumns)); content.add(actiontemplat); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } try { VelocityEngine ve = new VelocityEngine(); ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, this.getCoolwen_struts_templatepath()); ve.init(); Generator generator = Generator.getInstance(); generator.setVelocityEngine(ve); generator.setOutputPath(this.getCoolwen_struts_templatepath()); Template template = null; Writer writer = null; // TODO start struts config generat Context struts = new VelocityContext(); struts.put("contents",content); struts.put("schem",this.getCoolwen_struts_schem()); struts.put("package",this.getCoolwen_struts_package()); //template = generator.getTemplate("strutsTemplate.vm","GBK"); template = this.getTemplate(generator,"strutsTemplate.vm"); File strutsFile = this.getCoolwen_struts_documentpath(); strutsFile = new File(strutsFile,"WEB-INF"); strutsFile = new File(strutsFile,"struts-config-"+this.getCoolwen_struts_schem()+".xml"); writer = this.getWriter(strutsFile,null); template.merge(struts,writer); writer.flush(); writer.close(); String rootid = this.create("Tree",true,"-1",""); String schemid = this.create(this.getCoolwen_struts_schem().toLowerCase(),true,rootid,""); for (Iterator iter = content.iterator(); iter.hasNext(); ){ Context c = new VelocityContext(); c.put("now", new Date().toString()); ActionTemplateData tempdata = (ActionTemplateData)iter.next(); System.out.println(schemid); this.create(tempdata.classname.toLowerCase(),false,schemid,this.getCoolwen_struts_schem().toLowerCase()+"/"+tempdata.classname.toLowerCase()); c.put("content",tempdata); c.put("schem",this.getCoolwen_struts_schem()); c.put("package",this.getCoolwen_struts_package()+"."+this.getCoolwen_struts_schem()); //# TODO struts Action generat File tempf = this.getCoolwen_struts_outpath(); tempf = new File(tempf,this.getCoolwen_struts_schem()); if(!tempf.exists()) tempf.mkdirs(); tempf = new File(tempf,tempdata.classname+"Actions.java"); writer = this.getWriter(tempf,null); template = this.getTemplate(generator,"actionTemplate.vm"); template.merge(c,writer); writer.flush(); writer.close(); //c = null; //# TODO struts confit generat //# Velocity generat Map templateMap = new HashMap(); templateMap.put("add","addTemplate.vm"); templateMap.put("detail","detailTemplate.vm"); templateMap.put("list","listTemplate.vm"); templateMap.put("print","printTemplate.vm"); templateMap.put("update","updateTemplate.vm"); templateMap.put("index","indexTemplate.vm"); templateMap.put("frameMaster","frameMasterTemplate.vm"); File velocity = this.getCoolwen_struts_documentpath(); velocity = new File(velocity,this.getCoolwen_struts_schem().toLowerCase()+File.separator+tempdata.getClassname().toLowerCase()); if(!velocity.exists()) velocity.mkdirs(); for(Iterator tmap = templateMap.keySet().iterator(); tmap.hasNext();){ template = null; String key = (String)tmap.next(); String value = (String)templateMap.get(key); writer = null; File veloctiytemp = new File(velocity,key+".vm"); writer = this.getWriter(veloctiytemp,null); template = this.getTemplate(generator,value);//generator.getTemplate(value,"GBK"); template.merge(c,writer); writer.flush(); writer.close(); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } protected String makeEntityName(String name) { String tmp = makeMemberName(name); tmp = tmp.substring(0,1).toUpperCase() + tmp.substring(1); return tmp; } protected String makeMemberName(String name) { String memberName = name.toLowerCase(); int i; while ( (i = memberName.indexOf(StringHelper.UNDERSCORE) ) != -1 ) { java.lang.String tmp1 = memberName.substring(0, i); if (i+1 < memberName.length()) { tmp1 += memberName.substring(i+1, i+2).toUpperCase(); } if (i+2 < memberName.length()) { tmp1 += memberName.substring(i+2); } memberName = tmp1; } return memberName; } /** * @return Returns the coolwen_struts_outpath. */ public File getCoolwen_struts_outpath() { return coolwen_struts_outpath; } /** * @param coolwen_struts_outpath The coolwen_struts_outpath to set. */ public void setCoolwen_struts_outpath(File coolwen_struts_outpath) { this.coolwen_struts_outpath = coolwen_struts_outpath; } /** * @return Returns the coolwen_struts_schem. */ public String getCoolwen_struts_schem() { return coolwen_struts_schem; } /** * @param coolwen_struts_schem The coolwen_struts_schem to set. */ public void setCoolwen_struts_schem(String coolwen_struts_schem) { this.coolwen_struts_schem = coolwen_struts_schem; } /** * @return Returns the coolwen_struts_templatepath. */ public String getCoolwen_struts_templatepath() { return coolwen_struts_templatepath; } /** * @param coolwen_struts_templatepath The coolwen_struts_templatepath to set. */ public void setCoolwen_struts_templatepath( String coolwen_struts_templatepath) { this.coolwen_struts_templatepath = coolwen_struts_templatepath; } /** * @return Returns the coolwen_struts_documentpath. */ public File getCoolwen_struts_documentpath() { return coolwen_struts_documentpath; } /** * @param coolwen_struts_documentpath The coolwen_struts_documentpath to set. */ public void setCoolwen_struts_documentpath( File coolwen_struts_documentpath) { this.coolwen_struts_documentpath = coolwen_struts_documentpath; } /** * @return Returns the coolwen_struts_package. */ public String getCoolwen_struts_package() { return coolwen_struts_package; } /** * @param coolwen_struts_package The coolwen_struts_package to set. */ public void setCoolwen_struts_package(String coolwen_struts_package) { this.coolwen_struts_package = coolwen_struts_package; } /** * Returns a writer, based on encoding and path. * * @param path path to the output file * @param encoding output encoding */ public Writer getWriter(File path, String encoding) throws Exception { Writer writer; if (encoding == null || encoding.length() == 0 || encoding.equals("8859-1") || encoding.equals("8859_1")) { writer = new FileWriter(path); } else { writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path), encoding)); } return writer; } public Template getTemplate(Generator generator,String template) throws Exception{ Template temp = null; synchronized(ActionTask.template){ temp = (Template)ActionTask.template.get(template); if(temp == null){ temp = generator.getTemplate(template,"GB2312"); ActionTask.template.put(template,temp); } } return temp; } protected String create(String name,boolean isfloder,String parentid,String link){ QueryRunner queryRunner = new QueryRunner(this.getDataSource()); String id = ""; IsExist isExist = new IsExist(); String sql = ""; if(this.getCoolwen_hibernate_schem()!=null && this.getCoolwen_hibernate_schem().length()>0){ sql = "select * from "+this.getCoolwen_hibernate_schem()+".GEN_MENU where NODENAME = '"+name+"' and ISNODE ='"+isfloder+"'" ; }else{ sql = "select * from GEN_MENU where NODENAME = '"+name+"' and ISNODE ='"+isfloder+"'" ; } String isexist; try { isexist = queryRunner.query(sql, isExist).toString(); } catch (SQLException e) { e.printStackTrace(); return "0"; } if(isexist.equals("0")){ if(this.getCoolwen_hibernate_schem()!=null && this.getCoolwen_hibernate_schem().length()>0){ sql = "insert into "+this.getCoolwen_hibernate_schem()+".GEN_MENU(NODEID,NODENAME,ISNODE,PARENTID,LINK,STATE)values(?,?,?,?,?,?)" ; }else{ sql = "insert into GEN_MENU(NODEID,NODENAME,ISNODE,PARENTID,LINK,STATE)values(?,?,?,?,?,?)" ; } Vector parm = new Vector(); id = new Date().getTime()+""+(int)(Math.random()*99999); parm.add(id); parm.add(name); parm.add(new Boolean(isfloder)); parm.add(parentid); parm.add(link); parm.add(new Boolean(true)); try { queryRunner.update( sql, parm.toArray()); }catch(SQLException e){ e.printStackTrace(); } }else id = isexist; return id; } public class IsExist implements ResultSetHandler { public Object handle(ResultSet rs) throws SQLException { if(rs.next()) return rs.getObject("NODEID"); else return "0"; } } }
|