KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > loader > DefaultEntityAliases


1 package org.hibernate.loader;
2
3 import java.util.Map JavaDoc;
4
5 import org.hibernate.persister.entity.Loadable;
6 import org.hibernate.util.CollectionHelper;
7
8 /**
9  * EntityAliases which handles the logic of selecting user provided aliases (via return-property),
10  * before using the default aliases.
11  *
12  * @author max
13  *
14  */

15 public class DefaultEntityAliases implements EntityAliases {
16
17     private final String JavaDoc[] suffixedKeyColumns;
18     private final String JavaDoc[] suffixedVersionColumn;
19     private final String JavaDoc[][] suffixedPropertyColumns;
20     private final String JavaDoc suffixedDiscriminatorColumn;
21     private final String JavaDoc suffix;
22     private final String JavaDoc rowIdAlias;
23     private final Map JavaDoc userProvidedAliases;
24
25     public DefaultEntityAliases(Loadable persister, String JavaDoc suffix) {
26         this(CollectionHelper.EMPTY_MAP, persister, suffix);
27     }
28     
29     /**
30      * Calculate and cache select-clause suffixes.
31      * @param map
32      */

33     public DefaultEntityAliases(Map JavaDoc userProvidedAliases, Loadable persister, String JavaDoc suffix) {
34         this.suffix = suffix;
35         this.userProvidedAliases = userProvidedAliases;
36         
37         String JavaDoc[] keyColumnsCandidates = getUserProvidedAliases(
38                 persister.getIdentifierPropertyName(),
39                 (String JavaDoc[]) null
40             );
41         if (keyColumnsCandidates==null) {
42             suffixedKeyColumns = getUserProvidedAliases(
43                     "id",
44                     getIdentifierAliases(persister, suffix)
45                 );
46         }
47         else {
48             suffixedKeyColumns = keyColumnsCandidates;
49         }
50         intern(suffixedKeyColumns);
51         
52         suffixedPropertyColumns = getSuffixedPropertyAliases(persister);
53         suffixedDiscriminatorColumn = getUserProvidedAlias(
54                 "class",
55                 getDiscriminatorAlias(persister, suffix)
56             );
57         if ( persister.isVersioned() ) {
58             suffixedVersionColumn = suffixedPropertyColumns[ persister.getVersionProperty() ];
59         }
60         else {
61             suffixedVersionColumn = null;
62         }
63         rowIdAlias = Loadable.ROWID_ALIAS + suffix; // TODO: not visible to the user!
64
}
65
66     protected String JavaDoc getDiscriminatorAlias(Loadable persister, String JavaDoc suffix) {
67         return persister.getDiscriminatorAlias(suffix);
68     }
69
70     protected String JavaDoc[] getIdentifierAliases(Loadable persister, String JavaDoc suffix) {
71         return persister.getIdentifierAliases(suffix);
72     }
73     
74     protected String JavaDoc[] getPropertyAliases(Loadable persister, int j) {
75         return persister.getPropertyAliases(suffix, j);
76     }
77     
78     private String JavaDoc[] getUserProvidedAliases(String JavaDoc propertyPath, String JavaDoc[] defaultAliases) {
79         String JavaDoc[] result = (String JavaDoc[]) userProvidedAliases.get(propertyPath);
80         if (result==null) {
81             return defaultAliases;
82         }
83         else {
84             return result;
85         }
86     }
87
88     private String JavaDoc getUserProvidedAlias(String JavaDoc propertyPath, String JavaDoc defaultAlias) {
89         String JavaDoc[] columns = (String JavaDoc[]) userProvidedAliases.get(propertyPath);
90         if (columns==null) {
91             return defaultAlias;
92         }
93         else {
94             return columns[0];
95         }
96     }
97     
98     public String JavaDoc[][] getSuffixedPropertyAliases(Loadable persister) {
99         int size = persister.getPropertyNames().length;
100         String JavaDoc[][] suffixedPropertyAliases = new String JavaDoc[size][];
101         for ( int j = 0; j < size; j++ ) {
102             suffixedPropertyAliases[j] = getUserProvidedAliases(
103                     persister.getPropertyNames()[j],
104                     getPropertyAliases(persister, j)
105                 );
106             intern( suffixedPropertyAliases[j] );
107         }
108         return suffixedPropertyAliases;
109     }
110
111     public String JavaDoc[] getSuffixedVersionAliases() {
112         return suffixedVersionColumn;
113     }
114
115     public String JavaDoc[][] getSuffixedPropertyAliases() {
116         return suffixedPropertyColumns;
117     }
118
119     public String JavaDoc getSuffixedDiscriminatorAlias() {
120         return suffixedDiscriminatorColumn;
121     }
122
123     public String JavaDoc[] getSuffixedKeyAliases() {
124         return suffixedKeyColumns;
125     }
126
127     public String JavaDoc getRowIdAlias() {
128         return rowIdAlias;
129     }
130     
131     private static void intern(String JavaDoc[] strings) {
132         for (int i=0; i<strings.length; i++ ) {
133             strings[i] = strings[i].intern();
134         }
135     }
136
137 }
138
Popular Tags