001: package net.sourceforge.squirrel_sql.plugins.hibernate.mapping;
002:
003: import net.sourceforge.squirrel_sql.fw.completion.CompletionInfo;
004: import net.sourceforge.squirrel_sql.fw.completion.util.CompletionParser;
005: import net.sourceforge.squirrel_sql.plugins.hibernate.completion.HQLCompletionInfoCollection;
006: import net.sourceforge.squirrel_sql.plugins.hibernate.completion.MappingInfoProvider;
007:
008: import java.util.ArrayList;
009: import java.util.Collection;
010: import java.util.Arrays;
011:
012: public class MappedClassInfo extends CompletionInfo {
013: private String _mappedClassName;
014: private String _tableName;
015: private PropertyInfo[] _propertyInfos;
016: private String _simpleMappedClassName;
017: private CompletionParser _lastParser;
018:
019: public MappedClassInfo(String mappedClassName, String tableName,
020: HibernatePropertyInfo indentifierHibernatePropertyInfo,
021: HibernatePropertyInfo[] hibernatePropertyInfos) {
022: _mappedClassName = mappedClassName;
023: _tableName = tableName;
024: _simpleMappedClassName = MappingUtils
025: .getSimpleClassName(mappedClassName);
026:
027: _propertyInfos = new PropertyInfo[hibernatePropertyInfos.length + 1];
028:
029: _propertyInfos[0] = new PropertyInfo(
030: indentifierHibernatePropertyInfo, _mappedClassName);
031: for (int i = 0; i < hibernatePropertyInfos.length; i++) {
032: _propertyInfos[i + 1] = new PropertyInfo(
033: hibernatePropertyInfos[i], _mappedClassName);
034: }
035:
036: }
037:
038: public String getCompareString() {
039: if (null != _lastParser
040: && _mappedClassName.startsWith(_lastParser
041: .getStringToParse())) {
042: return _mappedClassName;
043: }
044:
045: return _simpleMappedClassName;
046: }
047:
048: public boolean matches(CompletionParser parser) {
049: _lastParser = parser;
050: return _mappedClassName.startsWith(parser.getStringToParse())
051: || _simpleMappedClassName.startsWith(parser
052: .getStringToParse());
053: }
054:
055: public boolean hasColumns() {
056: return true;
057: }
058:
059: public ArrayList<PropertyInfo> getQualifiedMatchingAttributes(
060: CompletionParser parser) {
061: ArrayList<PropertyInfo> ret = new ArrayList<PropertyInfo>();
062:
063: String stringToParse = parser.getStringToParse();
064:
065: String propertyChainBegin;
066: if (stringToParse.startsWith(_mappedClassName + ".")) {
067: propertyChainBegin = stringToParse
068: .substring((_mappedClassName + ".").length());
069: } else if (stringToParse.startsWith(_simpleMappedClassName
070: + ".")) {
071: propertyChainBegin = stringToParse
072: .substring((_simpleMappedClassName + ".").length());
073: } else {
074: return ret;
075: }
076:
077: ArrayList<String> props = getArrayFormChain(propertyChainBegin);
078:
079: PropertyInfo[] propInfoBuf = _propertyInfos;
080:
081: for (int i = 0; i < props.size(); i++) {
082: for (PropertyInfo propertyInfo : propInfoBuf) {
083: if (propertyInfo.matchesUnQualified(props.get(i))) {
084: if (i == props.size() - 1) {
085: ret.add(propertyInfo);
086: } else if (i < props.size() - 1
087: && props.get(i).equals(
088: propertyInfo
089: .getHibernatePropertyInfo()
090: .getPropertyName())) {
091: // This could (perhaps more elegantly) be done by recursion
092: propInfoBuf = new PropertyInfo[0];
093:
094: MappedClassInfo mappedClassInfo = propertyInfo
095: .getMappedClassInfo();
096: if (null != mappedClassInfo) {
097: propInfoBuf = mappedClassInfo
098: .getAttributes();
099: }
100: break;
101: }
102: }
103: }
104: }
105:
106: return ret;
107:
108: }
109:
110: private ArrayList<String> getArrayFormChain(
111: String propertyChainBegin) {
112: ArrayList<String> ret = new ArrayList<String>();
113:
114: ret.addAll(Arrays.asList(propertyChainBegin.split("\\.")));
115:
116: if (propertyChainBegin.endsWith(".")) {
117: ret.add("");
118: }
119:
120: return ret;
121:
122: }
123:
124: public Collection<? extends CompletionInfo> getMatchingAttributes(
125: CompletionParser parser) {
126: ArrayList<CompletionInfo> ret = new ArrayList<CompletionInfo>();
127:
128: for (PropertyInfo propertyInfo : _propertyInfos) {
129: if (propertyInfo.matchesUnQualified(parser.getLastToken())) {
130: ret.add(propertyInfo);
131: }
132: }
133:
134: return ret;
135: }
136:
137: public boolean isSame(String name) {
138: return _mappedClassName.equals(name)
139: || _simpleMappedClassName.equals(name);
140: }
141:
142: public String getClassName() {
143: return _mappedClassName;
144: }
145:
146: public String getSimpleClassName() {
147: return _simpleMappedClassName;
148: }
149:
150: public String[] getAttributeNames() {
151: String[] ret = new String[_propertyInfos.length];
152:
153: for (int i = 0; i < _propertyInfos.length; i++) {
154: ret[i] = _propertyInfos[i].getCompareString();
155:
156: }
157:
158: return ret;
159: }
160:
161: public PropertyInfo getAttributeByName(String attrName) {
162: for (PropertyInfo propertyInfo : _propertyInfos) {
163: if (propertyInfo.getCompareString().equals(attrName)) {
164: return propertyInfo;
165: }
166: }
167:
168: return null;
169:
170: }
171:
172: public PropertyInfo[] getAttributes() {
173: return _propertyInfos;
174: }
175:
176: public String getTableName() {
177: return _tableName;
178: }
179:
180: public void initAttributesWithClassInfo(
181: MappingInfoProvider mappingInfoProvider) {
182: for (PropertyInfo propertyInfo : _propertyInfos) {
183: propertyInfo
184: .setMappedClassInfo(mappingInfoProvider
185: .getMappedClassInfoFor(propertyInfo
186: .getHibernatePropertyInfo()
187: .getClassName()));
188: }
189: }
190: }
|