1 package org.apache.torque.map;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.Serializable;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.Iterator;
28 import java.util.LinkedHashMap;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Set;
32 import java.util.StringTokenizer;
33
34 import org.apache.commons.lang.StringUtils;
35 import org.apache.torque.Database;
36 import org.apache.torque.Torque;
37 import org.apache.torque.TorqueException;
38 import org.apache.torque.adapter.IDMethod;
39
40
41
42
43
44
45
46
47
48 public class TableMap implements Serializable
49 {
50
51
52
53 private static final long serialVersionUID = 1L;
54
55
56 private final Map<String, ColumnMap> columns
57 = Collections.synchronizedMap(
58 new LinkedHashMap<String, ColumnMap>());
59
60
61 private final List<ForeignKeyMap> foreignKeys = new ArrayList<ForeignKeyMap>();
62
63
64 private DatabaseMap dbMap;
65
66
67 private String tableName;
68
69
70
71
72
73 private String schemaName;
74
75
76 private String javaName;
77
78
79 private String prefix;
80
81
82 private IDMethod primaryKeyMethod = IDMethod.NO_ID_METHOD;
83
84
85 private String description = "";
86
87
88 private Class<?> peerClass;
89
90
91 private Class<?> omClass;
92
93
94 private boolean useInheritance = false;
95
96
97 private boolean useManager = false;
98
99
100 private Class<?> managerClass;
101
102
103 private Object pkInfoOverride;
104
105
106
107
108
109
110 private final Map<IDMethod, Object> pkInfoMap
111 = new HashMap<IDMethod, Object>();
112
113
114 private final Map<String, String> optionsMap
115 = Collections.synchronizedMap(new LinkedHashMap<String, String>());
116
117
118
119
120
121
122
123
124 public TableMap(String tableName, DatabaseMap containingDB)
125 {
126 setTableName(tableName);
127 dbMap = containingDB;
128 }
129
130
131
132
133
134
135
136
137
138
139 public TableMap(String tableName,
140 String prefix,
141 DatabaseMap containingDB)
142 {
143 setTableName(tableName);
144 this.prefix = prefix;
145 dbMap = containingDB;
146 }
147
148 private void setTableName(String tableName)
149 {
150 if (tableName == null)
151 {
152 throw new NullPointerException("tableName must not be null");
153 }
154 int dotIndex = tableName.indexOf(".");
155 if (dotIndex != -1)
156 {
157 this.schemaName = tableName.substring(0, dotIndex);
158 this.tableName = tableName.substring(dotIndex + 1);
159 }
160 else
161 {
162 this.tableName = tableName;
163 }
164 }
165
166
167
168
169
170 void setDatabaseMap(DatabaseMap databaseMap)
171 {
172 dbMap = databaseMap;
173 }
174
175
176
177
178
179
180
181 public boolean containsColumn(ColumnMap column)
182 {
183 return containsColumn(column.getColumnName());
184 }
185
186
187
188
189
190
191
192 public boolean containsColumn(String name)
193 {
194 if (name.indexOf('.') > 0)
195 {
196 name = name.substring(name.indexOf('.') + 1);
197 }
198 return columns.containsKey(name);
199 }
200
201
202
203
204
205
206 public DatabaseMap getDatabaseMap()
207 {
208 return dbMap;
209 }
210
211
212
213
214
215
216
217
218 public boolean containsObjectColumn()
219 {
220 synchronized (columns)
221 {
222 Iterator<ColumnMap> it = columns.values().iterator();
223 while (it.hasNext())
224 {
225 Object theType = it.next().getType();
226 if (!(theType instanceof String || theType instanceof Number
227 || theType instanceof java.util.Date))
228 {
229 return true;
230 }
231 }
232 }
233 return false;
234 }
235
236
237
238
239
240
241 public String getName()
242 {
243 return tableName;
244 }
245
246
247
248
249
250
251 public String getSchemaName()
252 {
253 return schemaName;
254 }
255
256
257
258
259
260
261 public String getJavaName()
262 {
263 return javaName;
264 }
265
266
267
268
269
270
271 public void setJavaName(String value)
272 {
273 this.javaName = value;
274 }
275
276
277
278
279
280
281 public String getPrefix()
282 {
283 return this.prefix;
284 }
285
286
287
288
289
290
291
292 public void setPrefix(String prefix)
293 {
294 this.prefix = prefix;
295 }
296
297
298
299
300
301
302 public IDMethod getPrimaryKeyMethod()
303 {
304 return primaryKeyMethod;
305 }
306
307
308
309
310
311
312 public Object getPrimaryKeyMethodInfo(IDMethod idMethod)
313 {
314 if (pkInfoOverride != null)
315 {
316 return pkInfoOverride;
317 }
318 return pkInfoMap.get(idMethod);
319 }
320
321
322
323
324
325
326 public ColumnMap[] getColumns()
327 {
328 ColumnMap[] tableColumns = new ColumnMap[columns.size()];
329 synchronized (columns)
330 {
331 Iterator<ColumnMap> it = columns.values().iterator();
332 int i = 0;
333 while (it.hasNext())
334 {
335 tableColumns[i++] = it.next();
336 }
337 }
338 return tableColumns;
339 }
340
341
342
343
344
345
346 public List<ForeignKeyMap> getForeignKeys()
347 {
348 return Collections.unmodifiableList(foreignKeys);
349 }
350
351
352
353
354
355
356
357 public ColumnMap getColumn(String name)
358 {
359 try
360 {
361 return columns.get(name);
362 }
363 catch (Exception e)
364 {
365 return null;
366 }
367 }
368
369
370
371
372
373
374
375 public void addColumn(ColumnMap cmap)
376 {
377 columns.put(cmap.getColumnName(), cmap);
378 }
379
380
381
382
383
384
385 public void addForeignKey(ForeignKeyMap foreignKey)
386 {
387 foreignKeys.add(foreignKey);
388 }
389
390
391
392
393
394
395
396
397 public void setPrimaryKeyMethod(IDMethod method)
398 {
399 if (method == null)
400 {
401 throw new NullPointerException("method must not be null");
402 }
403 primaryKeyMethod = method;
404 if (IDMethod.ID_BROKER == method)
405 {
406 Database database = Torque.getOrCreateDatabase(
407 getDatabaseMap().getName());
408 database.createAndRegisterIdBroker();
409 }
410 }
411
412
413
414
415
416
417
418
419 public void setPrimaryKeyMethodInfo(Object pkInfo)
420 {
421 pkInfoOverride = pkInfo;
422 }
423
424
425
426
427
428
429
430 public void setPrimaryKeyMethodInfo(IDMethod idMethod, Object pkInfo)
431 {
432 pkInfoMap.put(idMethod, pkInfo);
433 }
434
435
436
437
438
439
440
441
442
443 private boolean hasPrefix(String data)
444 {
445 return (data.indexOf(getPrefix()) != -1);
446 }
447
448
449
450
451
452
453
454 private String removePrefix(String data)
455 {
456 return data.substring(getPrefix().length());
457 }
458
459
460
461
462
463
464
465
466
467
468 public final String removeUnderScores(String data)
469 {
470 String tmp = null;
471 StringBuffer out = new StringBuffer();
472 if (hasPrefix(data))
473 {
474 tmp = removePrefix(data);
475 }
476 else
477 {
478 tmp = data;
479 }
480
481 StringTokenizer st = new StringTokenizer(tmp, "_");
482 while (st.hasMoreTokens())
483 {
484 String element = ((String) st.nextElement()).toLowerCase();
485 out.append(StringUtils.capitalize(element));
486 }
487 return out.toString();
488 }
489
490
491
492
493
494
495 public String getDescription()
496 {
497 return description;
498 }
499
500
501
502
503
504
505 public void setDescription(String description)
506 {
507 this.description = description;
508 }
509
510
511
512
513
514
515 public Class<?> getOMClass()
516 {
517 return omClass;
518 }
519
520
521
522
523
524
525 public void setOMClass(Class<?> omClass)
526 {
527 this.omClass = omClass;
528 }
529
530
531
532
533
534
535 public Class<?> getPeerClass()
536 {
537 return peerClass;
538 }
539
540
541
542
543
544
545 public void setPeerClass(Class<?> peerClass)
546 {
547 this.peerClass = peerClass;
548 }
549
550
551
552
553
554
555 public DatabaseMap getDbMap()
556 {
557 return dbMap;
558 }
559
560
561
562
563
564
565 public boolean isUseInheritance()
566 {
567 return useInheritance;
568 }
569
570
571
572
573
574
575 public void setUseInheritance(boolean useInheritance)
576 {
577 this.useInheritance = useInheritance;
578 }
579
580
581
582
583
584
585 public boolean isUseManager()
586 {
587 return useManager;
588 }
589
590
591
592
593
594
595 public void setUseManager(boolean useManager)
596 {
597 this.useManager = useManager;
598 }
599
600
601
602
603
604
605 public Class<?> getManagerClass()
606 {
607 return managerClass;
608 }
609
610
611
612
613
614
615 public void setManagerClass(Class<?> managerClass)
616 {
617 this.managerClass = managerClass;
618 }
619
620
621
622
623
624
625 public Map<String, String> getOptions()
626 {
627 return Collections.unmodifiableMap(optionsMap);
628 }
629
630
631
632
633
634
635
636 public void setOption(String key, String value)
637 {
638 optionsMap.put(key, value);
639 }
640
641
642
643
644
645
646
647
648 public String getOption(String key)
649 {
650 return optionsMap.get(key);
651 }
652
653
654
655
656
657
658
659
660
661 public ColumnMap getPrimaryKey()
662 throws TorqueException
663 {
664 Set<ColumnMap> result = new HashSet<ColumnMap>();
665
666 for (ColumnMap column : columns.values())
667 {
668 if (column.isPrimaryKey())
669 {
670 result.add(column);
671 }
672 }
673 if (result.isEmpty())
674 {
675 throw new TorqueException("getPrimaryKey(): Table " + tableName
676 + "has no primary key.");
677 }
678 if (result.size() > 1)
679 {
680 throw new TorqueException("getPrimaryKey(): Table " + tableName
681 + "has more than one primary key.");
682 }
683 return result.iterator().next();
684 }
685
686 @Override
687 public String toString()
688 {
689 return "TableMap[" + tableName + "]";
690 }
691 }