1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.finalist.tools.database;
19
20 import java.util.*;
21 import java.text.*;
22 import java.sql.*;
23 import java.util.*;
24 import java.io.*;
25 import javax.naming.*;
26 import javax.sql.*;
27 import java.lang.reflect.*;
28
29 import org.apache.commons.beanutils.BeanUtils;
30
31 /***
32 * Helper class for StatementExecutor to delegate some work to
33 * This class is beta and rather undocumented.
34 *
35 * @author P.S.D.Reitsma, Finalist IT Group
36 * @version 1.0
37 */
38 public class Helper {
39
40 public static ArrayList convertToColumnList(HashMap template) {
41 Iterator it = template.keySet().iterator();
42 ArrayList columnList = new ArrayList();
43
44 while (it.hasNext()) {
45 Object att = it.next();
46 columnList.add(new ColumnBean(createAttName(att),
47 createAttTypeAbr(att, template)));
48 }
49 return columnList;
50 }
51
52
53 public static String generateBeanFromHashMap(String name, String packageName, HashMap template) {/package-summary.html">ng> static String generateBeanFromHashMap(String name, String packageName, HashMap template) {
54 return generateBeanFromColumnList(name, packageName, convertToColumnList(template))/package-summary.html">trong> generateBeanFromColumnList(name, packageName, convertToColumnList(template));
55 }
56
57
58 public static String generateBeanFromColumnList(String name, String packageName, ArrayList columnList) {/package-summary.html">ng> static String generateBeanFromColumnList(String name, String packageName, ArrayList columnList) {
59
60 StringBuffer newBeanDef = new StringBuffer();
61 newBeanDef.append("package " + packageName + ";\n");
62 newBeanDef.append("\n");
63 newBeanDef.append("/**\n");
64 newBeanDef.append(" * ValueObject class.\n");
65 SimpleDateFormat s = new SimpleDateFormat("dd-MM-yyyy");
66 String now = s.format(Calendar.getInstance().getTime());
67 newBeanDef.append(" * Generated by StatementExecutor on " + now + "\n");
68 newBeanDef.append(" */\n");
69 newBeanDef.append("public class " + name + " {\n");
70 newBeanDef.append("\n");
71
72 for (int i = 0; i < columnList.size(); i++) {
73 ColumnBean column = (ColumnBean) columnList.get(i);
74 newBeanDef.append(" private " +
75 column.getJavaType() + " " +
76 createAttName(column.getName()) + ";\n");
77 }
78 newBeanDef.append("\n");
79
80 for (int i = 0; i < columnList.size(); i++) {
81 ColumnBean column = (ColumnBean) columnList.get(i);
82 String attName = createAttName(column.getName());
83 String attNameInitCap = createAttNameInitCap(attName);
84
85
86 String attTypeAbr = column.getJavaType();
87
88
89 newBeanDef.append(" /**\n");
90 newBeanDef.append(" * Getter method for property " + attName + "\n");
91 newBeanDef.append(" * @return " + attName + "\n");
92 newBeanDef.append(" */\n");
93 newBeanDef.append(" public " + attTypeAbr + " get" + attNameInitCap + "() {\n");
94 newBeanDef.append(" return this." + attName + ";\n");
95 newBeanDef.append(" }\n");
96 newBeanDef.append("\n");
97
98
99 newBeanDef.append(" /**\n");
100 newBeanDef.append(" * Setter method for property " + attName + "\n");
101 newBeanDef.append(" * @param pValue - input variable \n");
102 newBeanDef.append(" */\n");
103 newBeanDef.append(" public void set" + attNameInitCap + "(" + attTypeAbr + " pValue) {\n");
104 newBeanDef.append(" this." + attName + "= pValue;\n");
105 newBeanDef.append(" }\n");
106 newBeanDef.append("\n");
107 }
108 newBeanDef.append(" public String toString() {\n");
109 newBeanDef.append(" return \"\"\n");
110 for (int i = 0; i < columnList.size(); i++) {
111 ColumnBean column = (ColumnBean) columnList.get(i);
112 String attName = createAttName(column.getName());
113 String attNameInitCap = createAttNameInitCap(attName);
114 newBeanDef.append(" +\"" + attName + "=\"+get" + attNameInitCap + "()+\" \"\n");
115 }
116 newBeanDef.append(";\n }\n");
117 newBeanDef.append("}\n");
118 return newBeanDef.toString();
119 }
120
121
122 public static String createAttName(Object att) {
123 String attName = ((String) att).toLowerCase();
124
125 int i;
126 while ((i = attName.indexOf("_")) > -1) {
127 attName = attName.substring(0, i) + attName.substring(i + 1, i + 2).toUpperCase() + attName.substring(i + 2);
128 }
129 return attName;
130 }
131
132
133 public static String createAttNameInitCap(Object att) {
134 String attName = createAttName(att);
135 return attName.substring(0, 1).toUpperCase() + attName.substring(1);
136 }
137
138
139 public static String createAttTypeAbr(Object attObj, HashMap template) {
140 String attType;
141 if ((attObj != null) && ((Object) template.get(attObj) != null)) {
142 attType = ((Object) template.get(attObj)).getClass().getName();
143 }
144 else {
145 attType = "undetermined";
146 }
147 String attTypeAbr = attType;
148 if (attType.startsWith("java.lang.")) {
149 attTypeAbr = attType.substring(10);
150 }
151 if (attTypeAbr.equals("Byte")) attTypeAbr = "byte";
152 if (attTypeAbr.equals("Short")) attTypeAbr = "short";
153 if (attTypeAbr.equals("Integer")) attTypeAbr = "int";
154 if (attTypeAbr.equals("Long")) attTypeAbr = "long";
155 if (attTypeAbr.equals("Float")) attTypeAbr = "float";
156 if (attTypeAbr.equals("Double")) attTypeAbr = "double";
157 return attTypeAbr;
158 }
159 }