PageRenderTime 207ms CodeModel.GetById 61ms app.highlight 111ms RepoModel.GetById 20ms app.codeStats 1ms

/jeecg-framework/src/main/java/test/JeecgInitDB.java

https://github.com/nic9805/jeecg
Java | 336 lines | 255 code | 27 blank | 54 comment | 2 complexity | 26f0257b9da1b14cac9c8763e499d4d8 MD5 | raw file
  1package test;
  2
  3import java.io.File;
  4import java.io.FileOutputStream;
  5import java.io.IOException;
  6import java.io.OutputStreamWriter;
  7import java.io.Writer;
  8import java.sql.Connection;
  9import java.sql.DriverManager;
 10import java.sql.ResultSet;
 11import java.sql.SQLException;
 12import java.sql.Statement;
 13import java.util.ArrayList;
 14import java.util.HashMap;
 15import java.util.List;
 16import java.util.Map;
 17
 18import org.jeecgframework.web.cgform.entity.config.CgFormFieldEntity;
 19import org.jeecgframework.web.cgform.entity.config.CgFormHeadEntity;
 20import org.jeecgframework.web.system.pojo.base.TSAttachment;
 21import org.jeecgframework.web.system.pojo.base.TSBaseUser;
 22import org.jeecgframework.web.system.pojo.base.TSDepart;
 23import org.jeecgframework.web.system.pojo.base.TSFunction;
 24import org.jeecgframework.web.system.pojo.base.TSLog;
 25import org.jeecgframework.web.system.pojo.base.TSRole;
 26import org.jeecgframework.web.system.pojo.base.TSType;
 27import org.jeecgframework.web.system.pojo.base.TSTypegroup;
 28import org.jeecgframework.web.system.pojo.base.TSUser;
 29import freemarker.template.Configuration;
 30import freemarker.template.DefaultObjectWrapper;
 31import freemarker.template.Template;
 32
 33
 34/** 
 35 * @Description 
 36 * @ClassName: JeecgInitDB
 37 * @author tanghan
 38 * @date 2013-7-19 下午04:24:51  
 39 */
 40
 41public class JeecgInitDB {
 42   
 43    private static Connection con=null;
 44    
 45    
 46	public static Connection getConnection() throws ClassNotFoundException, SQLException{
 47		if(con == null){
 48			Class.forName("com.mysql.jdbc.Driver");
 49            con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jeecg", "root","root");
 50		}
 51		return con;
 52	}
 53	
 54	public static void main(String[] args) throws Exception {
 55		Configuration cfg = new Configuration();
 56		String sql1="select * from t_s_attachment";
 57		String sql2="select * from t_s_base_user";
 58		String sql3="select * from t_s_depart";
 59		String sql4="select * from t_s_role";
 60		String sql5="select * from t_s_user";
 61		String sql6="select * from t_s_typegroup";
 62		String sql7 = "select * from t_s_function";
 63		String sql8 = "select * from t_s_type";
 64		String sql9 = "select * from t_s_log limit 100";
 65		String sql10 = "select * from cgform_field where table_id =";  //此处由于需要更具cgform_head生成,故只能单个生成
 66		String sql11 = "select * from cgform_head ";
 67        Statement st=null;
 68        ResultSet rs=null;
 69		try {
 70			cfg.setDirectoryForTemplateLoading(new File("E:/Workspace-jeecg/jeecg-v3-simple-new/src/test"));
 71			cfg.setObjectWrapper(new DefaultObjectWrapper());
 72			Template temp = cfg.getTemplate("init.ftl","UTF-8");
 73            con=getConnection();
 74            st=con.createStatement();
 75            rs=st.executeQuery(sql1);
 76            int i=1;
 77            Map<String, Object> root = new HashMap<String, Object>();
 78            List att = new ArrayList();
 79            while(rs.next())
 80            {
 81                 TSAttachment tsAttachment = new TSAttachment();
 82            	 tsAttachment.setId(i+"");
 83            	 tsAttachment.setAttachmenttitle(rs.getString("attachmenttitle"));
 84            	 tsAttachment.setRealpath(rs.getString("attachmenttitle"));
 85            	 tsAttachment.setSwfpath(rs.getString("swfpath"));
 86            	 tsAttachment.setExtend(rs.getString("extend"));
 87            	 att.add(tsAttachment);
 88                 i++;
 89            }
 90            root.put("animals", att);
 91            
 92            
 93            rs=st.executeQuery(sql2);
 94            i=1;
 95            List user = new ArrayList();
 96            while(rs.next())
 97            {
 98                 TSBaseUser baseUser = new TSBaseUser();
 99                 baseUser.setId(i+"");
100                 baseUser.setUserKey(rs.getString("userkey"));
101                 baseUser.setStatus(rs.getShort("status"));
102                 baseUser.setRealName(rs.getString("realname"));
103                 baseUser.setUserName(rs.getString("username"));
104                 baseUser.setPassword(rs.getString("password"));
105                 baseUser.setActivitiSync(rs.getShort("activitisync"));
106                 user.add(baseUser);
107                 i++;
108            }
109            root.put("baseuser", user);
110            
111            
112            rs=st.executeQuery(sql3);
113            List dep = new ArrayList();
114            i=1;
115            while(rs.next())
116            {
117                 TSDepart tsDepart = new TSDepart();
118                 tsDepart.setId(i+"");
119                 tsDepart.setDepartname(rs.getString("departname"));
120                 tsDepart.setDescription(rs.getString("description"));
121                 dep.add(tsDepart);
122                 i++;
123            }
124            root.put("depart", dep);    
125            
126            
127            rs=st.executeQuery(sql4);
128            List role = new ArrayList();
129            i=1;
130            while(rs.next())
131            {
132            	org.jeecgframework.core.util.LogUtil.info(rs.getString("rolename"));
133                 TSRole tsRole = new TSRole();
134                 tsRole.setId(i+"");
135                 tsRole.setRoleName(rs.getString("rolename"));
136                 tsRole.setRoleCode(rs.getString("rolecode"));
137                 role.add(tsRole);
138                 i++;
139            }
140            root.put("role", role);
141            
142            rs=st.executeQuery(sql5);
143            List susers = new ArrayList();
144            i=1;
145            while(rs.next())
146            {
147                 TSUser suer = new TSUser();
148                 suer.setId(i+"");
149//                 org.jeecgframework.core.util.LogUtil.info(rs.getString("signaturefile"));
150//                 suer.setSignatureFile(rs.getString("signaturefile"));
151                 suer.setMobilePhone(rs.getString("mobilephone"));
152                 suer.setOfficePhone(rs.getString("officephone"));
153                 suer.setEmail(rs.getString("email"));
154                 susers.add(suer);
155                 i++;
156            }
157            root.put("suser", susers);
158            
159            rs=st.executeQuery(sql6);
160            List typegroup = new ArrayList();
161            i=1;
162            while(rs.next())
163            {
164                 TSTypegroup tsTypegroup = new TSTypegroup();
165                 tsTypegroup.setId(i+"");
166//                 org.jeecgframework.core.util.LogUtil.info(rs.getString("signaturefile"));
167//                 suer.setSignatureFile(rs.getString("signaturefile"));
168                 tsTypegroup.setTypegroupname(rs.getString("typegroupname"));
169                 tsTypegroup.setTypegroupcode(rs.getString("typegroupcode"));
170                 typegroup.add(tsTypegroup);
171                 i++;
172            }
173            root.put("typegroup", typegroup);
174            
175            rs=st.executeQuery(sql7);
176            List function = new ArrayList();
177            i=1;
178            while(rs.next())
179            {
180                 TSFunction tsFunction = new TSFunction();
181                 tsFunction.setId(i+"");
182                 tsFunction.setFunctionName(rs.getString("functionName"));
183                 tsFunction.setFunctionUrl(rs.getString("functionUrl"));
184                 tsFunction.setFunctionLevel(rs.getShort("functionLevel"));
185                 tsFunction.setFunctionOrder(rs.getString("functionOrder"));
186                 function.add(tsFunction);
187                 i++;
188            }
189            root.put("menu", function);
190            
191            rs=st.executeQuery(sql8);
192            List type = new ArrayList();
193            i=1;
194            while(rs.next())
195            {
196                 TSType tsType = new TSType();
197                 tsType.setId(i+"");
198//                 org.jeecgframework.core.util.LogUtil.info(rs.getString("signaturefile"));
199//                 suer.setSignatureFile(rs.getString("signaturefile"));
200                 tsType.setTypename(rs.getString("typename"));
201                 tsType.setTypecode(rs.getString("typecode"));
202                 type.add(tsType);
203                 i++;
204            }
205            root.put("type", type);
206            
207            rs=st.executeQuery(sql9);
208            List log = new ArrayList();
209            i=1;
210            while(rs.next())
211            {
212                TSLog slog = new TSLog();
213                slog.setId(i+"");
214//                 org.jeecgframework.core.util.LogUtil.info(rs.getString("signaturefile"));
215//                 suer.setSignatureFile(rs.getString("signaturefile"));
216//                 tsType.setTypename(rs.getString("typename"));
217//                 tsType.setTypecode(rs.getString("typecode"));
218//                 type.add(tsType);
219                   slog.setId(i+"");
220//                   org.jeecgframework.core.util.LogUtil.info(rs.getString("logcontent"));
221                   slog.setLogcontent(rs.getString("logcontent"));
222                   slog.setLoglevel(rs.getShort("loglevel"));
223                   slog.setBroswer(rs.getString("broswer"));
224                   slog.setNote(rs.getString("note"));
225                   slog.setOperatetime(rs.getTimestamp("operatetime"));
226                   slog.setOperatetype(rs.getShort("operatetype"));
227                   log.add(slog);
228                 i++;
229            }
230            root.put("log", log);
231            
232            rs=st.executeQuery(sql11);
233            List cghead = new ArrayList();
234            i=1;
235            while(rs.next())
236            {
237                CgFormHeadEntity head = new CgFormHeadEntity();
238                head.setId(i+"");
239                head.setTableName(rs.getString("table_name"));
240                head.setIsTree(rs.getString("is_tree"));
241                head.setIsPagination(rs.getString("is_pagination"));
242                head.setQuerymode(rs.getString("queryMode"));
243                head.setIsCheckbox(rs.getString("is_checkbox"));
244                head.setIsDbSynch(rs.getString("is_dbsynch"));
245                head.setContent(rs.getString("content"));
246                head.setJformVersion(rs.getString("JFORM_VERSION"));
247                head.setJformType(rs.getInt("jform_type"));
248                head.setColumns(getCgFormItem(sql10, rs.getString("id")));
249                cghead.add(head);
250                i++;
251            }
252            root.put("cghead", cghead);
253//            rs=st.executeQuery(sql10);
254//            List cgfield = new ArrayList();
255//            i=1;
256//            while(rs.next())
257//            {
258//            	CgFormFieldEntity filed = new CgFormFieldEntity();
259//                filed.setFieldName(rs.getString("field_name"));
260//                filed.setLength(rs.getInt("length"));
261//                filed.setType(rs.getString("type"));
262//                filed.setPointLength(rs.getInt("point_length"));
263//                filed.setIsNull(rs.getString("is_null"));
264//                filed.setIsKey(rs.getString("is_key"));
265//                filed.setIsQuery(rs.getString("is_query"));
266//                filed.setIsShow(rs.getString("is_show"));
267//                filed.setShowType(rs.getString("show_type"));
268//                filed.setOrderNum(rs.getInt("order_num"));
269//                filed.setFieldHref(rs.getString("field_href"));
270//                filed.setFieldLength(rs.getInt("field_length"));
271//                filed.setFieldValidType(rs.getString("field_valid_type"));
272//                filed.setQueryMode(rs.getString("query_mode"));
273//                filed.setContent(rs.getString("content"));
274//                filed.setDictTable(rs.getString("dict_table"));
275//                filed.setDictField(rs.getString("dict_field"));
276//                filed.setMainField(rs.getString("main_field"));
277//                filed.setMainTable(rs.getString("main_table"));
278//                cgfield.add(filed);
279//                i++;
280//            }
281//            root.put("cgfield", cgfield);
282            
283            
284            Writer out = new OutputStreamWriter(new FileOutputStream("RepairServiceImpl.java"), "UTF-8");
285            temp.process(root, out);
286            out.flush();
287            out.close();
288            org.jeecgframework.core.util.LogUtil.info("Successfull................");
289		} catch (IOException e) {
290			e.printStackTrace();
291		}
292	}
293	
294	
295	
296	/**
297	 * 获取表单字段方法
298	 * @param sql10
299	 * @param cgformhead_id
300	 * @return
301	 * @throws Exception
302	 */
303	public static List getCgFormItem(String sql10,String cgformhead_id) throws Exception{
304		Statement st =con.createStatement();
305		ResultSet rs = st.executeQuery(sql10+"'"+cgformhead_id.trim()+"'");
306        List cgfield = new ArrayList();
307        int i=1;
308        while(rs.next())
309        {
310        	CgFormFieldEntity filed = new CgFormFieldEntity();
311            filed.setFieldName(rs.getString("field_name"));
312            filed.setLength(rs.getInt("length"));
313            filed.setType(rs.getString("type"));
314            filed.setPointLength(rs.getInt("point_length"));
315            filed.setIsNull(rs.getString("is_null"));
316            filed.setIsKey(rs.getString("is_key"));
317            filed.setIsQuery(rs.getString("is_query"));
318            filed.setIsShow(rs.getString("is_show"));
319            filed.setShowType(rs.getString("show_type"));
320            filed.setOrderNum(rs.getInt("order_num"));
321            filed.setFieldHref(rs.getString("field_href"));
322            filed.setFieldLength(rs.getInt("field_length"));
323            filed.setFieldValidType(rs.getString("field_valid_type"));
324            filed.setQueryMode(rs.getString("query_mode"));
325            filed.setContent(rs.getString("content"));
326            filed.setDictTable(rs.getString("dict_table"));
327            filed.setDictField(rs.getString("dict_field"));
328            filed.setMainField(rs.getString("main_field"));
329            filed.setMainTable(rs.getString("main_table"));
330            cgfield.add(filed);
331            i++;
332        }
333        return cgfield;
334	}
335	
336}