1
2
3 package salto.tool.jdo;
4
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import java.util.HashSet;
8 import java.util.Iterator;
9 import java.util.List;
10 import java.util.Set;
11
12 import salto.tool.ide.IdeException;
13 import salto.tool.jdo.data.JdoColInfo;
14 import salto.tool.jdo.data.JdoInfo;
15 import salto.tool.jdo.util.CreatorUtil;
16 import salto.tool.sql.DatabaseInfo;
17 import salto.tool.sql.data.TableColInfo;
18 import salto.tool.sql.data.TableFK;
19 import salto.tool.sql.data.TableInfo;
20
21 import com.salto.db.generator.plugin.IGeneratorPlugin;
22 import com.salto.db.generator.plugin.Plugins;
23
24 /***
25 * Object capable de générer les codes des DataObject et de les réfléchir
26 * (i.e. extraire et interpréter leurs propriétés) Date de création :
27 * (7/02/01 17:55:05)
28 *
29 * @author : E.Loiez
30 */
31 public class DataObjectCreate {
32 public static final String NEW_LINE = "\r\n";
33
34 /***
35 * Constructeur implicite
36 */
37 public DataObjectCreate() {
38 super();
39 }
40
41 /***
42 * @exception
43 * @author
44 * @deprecated Insérez la description de la méthode à cet endroit. Date
45 * de création : (20/03/01 17:09:16)
46 * @return int
47 * @param sqlTyp
48 * int
49 * @roseuid 3C025B6D030E
50 */
51 public static String convert(int sqlTyp) {
52 String result = null;
53 switch (sqlTyp) {
54 case java.sql.Types.BIGINT: {
55 result = "Long";
56 break;
57 }
58 case java.sql.Types.NUMERIC:
59 case java.sql.Types.DECIMAL: {
60 result = "BigDecimal";
61 break;
62 }
63 case java.sql.Types.CHAR:
64 case java.sql.Types.VARCHAR:
65 case java.sql.Types.LONGVARCHAR: {
66 result = "String";
67 break;
68 }
69 case java.sql.Types.DATE:
70 case java.sql.Types.TIME:
71 case java.sql.Types.TIMESTAMP: {
72 result = "Timestamp";
73 break;
74 }
75 case java.sql.Types.DOUBLE: {
76 result = "Double";
77 break;
78 }
79 case java.sql.Types.FLOAT: {
80 result = "Float";
81 break;
82 }
83 case java.sql.Types.INTEGER: {
84 result = "Integer";
85 break;
86 }
87 case java.sql.Types.SMALLINT: {
88 result = "Short";
89 break;
90 }
91 default: {
92
93
94 }
95 }
96 return result;
97 }
98
99 /***
100 * @exception
101 * @author
102 * @deprecated Retourne la conversion pour . Date de création : (20/03/01
103 * 17:09:16)
104 * @return int
105 * @param sqlTyp
106 * int
107 * @roseuid 3C025B6D0313
108 */
109 public static String convertSet(int sqlTyp) {
110 String result = "";
111 switch (sqlTyp) {
112 case java.sql.Types.BIGINT:
113 case java.sql.Types.DECIMAL: {
114 result = "setBigDecimal";
115 break;
116 }
117 case java.sql.Types.CHAR:
118 case java.sql.Types.VARCHAR: {
119 result = "setString";
120 break;
121 }
122 case java.sql.Types.DATE:
123 case java.sql.Types.TIME:
124 case java.sql.Types.TIMESTAMP: {
125 result = "setTimestamp";
126 break;
127 }
128 case java.sql.Types.DOUBLE: {
129 result = "setDouble";
130 break;
131 }
132 case java.sql.Types.FLOAT:
133 case java.sql.Types.NUMERIC: {
134 result = "setFloat";
135 break;
136 }
137 case java.sql.Types.INTEGER: {
138 result = "setInt";
139 break;
140 }
141 case java.sql.Types.SMALLINT: {
142 result = "setShort";
143 break;
144 }
145 default: {
146
147
148 }
149 }
150
151 return result;
152 }
153
154 /***
155 * TODO : this code is ugly
156 *
157 * @param jdoInfos
158 * @return
159 */
160 private JdoInfo[] extend(JdoInfo[] jdoInfos) {
161 Set tablesNames = new HashSet();
162 Set addedTablesNames = new HashSet();
163
164 try {
165 List jdo = new ArrayList();
166
167 for (int j = 0; j < jdoInfos.length; j++) {
168 tablesNames.add(jdoInfos[j].getTableName());
169 }
170
171 for (int j = 0; j < jdoInfos.length; j++) {
172
173 TableFK[][] fks = jdoInfos[j].getFK();
174 for (int i = 0; i < fks.length; i++) {
175 for (int k = 0; k < fks[i].length; k++) {
176
177 String newTableName = fks[i][k].getPkTableName();
178
179 if (fks[i][k].getPkTableName().equals(jdoInfos[j].getTableName())) {
180 newTableName = fks[i][k].getFkTableName();
181 }
182
183 boolean already = tablesNames.contains(newTableName);
184 if (!already) {
185 addedTablesNames.add(newTableName);
186 System.out.println("Adding table " + newTableName + " as it is needed by table " + jdoInfos[j].getTableName());
187
188 }
189 }
190
191 }
192
193 }
194
195 ArrayList list = new ArrayList();
196 for (Iterator iterator = addedTablesNames.iterator(); iterator.hasNext();) {
197 String string = (String) iterator.next();
198 TableInfo[] tableInfos = DatabaseInfo.getTables(jdoInfos[0].getConnInfo().getConn(), null, jdoInfos[0].getTableShem(), string);
199 list.addAll(Arrays.asList(tableInfos));
200 }
201
202 TableInfo[] tableInfos = (TableInfo[]) list.toArray(new TableInfo[list.size()]);
203
204 for (int f = 0; f < tableInfos.length; f++) {
205 tableInfos[f].setConnInfo(jdoInfos[0].getConnInfo());
206 JdoInfo jdoInfo = DataObjectCreate.createJdoInfoFromTableInfo(tableInfos[f]);
207 jdo.add(jdoInfo);
208 }
209
210 if (!jdo.isEmpty()) {
211 jdo.addAll(Arrays.asList(jdoInfos));
212 jdoInfos = (JdoInfo[]) jdo.toArray(new JdoInfo[jdo.size()]);
213 }
214
215 } catch (Exception e) {
216
217 e.printStackTrace();
218 }
219 return jdoInfos;
220
221 }
222
223 /***
224 * Méthode permettant de générer les dataobjects Date de création :
225 * (7/02/01 17:55:57)
226 *
227 * @param compName
228 * java.lang.String le nom du composant
229 * @param infos
230 * salto.tool.jdo.JdoInfo[] informations sur les attributs java
231 * et le mappage de la base de donn�e
232 * @return void
233 * @exception JdoCreateException,IdeException
234 */
235 public void creerDoSimple(String doPrefix, int defaultNameOffset, String srcPath, String pckName, JdoInfo[] jdoInfos, String pluginName, Integer maxExtendIteration) throws JdoCreateException, IdeException {
236
237 int size = jdoInfos.length;
238 int previousSize = 0;
239 int i = 0;
240
241 while (size > previousSize && ((maxExtendIteration != null && i < maxExtendIteration.intValue()) || maxExtendIteration == null)) {
242 jdoInfos = extend(jdoInfos);
243 previousSize = size;
244 size = jdoInfos.length;
245 i++;
246 }
247
248 IGeneratorPlugin plugin = null;
249 try {
250 plugin = Plugins.getInstance().getPlugin(pluginName);
251 if (plugin != null) {
252 plugin.init(doPrefix, defaultNameOffset, srcPath, pckName);
253 for (int j = 0; j < jdoInfos.length; j++) {
254
255 String doName = CreatorUtil.getDoNameCreator().createDoName(jdoInfos[j].getTableName(), doPrefix, defaultNameOffset);
256 plugin.execute(doName, jdoInfos[j]);
257 }
258
259 plugin.postExecute(jdoInfos);
260
261 } else
262 System.out.println("Cannot find plugin " + pluginName);
263 } catch (Exception e) {
264 if (plugin != null) {
265 try {
266 plugin.abort();
267 } catch (Exception e1) {
268 e1.printStackTrace();
269 }
270 }
271 e.printStackTrace();
272 }
273
274 }
275
276 public static JdoInfo createJdoInfoFromTableInfo(TableInfo tableInfo) {
277
278 TableColInfo[] colInfos = DatabaseInfo.getColumns(tableInfo);
279 if (colInfos != null) {
280 tableInfo.setColInfos(colInfos);
281 JdoInfo jdoInfo = new JdoInfo(tableInfo);
282 JdoColInfo[] infos = new JdoColInfo[colInfos.length];
283 for (int i = 0; i < colInfos.length; i++) {
284 infos[i] = new JdoColInfo(colInfos[i]);
285 }
286
287 return jdoInfo;
288 } else {
289 System.out.println("Skipping " + tableInfo.getTableType() + " '" + tableInfo.getTableName() + "' because it was not possible to read it.");
290 return null;
291 }
292
293 }
294
295 }