1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package com.finalist.tools.statementExecutorUtils;
25
26 import com.finalist.tools.database.*;
27
28 import java.sql.Connection;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.io.File;
32 import java.io.FileWriter;
33 import java.io.IOException;
34 import java.sql.DriverManager;
35
36 /***
37 * Generates Data Transfer Objects (Value Objects) of tables in the database.
38 *
39 *
40 * @author hillebrand
41 */
42 public class OracleDTOGenerator {
43
44 /***
45 * @param args the command line arguments
46 */
47 public static void main(String[] args) {
48 QueryHelper qh = new QueryHelper();
49 ArrayList tables;
50
51 if (args.length == 0) {
52 qh.prepareStatement("SELECT TABLE_NAME FROM USER_TABLES");
53 tables = qh.executeQueryIntoHashMap();
54 }
55 else {
56 tables = new ArrayList();
57 for (int i = 0; i < args.length; i++) {
58 HashMap hm = new HashMap();
59 hm.put("TABLE_NAME", args[i]);
60 tables.add(hm);
61 }
62 }
63
64 qh.prepareStatement("SELECT COLUMN_NAME as name, " +
65 " DATA_TYPE as type, " +
66 " DATA_PRECISION as precision, " +
67 " DATA_SCALE as scale " +
68 " FROM USER_TAB_COLUMNS " +
69 " WHERE TABLE_NAME = :TABLE_NAME");
70
71 String packageName = "com.finalist.facturenSynchronisatie.value";
72 String saveLocation = "C:/facturenSynchronisatie/src/java/com/finalist/facturenSynchronisatie/value/";
73
74 for (int i = 0; i < tables.size(); i++) {
75 String tableName = (String) ((HashMap) tables.get(i)).get("TABLE_NAME");
76 System.out.println(tableName);
77 qh.setBindVar("TABLE_NAME", tableName);
78 ArrayList columnList = qh.executeQueryIntoHashMap();
79 columnList = OracleRowMapper.convertDatatypes(columnList);
80 String bean = Helper.generateBeanFromColumnList(Helper.createAttNameInitCap(tableName),
81 packageName, columnList);
82 try {
83 File classFile = new File(saveLocation + Helper.createAttNameInitCap(tableName) + ".java");
84 FileWriter fw = new FileWriter(classFile);
85 fw.write(bean);
86 fw.close();
87 }
88 catch (IOException e) {
89 e.printStackTrace();
90 }
91 }
92 qh.close();
93 }
94 }