PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/v2_0_3/generator/src/org/collazos/generator/GenerateUtil.java

#
Java | 448 lines | 395 code | 25 blank | 28 comment | 20 complexity | 5d533b668048b2f3e13dc7954a78ce5d MD5 | raw file
Possible License(s): LGPL-2.1
  1. /*
  2. * generator Database Bean Generator
  3. * Copyright (C) 2005 Josep Carles Collazos
  4. *
  5. * This software is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This software is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * Josep Carles Collazos
  20. * mailto: jespo@users.sourceforge.net
  21. *
  22. */
  23. package org.collazos.generator;
  24. import java.io.BufferedWriter;
  25. import java.io.File;
  26. import java.io.FileWriter;
  27. import java.io.IOException;
  28. import java.sql.Connection;
  29. import java.sql.ResultSet;
  30. import java.sql.SQLException;
  31. import java.sql.Statement;
  32. import java.util.Properties;
  33. import org.collazos.generator.bdexception.BaseRuntimeException;
  34. import org.collazos.util.database.ConnectDB;
  35. import electric.xml.Element;
  36. /**
  37. * Type description
  38. *
  39. * @author Josep Carles Collazos
  40. * @created 11-mar-2005
  41. */
  42. public class GenerateUtil {
  43. private Properties props;
  44. private boolean valueAsAttribute = false;
  45. private boolean stringsAsCdata = false;
  46. private String basedir;
  47. private String packageName;
  48. private boolean tofile;
  49. private boolean toStdout;
  50. private BufferedWriter output;
  51. private String connectionOpen;
  52. private String connectionClose;
  53. private String connectionPackage;
  54. private String exceptionPackage;
  55. private String xmlImport;
  56. private boolean isEnabled;
  57. private String xmlRowName;
  58. private String xmlFieldName;
  59. private String xmlRowidName;
  60. private String xmlValueName;
  61. private boolean isOracle;
  62. private boolean hasLogs;
  63. public GenerateUtil(Properties props) {
  64. this.props = props;
  65. this.basedir = (String) props.get("generator.io.basedir");
  66. this.packageName = (String) props.get("generator.class.package");
  67. this.tofile = new Boolean((String) props.get("generator.io.file")).booleanValue();
  68. this.toStdout = new Boolean((String) props.get("generator.io.stdout")).booleanValue();
  69. this.connectionOpen = (String) props.get("generator.database.connection.open");
  70. this.connectionClose = (String) props.get("generator.database.connection.close");
  71. this.exceptionPackage = (String) props.get("generator.database.exception.package");
  72. this.connectionPackage = (String) props.get("generator.database.connection.package");
  73. this.xmlImport = (String) props.get("generator.xml.import");
  74. this.isEnabled = new Boolean((String) props.get("generator.static.enabled")).booleanValue();
  75. this.xmlRowName = (String) props.getProperty("generator.static.row.name","row");
  76. this.xmlFieldName = (String) props.getProperty("generator.static.field.name","field");
  77. this.xmlRowidName = (String) props.getProperty("generator.static.rowid.name","rowid");
  78. this.xmlValueName = (String) props.getProperty("generator.static.value.name","value");
  79. this.valueAsAttribute = new Boolean((String) props.get("generator.static.value.attribute")).booleanValue();
  80. this.stringsAsCdata = new Boolean((String) props.get("generator.static.strings.cdata")).booleanValue();
  81. this.isOracle = ((String) props.get("generator.database.driver")).indexOf("oracle")!=-1;
  82. this.hasLogs = new Boolean((String) props.get("generator.log.enabled")).booleanValue();
  83. }
  84. public void out(String text) throws IOException{
  85. if (this.toStdout)
  86. System.out.println(text);
  87. if (this.tofile)
  88. output.write(text+"\n");
  89. }
  90. public static Element executeQueryToXml(String sql) {
  91. Connection con = null;
  92. Statement stm = null;
  93. ResultSet rs = null;
  94. int k=0;
  95. try {
  96. con = ConnectDB.getSimpleConnection();
  97. stm = con.createStatement();
  98. rs = stm.executeQuery(sql);
  99. Element queryResult = new Element("query-result");
  100. int numColumns = rs.getMetaData().getColumnCount();
  101. String[] fieldName = new String[numColumns];
  102. String[] fieldType = new String[numColumns];
  103. for (int i=0;i<numColumns;i++) {
  104. fieldName[i]=rs.getMetaData().getColumnName(i+1);
  105. int numType = rs.getMetaData().getColumnType(i+1);
  106. ColumnData cd = new ColumnData(fieldName[i],numType,1);
  107. fieldType[i]=cd.getJavaType();
  108. }
  109. while (rs.next()) {
  110. Element tmpElem = new Element("row");
  111. for (int i=0;i<numColumns;i++) {
  112. String fieldValue=rs.getString(fieldName[i]);
  113. Element fieldElem = new Element("field");
  114. fieldElem.setAttribute("name",fieldName[i]);
  115. fieldElem.setAttribute("java-type",fieldType[i]);
  116. fieldElem.setText(rs.getString(fieldName[i]));
  117. tmpElem.addChild(fieldElem);
  118. }
  119. tmpElem.setAttribute("rowid",""+k);
  120. queryResult.addChild(tmpElem);
  121. k++;
  122. }
  123. return queryResult;
  124. } catch (Exception e) {
  125. throw new BaseRuntimeException(e.toString());
  126. } finally {
  127. if (rs != null) try { rs.close(); } catch (Exception ignored) {}
  128. if (stm != null) try { stm.close(); } catch (Exception ignored) {}
  129. if (con != null) ConnectDB.putConnection(con);
  130. }
  131. }
  132. public void writeHeader() throws IOException {
  133. out("//Generation Date: "+new java.util.Date());
  134. out("package "+packageName+";");
  135. out("");
  136. out("import java.sql.*;");
  137. out("import "+this.connectionPackage+";");
  138. out("import "+this.exceptionPackage+".*;");
  139. out("import "+this.xmlImport+";");
  140. out("");
  141. out("/**");
  142. out(" * BdUtils");
  143. out(" * Generator version: "+GenerateSkel.version);
  144. out(" * Class to provide some useful stuff.");
  145. out(" * Generation Date: "+new java.util.Date());
  146. out(" * ");
  147. out(" */");
  148. out("public class BdUtils {");
  149. out("");
  150. if (hasLogs)
  151. out(" private static ISimpleLogger logger = new Logger(System.out);");
  152. out("");
  153. }
  154. public void writeConstructor() throws IOException {
  155. out(" public BdUtils() {");
  156. out(" }");
  157. }
  158. public void writeExecuteQuery() throws IOException {
  159. out(" public static Element executeQueryToXml(String sql) {");
  160. out(" Connection con = null;");
  161. out(" Statement stm = null;");
  162. out(" ResultSet rs = null;");
  163. out(" int k=0;");
  164. out(" try {");
  165. out(" con = "+this.connectionOpen+";");
  166. if (this.hasLogs)
  167. out(" logger.log(\"[executeQueryToXml] SQL:\"+sql);");
  168. out(" stm = con.createStatement();");
  169. out(" rs = stm.executeQuery(sql);");
  170. out(" Element queryResult = new Element(\"query-result\");");
  171. out(" int numColumns = rs.getMetaData().getColumnCount();");
  172. out(" String[] fieldName = new String[numColumns];");
  173. out(" String[] fieldType = new String[numColumns];");
  174. out(" for (int i=0;i<numColumns;i++) {");
  175. out(" fieldName[i]=rs.getMetaData().getColumnName(i+1);");
  176. out(" int numType = rs.getMetaData().getColumnType(i+1);");
  177. out(" ColumnData cd = new ColumnData(fieldName[i],numType,1);");
  178. out(" fieldType[i]=cd.getJavaType();");
  179. out(" }");
  180. out(" while (rs.next()) {");
  181. out(" Element tmpElem = new Element(\""+this.xmlRowName+"\");");
  182. out(" for (int i=0;i<numColumns;i++) {");
  183. out(" String fieldValue=rs.getString(fieldName[i]);");
  184. out(" Element fieldElem = new Element(\""+this.xmlFieldName+"\");");
  185. out(" fieldElem.setAttribute(\"name\",fieldName[i]);");
  186. out(" fieldElem.setAttribute(\"java-type\",fieldType[i]);");
  187. if (valueAsAttribute)
  188. out(" fieldElem.setAttribute(\""+this.xmlValueName+"\",rs.getString(fieldName[i]));");
  189. else {
  190. if (stringsAsCdata) {
  191. out(" if (fieldType[i].equals(\"String\")) {");
  192. out(" CData txtCdata = new CData(rs.getString(fieldName[i]));");
  193. out(" fieldElem.setText(txtCdata);");
  194. out(" } else {");
  195. out(" fieldElem.setText(rs.getString(fieldName[i]));");
  196. out(" }");
  197. }
  198. else
  199. out(" fieldElem.setText(rs.getString(fieldName[i]));");
  200. }
  201. out(" tmpElem.addChild(fieldElem);");
  202. out(" }");
  203. out(" tmpElem.setAttribute(\""+this.xmlRowidName+"\",\"\"+k);");
  204. out(" queryResult.addChild(tmpElem);");
  205. out(" k++;");
  206. out(" }");
  207. out(" return queryResult;");
  208. out(" } catch (Exception e) {");
  209. out(" throw new BaseRuntimeException(e.toString());");
  210. out(" } finally {");
  211. out(" if (rs != null) try { rs.close(); } catch (Exception ignored) {}");
  212. out(" if (stm != null) try { stm.close(); } catch (Exception ignored) {}");
  213. out(" if (con != null) "+this.connectionClose+";");
  214. out(" }");
  215. out(" }");
  216. }
  217. public void writeReplaceString() throws IOException {
  218. out(" public static String replaceString(String orig, String tag, String substitute) {");
  219. out(" int posIni = 0;");
  220. out(" int posFin = 0;");
  221. out(" String result = orig;");
  222. out(" boolean go = true;");
  223. out(" while (go) {");
  224. out(" posIni = orig.indexOf(tag, posFin);");
  225. out(" posFin = posIni+tag.length();");
  226. out(" if (posIni == -1)");
  227. out(" break;");
  228. out(" result = result.substring(0,posIni)+substitute+result.substring(posFin);");
  229. out(" }");
  230. out(" return result;");
  231. out(" }");
  232. }
  233. public void writeGetNextSequenceValue() throws IOException {
  234. out(" public static int getNextSequenceValue(String sequenceName,Connection con) {");
  235. out(" String sql=\"select \"+sequenceName+\".nextval from dual\";");
  236. out(" Statement stm = null;");
  237. out(" ResultSet rs = null;");
  238. out(" int k=0;");
  239. out(" try {");
  240. if (this.hasLogs)
  241. out(" logger.log(\"[getNextSequenceValue] SQL:\"+sql);");
  242. out(" stm = con.createStatement();");
  243. out(" rs = stm.executeQuery(sql);");
  244. out(" rs.next();");
  245. out(" int id = rs.getInt(1);");
  246. out(" return id;");
  247. out(" } catch (Exception e) {");
  248. out(" throw new BaseRuntimeException(e.toString());");
  249. out(" } finally {");
  250. out(" if (rs != null) try { rs.close(); } catch (Exception ignored) {}");
  251. out(" if (stm != null) try { stm.close(); } catch (Exception ignored) {}");
  252. out(" }");
  253. out(" }");
  254. }
  255. public void writeColumnDataClass() throws IOException {
  256. out(" ");
  257. out(" ");
  258. out(" ");
  259. out(" private static class ColumnData");
  260. out(" {");
  261. out(" String[] sqlTypes = { \"CHAR\",\"TINYINT\",\"BIGINT\",\"INT\",\"SMALLINT\",\"FLOAT\",\"REAL\",\"DOUBLE\",\"NUMERIC\",\"DECIMAL\",\"DATE\",\"VARCHAR\",\"LONGVARCHAR\",\"TIMESTAMP\",\"TIME\",\"BIT\",\"BINARY\",\"VARBINARY\",\"LONGVARBINARY\",\"NULL\",\"OTHER\",\"TEXT\" };");
  262. out(" int type;");
  263. out(" int columns;");
  264. out(" String name;");
  265. out(" public ColumnData (String name,int origType, int columns)");
  266. out(" {");
  267. out(" this.name = name;");
  268. out(" this.type = origType;");
  269. out(" this.columns = columns;");
  270. out(" switch (type)");
  271. out(" {");
  272. out(" case -1:");
  273. out(" break;");
  274. out(" case -2:");
  275. out(" type = 18;");
  276. out(" break;");
  277. out(" case -3:");
  278. out(" type = 19;");
  279. out(" break;");
  280. out(" case -4:");
  281. out(" type = 19;");
  282. out(" break;");
  283. out(" case -5:");
  284. out(" type = 3;");
  285. out(" break;");
  286. out(" case -6:");
  287. out(" type = 2;");
  288. out(" break;");
  289. out(" case -7:");
  290. out(" type = 16;");
  291. out(" break;");
  292. out(" case 1111:");
  293. out(" type = 3;");
  294. out(" break;");
  295. out(" case 91:");
  296. out(" type = 11;");
  297. out(" break;");
  298. out(" case 92:");
  299. out(" type = 14;");
  300. out(" break;");
  301. out(" case 93:");
  302. out(" type = 14;");
  303. out(" break;");
  304. out(" default:");
  305. out(" break;");
  306. out(" }");
  307. out(" }");
  308. out(" public ColumnData (String name, String coltype, int columns)");
  309. out(" {");
  310. out(" this.name = name;");
  311. out(" this.columns = columns;");
  312. out(" int i=0;");
  313. out(" boolean quit = false;");
  314. out(" this.type = -1; // invalid");
  315. out(" while (!quit)");
  316. out(" {");
  317. out(" if (coltype.toUpperCase().compareTo(sqlTypes[i]) == 0)");
  318. out(" {");
  319. out(" this.type = i+1;");
  320. out(" quit = true;");
  321. out(" }");
  322. out(" i++;");
  323. out(" if (i>=sqlTypes.length)");
  324. out(" quit = true;");
  325. out(" }");
  326. out(" }");
  327. out(" public String getName() {");
  328. out(" return name;");
  329. out(" }");
  330. out(" public String getJavaType()");
  331. out(" {");
  332. out(" String jType = null;");
  333. out(" switch (type)");
  334. out(" {");
  335. out(" case 1:");
  336. out(" case 12:");
  337. out(" case 13:");
  338. out(" case 22:");
  339. out(" jType = \"String\";");
  340. out(" break;");
  341. out(" case 2:");
  342. out(" jType = \"byte\";");
  343. out(" break;");
  344. out(" case 3:");
  345. out(" jType = \"long\";");
  346. out(" break;");
  347. out(" case 4:");
  348. out(" jType = \"int\";");
  349. out(" break;");
  350. out(" case 5:");
  351. out(" jType = \"short\";");
  352. out(" break;");
  353. out(" case 6:");
  354. out(" case 8:");
  355. out(" jType = \"double\";");
  356. out(" break;");
  357. out(" case 9:");
  358. out(" case 10:");
  359. out(" jType = \"java.math.BigDecimal\";");
  360. out(" break;");
  361. out(" case 7:");
  362. out(" jType = \"float\";");
  363. out(" break;");
  364. out(" case 11:");
  365. out(" jType = \"java.util.Date\";");
  366. out(" break;");
  367. out(" case 14:");
  368. out(" jType = \"Timestamp\";");
  369. out(" break;");
  370. out(" case 15:");
  371. out(" jType = \"Time\";");
  372. out(" break;");
  373. out(" case 16:");
  374. out(" jType = \"boolean\";");
  375. out(" break;");
  376. out(" case 17:");
  377. out(" case 18:");
  378. out(" case 19:");
  379. out(" jType = \"byte[]\";");
  380. out(" break;");
  381. out(" case 20:");
  382. out(" jType = \"null\";");
  383. out(" break;");
  384. out(" default:");
  385. out(" jType = \"unknown\";");
  386. out(" break;");
  387. out(" }");
  388. out(" return jType;");
  389. out(" }");
  390. out(" public String getType() {");
  391. out(" if ((type > sqlTypes.length) || (type < 0))");
  392. out(" return \"\"+type;");
  393. out(" else");
  394. out(" return sqlTypes[type-1];");
  395. out(" }");
  396. out(" } ");
  397. }
  398. public void generate() throws IOException, SQLException {
  399. if (!isEnabled)
  400. return;
  401. String folder = basedir+File.separator+GenerateSkel.packageToFile(packageName);
  402. try {
  403. File f = new File(folder);
  404. if (!f.exists())
  405. f.mkdirs();
  406. } catch (Exception e) {
  407. e.printStackTrace();
  408. }
  409. String fileName = folder+File.separator+"BdUtils.java";
  410. output = new BufferedWriter(new FileWriter(fileName));
  411. writeHeader();
  412. writeConstructor();
  413. writeExecuteQuery();
  414. writeReplaceString();
  415. writeColumnDataClass();
  416. if (isOracle)
  417. writeGetNextSequenceValue();
  418. out("}");
  419. output.close();
  420. }
  421. public static void main(String[] args) {
  422. }
  423. }