KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdbc > SharedColumnChooser


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdbc;
13
14 import com.versant.core.metadata.ClassMetaData;
15 import com.versant.core.metadata.MDStatics;
16 import com.versant.core.jdbc.metadata.JdbcField;
17 import com.versant.core.jdbc.metadata.JdbcClass;
18 import com.versant.core.jdbc.metadata.JdbcColumn;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Comparator JavaDoc;
22 import java.util.Collections JavaDoc;
23
24 /**
25  * This is used by the JdbcMetaDataBuilder to decide which columns are shared
26  * when several fields in a class heirachy are mapped with the same column
27  * names.
28  *
29  */

30 public class SharedColumnChooser implements Comparator JavaDoc {
31
32     public SharedColumnChooser() {
33     }
34
35     /**
36      * Select shared columns for cmd and all of its subclasses.
37      */

38     public void chooseSharedColumns(ClassMetaData cmd) {
39         JdbcClass jdbcClass = (JdbcClass)cmd.storeClass;
40         ArrayList JavaDoc fieldList = new ArrayList JavaDoc();
41
42         for (int i = 0; i < jdbcClass.fields.length; i++) {
43             JdbcField f = jdbcClass.fields[i];
44             if (f == null) continue;
45             JdbcColumn[] mtc = f.mainTableCols;
46             if (mtc == null) continue;
47
48             // decide on shared for each main table column for this field
49
for (int j = 0; j < mtc.length; j++) {
50                 JdbcColumn c = mtc[j];
51                 if (c.shared) continue; // already shared
52

53                 // if this column is part of the primary key then any non-pk
54
// fields using it must have their column set shared = true
55
if (c.pk) {
56                     jdbcClass.markColumnsShared(c.name, jdbcClass.table);
57                 } else {
58
59                     // find all the fields we have that use this column and
60
// sort them so the the only field that will have
61
// shared = false is first in the list
62
fieldList.clear();
63                     jdbcClass.findFieldsForColumn(f.fmd.classMetaData, c.name,
64                             fieldList);
65                     int n = fieldList.size();
66                     if (n > 1) {
67                         Collections.sort(fieldList, this);
68                         for (int k = 1; k < n; k++) {
69                             JdbcField kf = ((JdbcField)fieldList.get(k));
70                             kf.findMainTableColumn(c.name).setShared(true);
71                         }
72                     }
73                 }
74
75                 // mark the column shared in all subclass fields
76
jdbcClass.markSubclassColumnsShared(c.name);
77             }
78         }
79
80         if (((JdbcClass)cmd.storeClass).classIdCol != null) {
81             jdbcClass.markColumnsShared(jdbcClass.classIdCol.name, jdbcClass.table);
82         }
83
84         //if (Debug.DEBUG) dump(jdbcClass);
85

86         // now do all of our subclasses
87
if (cmd.pcSubclasses != null) {
88             for (int i = 0; i < cmd.pcSubclasses.length; i++) {
89                 chooseSharedColumns(cmd.pcSubclasses[i]);
90             }
91         }
92     }
93
94     /**
95      * Order JdbcField's so the field that should have shared=false for
96      * columns in common is first.
97      */

98     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
99         JdbcField a = (JdbcField)o1;
100         JdbcField b = (JdbcField)o2;
101
102         // put simple fields first
103
boolean simpleA = a.fmd.category == MDStatics.CATEGORY_SIMPLE;
104         boolean simpleB = b.fmd.category == MDStatics.CATEGORY_SIMPLE;
105         if (simpleA && !simpleB) return -1;
106         if (!simpleA && simpleB) return +1;
107
108         // put the field with the least columns first
109
int diff = a.mainTableCols.length - b.mainTableCols.length;
110         if (diff != 0) return diff < 0 ? -1 : +1;
111
112         // order by fieldNo (i.e. alpha sort on name)
113
return a.fmd.fieldNo - b.fmd.fieldNo;
114     }
115
116     private void dump(JdbcClass jdbcClass) {
117         System.out.println("\nSharedColumnChooser.dump " + jdbcClass);
118         for (int i = 0; i < jdbcClass.fields.length; i++) {
119             JdbcField f = jdbcClass.fields[i];
120             System.out.println("fields[" + i + "] = " + f);
121             if (f == null) continue;
122             JdbcColumn[] mtc = f.mainTableCols;
123             if (mtc == null) continue;
124             for (int j = 0; j < mtc.length; j++) {
125                 JdbcColumn c = mtc[j];
126                 System.out.println(" mtc[" + j + "] = " + c);
127             }
128         }
129         System.out.println("---\n");
130     }
131
132 }
133
134
135
Popular Tags