1 ///*2 // * The contents of this file are subject to the terms of the Common Development3 // * and Distribution License (the License). You may not use this file except in4 // * compliance with the License.5 // *6 // * You can obtain a copy of the License at http://www.netbeans.org/cddl.html7 // * or http://www.netbeans.org/cddl.txt.8 // *9 // * When distributing Covered Code, include this CDDL Header Notice in each file10 // * and include the License file at http://www.netbeans.org/cddl.txt.11 // * If applicable, add the following below the CDDL Header, with the fields12 // * enclosed by brackets [] replaced by your own identifying information:13 // * "Portions Copyrighted [year] [name of copyright owner]"14 // *15 // * The Original Software is NetBeans. The Initial Developer of the Original16 // * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun17 // * Microsystems, Inc. All Rights Reserved.18 // */19 //20 //package org.netbeans.modules.j2ee.persistence.editor.completion.db;21 //22 //import java.lang.reflect.Modifier;23 //import java.sql.Connection;24 //import java.sql.SQLException;25 //import java.util.ArrayList;26 //import java.util.Collections;27 //import java.util.Iterator;28 //import java.util.List;29 //import java.util.Map;30 //import java.util.Set;31 //import java.util.TreeSet;32 //import javax.swing.text.BadLocationException;33 //import org.netbeans.api.db.explorer.DatabaseConnection;34 //import org.netbeans.api.project.FileOwnerQuery;35 //import org.netbeans.api.project.Project;36 //import org.netbeans.editor.ext.java.JCExpression;37 //import org.netbeans.jmi.javamodel.Annotation;38 //import org.netbeans.jmi.javamodel.ClassDefinition;39 //import org.netbeans.jmi.javamodel.Feature;40 //import org.netbeans.jmi.javamodel.Field;41 //import org.netbeans.jmi.javamodel.JavaClass;42 //import org.netbeans.jmi.javamodel.Method;43 //import org.netbeans.jmi.javamodel.Type;44 //import org.netbeans.modules.dbschema.ColumnElement;45 //import org.netbeans.modules.dbschema.TableElement;46 //import org.netbeans.modules.j2ee.common.DatasourceHelper;47 //import org.netbeans.modules.j2ee.deployment.common.api.Datasource;48 //import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;49 //import org.netbeans.modules.j2ee.metadata.JMIClassIntrospector;50 //import org.netbeans.modules.j2ee.persistence.dd.orm.model_1_0.Entity;51 //import org.netbeans.modules.j2ee.persistence.dd.orm.model_1_0.EntityMappings;52 //import org.netbeans.modules.j2ee.persistence.dd.orm.model_1_0.ManyToMany;53 //import org.netbeans.modules.j2ee.persistence.dd.orm.model_1_0.ManyToOne;54 //import org.netbeans.modules.j2ee.persistence.dd.orm.model_1_0.OneToMany;55 //import org.netbeans.modules.j2ee.persistence.dd.orm.model_1_0.OneToOne;56 //import org.netbeans.modules.j2ee.persistence.dd.orm.model_1_0.SecondaryTable;57 //import org.netbeans.modules.j2ee.persistence.dd.orm.model_1_0.Table;58 //import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.PersistenceUnit;59 //import org.netbeans.modules.j2ee.persistence.editor.completion.AnnotationUtils;60 //import org.netbeans.modules.j2ee.persistence.editor.completion.CompletionContextResolver;61 //import org.netbeans.modules.j2ee.persistence.editor.completion.JMIUtils;62 //import org.netbeans.modules.j2ee.persistence.editor.completion.NNCompletionQuery;63 //import org.netbeans.modules.j2ee.persistence.editor.completion.NNParser;64 //import org.netbeans.modules.j2ee.persistence.editor.completion.NNResultItem;65 //import org.netbeans.modules.j2ee.persistence.dd.PersistenceUtils;66 //import org.netbeans.modules.j2ee.persistence.provider.ProviderUtil;67 //import org.netbeans.modules.javacore.JMManager;68 //import org.openide.ErrorManager;69 //import org.openide.awt.StatusDisplayer;70 //import org.openide.filesystems.FileObject;71 //import org.openide.util.NbBundle;72 //73 ///**74 // *75 // * @author Marek Fukala76 // */77 // TODO: RETOUCHE78 //public class DBCompletionContextResolver implements CompletionContextResolver {79 // 80 // private DatabaseConnection dbconn;81 // private DBMetaDataProvider provider;82 // 83 // //annotations names handled somehow by this completion context resolver84 // private static final String[] ANNOTATION_QUERY_TYPES = {85 // "Table", //086 // "SecondaryTable", //187 // "Column", //288 // "PrimaryKeyJoinColumn", //389 // "JoinColumn", //490 // "JoinTable", //591 // "PersistenceUnit", //692 // "PersistenceContext", //793 // "ManyToMany"//894 // };95 // 96 // private static final String PERSISTENCE_PKG = "javax.persistence";97 // 98 // public List resolve(JCExpression exp, NNCompletionQuery.Context ctx) {99 // 100 // List<NNResultItem> result = new ResultItemsFilterList(ctx);101 // 102 // //parse the annotation103 // NNParser.NN parsedNN = ctx.getParsedAnnotation();104 // if (parsedNN == null) return result;105 // 106 // NNParser.NNAttr nnattr = parsedNN.getAttributeForOffset(ctx.getCompletionOffset());107 // if(nnattr == null) return result;108 // 109 // String annotationName = parsedNN.getName();110 // if(annotationName == null) return result;111 // 112 // try {113 // //get nn index from the nn list114 // int index = getAnnotationIndex(annotationName);115 // if(index == 6 || index == 7) {116 // //we do not need database connection for PU completion117 // completePersistenceUnitContext(ctx, parsedNN, nnattr, result);118 // } else if(index != -1) {119 // //the completion has been invoked in supported annotation and there is no db connection initialized yet120 // //try to init the database connection121 // dbconn = findDatabaseConnection(ctx);122 // if(dbconn != null) {123 // // DatabaseConnection.getJDBCConnection() unfortunately acquires Children.MUTEX read access;124 // // it should not be called in a MDR transaction, as this is deadlock-prone125 // assert Thread.currentThread() != JMManager.getTransactionMutex().getThread();126 // 127 // Connection conn = dbconn.getJDBCConnection();128 // if(conn != null) {129 // this.provider = getDBMetadataProvider(dbconn, conn);130 // } else {131 // //Database connection not established ->132 // //put 'connect' CC item133 // result = new ArrayList();134 // result.add(new NNResultItem.NoConnectionElementItem(dbconn));135 // return result;136 // }137 // } else {138 // //no database connection -> give up139 // ErrorManager.getDefault().log("No Database Connection.");140 // return result;141 // }142 // }143 // 144 // //test if the initialization of DB and DBMetadataProvider has succeeded145 // if(this.provider != null) {146 // //and retrieve the CC items under MDR transaction147 // JMIUtils utils = JMIUtils.get(ctx.getBaseDocument());148 // utils.beginTrans(false);149 // try {150 // ((JMManager) JMManager.getManager()).setSafeTrans(true);151 // switch(index) {152 // case 0:153 // completeTable(parsedNN, nnattr, result, false);//Table154 // break;155 // case 5: //JoinTable156 // case 1:157 // completeTable(parsedNN, nnattr, result, true);//SecondaryTable158 // break;159 // case 2:160 // completeColumn(ctx, parsedNN, nnattr, result);//Column161 // break;162 // case 3:163 // completePrimaryKeyJoinColumn(ctx, parsedNN, nnattr, result);164 // break;165 // case 4:166 // completeJoinColumn(ctx, parsedNN, nnattr, result); //JoinColumn167 // break;168 // case 8:169 // completeManyToMany(ctx, parsedNN, nnattr, result);170 // }171 // } finally {172 // utils.endTrans(false);173 // }174 // }175 // 176 // } catch (SQLException e) {177 // ErrorManager.getDefault().notify(ErrorManager.WARNING, e);178 // }179 // 180 // return result;181 // }182 // 183 // /** @return index of the annotation type qhich is going to be queried or -1 if no such annotation found. */184 // private int getAnnotationIndex(String annotationName) {185 // if(annotationName.startsWith(PERSISTENCE_PKG)) {186 // //cut off the package187 // annotationName = annotationName.substring(annotationName.lastIndexOf('.') + 1);188 // }189 // for(int i = 0; i < ANNOTATION_QUERY_TYPES.length; i++) {190 // if(ANNOTATION_QUERY_TYPES[i].equals(annotationName)) return i;191 // }192 // return -1;193 // }194 // 195 // private DBMetaDataProvider getDBMetadataProvider(DatabaseConnection dbconn, Connection con) {196 // return DBMetaDataProvider.get(con, dbconn.getDriverClass());197 // }198 // 199 // private DatabaseConnection findDatabaseConnection(NNCompletionQuery.Context ctx) {200 // PersistenceUnit[] pus = ctx.getPersistenceUnits();201 // if(pus == null || pus.length == 0) {202 // ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "There isn't any defined persistence unit for this class in the project!");203 // return null;204 // }205 // PersistenceUnit pu = pus[0]; // XXX only using the first persistence unit206 // 207 // // try to find a connection specified using the PU properties208 // DatabaseConnection dbconn = ProviderUtil.getConnection(pu);209 // if (dbconn != null) {210 // return dbconn;211 // }212 // 213 // // try to find a datasource-based connection, but only for a FileObject-based context,214 // // otherwise we don't have a J2eeModuleProvider to retrieve the DS's from215 // String datasourceName = ProviderUtil.getDatasourceName(pu);216 // if (datasourceName == null) {217 // return null;218 // }219 // FileObject fo = ctx.getFileObject();220 // if (fo == null) {221 // return null;222 // }223 // Project project = FileOwnerQuery.getOwner(fo);224 // if (project == null) {225 // return null;226 // }227 // J2eeModuleProvider moduleProvider = (J2eeModuleProvider)project.getLookup().lookup(J2eeModuleProvider.class);228 // if (moduleProvider == null) {229 // return null;230 // }231 // Datasource datasource = DatasourceHelper.findDatasource(moduleProvider, datasourceName);232 // if (datasource == null) {233 // ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "The " + datasourceName + " was not found."); // NOI18N234 // return null;235 // }236 // List<DatabaseConnection> dbconns = DatasourceHelper.findDatabaseConnections(datasource);237 // if (dbconns.size() > 0) {238 // return dbconns.get(0);239 // }240 // return null;241 // }242 // 243 // private List completeTable(NNParser.NN nn, NNParser.NNAttr nnattr, List<NNResultItem> results, boolean secondaryTable) throws SQLException {244 // String completedMember = nnattr.getName();245 // Map<String,Object> members = nn.getAttributes();246 // 247 // if ("catalog".equals(completedMember)) { // NOI18N248 // Catalog[] catalogs = provider.getCatalogs();249 // for (int i = 0; i < catalogs.length; i++) {250 // String catalogName = catalogs[i].getName();251 // if (catalogName != null) {252 // results.add(new NNResultItem.CatalogElementItem(catalogName, nnattr.isValueQuoted(), nnattr.getValueOffset()));253 // }254 // }255 // } else if ("schema".equals(completedMember)) { // NOI18N256 // String catalogName = getThisOrDefaultCatalog((String)members.get("catalog")); // NOI18N257 // Catalog catalog = provider.getCatalog(catalogName);258 // if (catalog != null) {259 // Schema[] schemas = catalog.getSchemas();260 // for (int i = 0; i < schemas.length; i++) {261 // results.add(new NNResultItem.SchemaElementItem(schemas[i].getName(), nnattr.isValueQuoted(), nnattr.getValueOffset()));262 // }263 // }264 // } else if ("name".equals(completedMember)) { // NOI18N265 // String catalogName = getThisOrDefaultCatalog((String)members.get("catalog")); // NOI18N266 // String schemaName = getThisOrDefaultSchema((String)members.get("schema")); // NOI18N267 // Schema schema = DBMetaDataUtils.getSchema(provider, catalogName, schemaName);268 // if (schema != null) {269 // String[] tableNames = schema.getTableNames();270 // for (int i = 0; i < tableNames.length; i++) {271 // results.add(new NNResultItem.TableElementItem(tableNames[i], nnattr.isValueQuoted(), nnattr.getValueOffset()));272 // }273 // }274 // }275 // return results;276 // }277 // 278 // private List completePrimaryKeyJoinColumn(NNCompletionQuery.Context ctx, NNParser.NN nn, NNParser.NNAttr nnattr, List<NNResultItem> results) throws SQLException {279 // String completedMember = nnattr.getName();280 // Map<String,Object> members = nn.getAttributes();281 // 282 // if ("name".equals(completedMember)) { // NOI18N283 // //XXX should I take into account the @SecondaryTable here???284 // Entity entity = PersistenceUtils.getEntity(ctx.getJavaClass(), ctx.getEntityMappings());285 // if(entity != null) {286 // Table table = entity.getTable();287 // if(table != null) {288 // String tableName = table.getName();289 // if(tableName != null) {290 // String catalogName = getThisOrDefaultCatalog(table.getCatalog());291 // String schemaName = getThisOrDefaultSchema(table.getSchema());292 // //if(DEBUG) System.out.println("Columns for " + catalogName + "." + schemaName + "." + tableName);293 // TableElement tableElement = DBMetaDataUtils.getTable(provider, catalogName, schemaName, tableName);294 // if(tableElement != null) {295 // ColumnElement[] columnElements = tableElement.getColumns();296 // for (int i = 0; i < columnElements.length; i++) {297 // results.add(new NNResultItem.ColumnElementItem(columnElements[i].getName().getName(), tableName, true, -1));298 // }299 // }300 // }301 // }302 // }303 // }304 // 305 // return results;306 // }307 // 308 // 309 // private List completeColumn(NNCompletionQuery.Context ctx, NNParser.NN nn, NNParser.NNAttr nnattr, List<NNResultItem> results) throws SQLException {310 // String completedMember = nnattr.getName();311 // Map<String,Object> members = nn.getAttributes();312 // 313 // if ("table".equals(completedMember)) { // NOI18N314 // Set/*<String>*/ mappingTables = getMappingEntityTableNames(ctx.getJavaClass());315 // for (Iterator i = mappingTables.iterator(); i.hasNext();) {316 // String tableName = (String)i.next();317 // results.add(new NNResultItem.TableElementItem(tableName, nnattr.isValueQuoted(), nnattr.getValueOffset()));318 // }319 // }320 // if ("name".equals(completedMember)) { // NOI18N321 // String catalogName = null;322 // String schemaName = null;323 // String tableName = (String)members.get("table"); // NOI18N324 // 325 // if (tableName == null) {326 // //no table attribute provided327 // //get the columns from @Table and @SecondaryTable(s) annotations328 // Entity entity = PersistenceUtils.getEntity(ctx.getJavaClass(), ctx.getEntityMappings());329 // if(entity != null) {330 // Table table = entity.getTable();331 // if(table != null) {332 // //the entity has table defined333 // tableName = table.getName();334 // if(tableName != null) {335 // catalogName = getThisOrDefaultCatalog(table.getCatalog());336 // schemaName = getThisOrDefaultSchema(table.getSchema());337 // TableElement tableElement = DBMetaDataUtils.getTable(provider, catalogName, schemaName, tableName);338 // if(tableElement != null) {339 // ColumnElement[] columnElements = tableElement.getColumns();340 // for (int i = 0; i < columnElements.length; i++) {341 // results.add(new NNResultItem.ColumnElementItem(columnElements[i].getName().getName(), tableName, nnattr.isValueQuoted(), nnattr.getValueOffset()));342 // }343 // }344 // }345 // }346 // SecondaryTable[] stables = entity.getSecondaryTable();347 // if(stables != null) {348 // for(int idx = 0; idx < stables.length; idx++) {349 // String secTableName = stables[idx].getName();350 // TableElement tableElement = DBMetaDataUtils.getTable(provider, catalogName, schemaName, secTableName);351 // if(tableElement != null) {352 // ColumnElement[] columnElements = tableElement.getColumns();353 // for (int i = 0; i < columnElements.length; i++) {354 // results.add(new NNResultItem.ColumnElementItem(columnElements[i].getName().getName(), tableName, nnattr.isValueQuoted(), nnattr.getValueOffset()));355 // }356 // }357 // }358 // }359 // }360 // } else {361 // //table attribute of @Column annotation provided362 // catalogName = getThisOrDefaultCatalog(catalogName);363 // schemaName = getThisOrDefaultSchema(schemaName);364 // TableElement tableElement = DBMetaDataUtils.getTable(provider, catalogName, schemaName, tableName);365 // if(tableElement != null) {366 // ColumnElement[] columnElements = tableElement.getColumns();367 // for (int i = 0; i < columnElements.length; i++) {368 // results.add(new NNResultItem.ColumnElementItem(columnElements[i].getName().getName(), tableName, nnattr.isValueQuoted(), nnattr.getValueOffset()));369 // }370 // }371 // }372 // }373 // 374 // return results;375 // }376 // 377 // private List completeJoinColumn(NNCompletionQuery.Context ctx, NNParser.NN nn, NNParser.NNAttr nnattr, List<NNResultItem> results) throws SQLException {378 // String completedMember = nnattr.getName();379 // Map<String,Object> members = nn.getAttributes();380 // 381 // if ("name".equals(completedMember)) { // NOI18N382 // //I need to get @Table annotation to get know which table is primary for this class383 // //XXX should I take into account the @SecondaryTable here???384 // Entity entity = PersistenceUtils.getEntity(ctx.getJavaClass(), ctx.getEntityMappings());385 // //TODO it is bad - since we should allow the CC to complete even the class is not "Entity"386 // if(entity != null && entity.getAttributes() != null) {387 // String propertyName = ctx.getCompletedMemberName();388 // String resolvedClassName = ctx.getCompletedMemberClassName();389 //// JCClass resolvedClass = ctx.getSyntaxSupport().getClassFromName(resolvedClassName, true);390 // Type type = ctx.getSyntaxSupport().getTypeFromName(resolvedClassName, false, null, false);391 // 392 // if(type == null) {393 // //show an error message394 // StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(NNCompletionQuery.class, "MSG_CannotFindClass", new Object[]{resolvedClassName, propertyName}));395 // return Collections.EMPTY_LIST;396 // }397 // 398 // String resolvedType = type.getName();399 // 400 // if(DEBUG) System.out.println("completion called on property " + propertyName + " of " + resolvedType + " type.");401 // 402 // EntityMappings em = ctx.getEntityMappings();403 // 404 // //set is in the declared class405 // ManyToOne[] m2o = entity.getAttributes().getManyToOne();406 // OneToOne[] o2o = entity.getAttributes().getOneToOne();407 // 408 // //set is in the declaring class409 // OneToMany[] o2m = entity.getAttributes().getOneToMany();410 // //set is in both refered and declaring class411 // ManyToMany[] m2m = entity.getAttributes().getManyToMany();412 // 413 // ManyToOne m2onn = null;414 // if(m2o != null) {415 // for(int i = 0; i < m2o.length; i++) {416 // if(m2o[i].getName().equals(propertyName)) {417 // m2onn = m2o[i];418 // break;419 // }420 // }421 // }422 // OneToOne o2onn = null;423 // if(o2o != null) {424 // for(int i = 0; i < o2o.length; i++) {425 // if(o2o[i].getName().equals(propertyName)) {426 // o2onn = o2o[i];427 // break;428 // }429 // }430 // }431 // 432 // OneToMany o2mnn = null;433 // if(o2m != null) {434 // for(int i = 0; i < o2m.length; i++) {435 // if(o2m[i].getName().equals(propertyName)) {436 // o2mnn = o2m[i];437 // break;438 // }439 // }440 // }441 // 442 // ManyToMany m2mnn = null;443 // if(m2m != null) {444 // for(int i = 0; i < m2m.length; i++) {445 // if(m2m[i].getName().equals(propertyName)) {446 // m2mnn = m2m[i];447 // break;448 // }449 // }450 // }451 // 452 // 453 // if(m2onn != null || o2onn != null) {454 // if(DEBUG) System.out.println("found OneToOne or ManyToOne annotation on the completed field.");455 // //OneToOne or ManyToOne456 // //find the entity according to the type of the referred object457 // Entity ent = PersistenceUtils.getEntity(resolvedType, ctx.getEntityMappings());458 // 459 // //also check whether the entity is explicitly determined by "targetEntity"460 // //attribute of the OneToOne or ManyToOne annotations461 // if(m2onn != null) {462 // String targetEntity = m2onn.getTargetEntity();463 // if(targetEntity != null) {464 // ent = PersistenceUtils.getEntity(targetEntity, em);465 // if(DEBUG) System.out.println("entity " + ent.getName() + " is specified in ManyToOne element.");466 // }467 // }468 // if(o2onn != null) {469 // String targetEntity = o2onn.getTargetEntity();470 // if(targetEntity != null) {471 // ent = PersistenceUtils.getEntity(targetEntity, em);472 // if(DEBUG) System.out.println("entity " + ent.getName() + " is specified in OneToOne element.");473 // }474 // }475 // 476 // if(ent != null) {477 // Table table = ent.getTable();478 // if(table != null) {479 // String catalogName = getThisOrDefaultCatalog(null); //XXX need to provide correct data480 // String schemaName = getThisOrDefaultSchema(null);//XXX need to provide correct data481 // String tableName = table.getName();482 // if(tableName != null) {483 // TableElement tableElement = DBMetaDataUtils.getTable(provider, catalogName, schemaName, tableName);484 // if(tableElement != null) {485 // ColumnElement[] columnElements = tableElement.getColumns();486 // for (int i = 0; i < columnElements.length; i++) {487 // results.add(new NNResultItem.ColumnElementItem(columnElements[i].getName().getName(), tableName, nnattr.isValueQuoted(), nnattr.getValueOffset()));488 // }489 // if(DEBUG) System.out.println("added " +columnElements.length + " CC items.");490 // }491 // }492 // } else {493 // if(DEBUG) System.out.println("the found entity has not defined table!?! (probably a bug in values defaultter).");494 // }495 // }496 // 497 // }498 // 499 // //the @JoinTable doesn't make sense for @OneToMany500 // 501 // if(m2mnn != null) {502 // if(DEBUG) System.out.println("found ManyToMany annotation on the completed field.");503 // //the column names in this case needs to be gotten from the surrounding @JoinTable annotation504 // //using of the model doesn't make much sense here because once we complete @JoinColumn inside505 // //a @JoinTable the @JoinTable must be present in the source506 // 507 // //gettting the annotations structure from own simple parser508 // 509 // if(DEBUG) System.out.println(nn);510 // 511 // NNParser.NN tblNN = null;512 // if(nn != null && nn.getName().equals("JoinTable")) { //NOI18N513 // Map attrs = nn.getAttributes();514 // Object val = attrs.get("table"); //NOI18N515 // if(val != null && val instanceof NNParser.NN) {516 // NNParser.NN tableNN = (NNParser.NN)val;517 // if(tableNN.getName().equals("Table")) {//NOI18N518 // tblNN = tableNN;519 // }520 // }521 // }522 // 523 // if(tblNN != null) {524 // String catalogName = getThisOrDefaultCatalog((String)tblNN.getAttributes().get("catalog")); //XXX need to provide correct data525 // String schemaName = getThisOrDefaultSchema((String)tblNN.getAttributes().get("schema"));//XXX need to provide correct data526 // String tableName = (String)tblNN.getAttributes().get("name");527 // if(tableName != null) {528 // TableElement tableElement = DBMetaDataUtils.getTable(provider, catalogName, schemaName, tableName);529 // if(tableElement != null) {530 // ColumnElement[] columnElements = tableElement.getColumns();531 // for (int i = 0; i < columnElements.length; i++) {532 // results.add(new NNResultItem.ColumnElementItem(columnElements[i].getName().getName(), tableName, nnattr.isValueQuoted(), nnattr.getValueOffset()));533 // }534 // }535 // }536 // }537 // }538 // }539 // }540 // 541 // return results;542 // }543 // 544 // private List completePersistenceUnitContext(NNCompletionQuery.Context ctx, NNParser.NN nn, NNParser.NNAttr nnattr, List<NNResultItem> results) throws SQLException {545 // String completedMember = nnattr.getName();546 // Map<String,Object> members = nn.getAttributes();547 // 548 // if ("unitName".equals(completedMember)) { // NOI18N549 // PersistenceUnit[] pus = ctx.getPersistenceUnits();550 // for (PersistenceUnit pu : pus) {551 // results.add(new NNResultItem.PersistenceUnitElementItem(pu.getName(), nnattr.isValueQuoted(), nnattr.getValueOffset()));552 // }553 // }554 // 555 // return results;556 // }557 // 558 // private List completeManyToMany(NNCompletionQuery.Context ctx, NNParser.NN nn, NNParser.NNAttr nnattr, List<NNResultItem> results) throws SQLException {559 // String completedMember = nnattr.getName();560 // Map<String,Object> members = nn.getAttributes();561 // 562 // if ("mappedBy".equals(completedMember)) { // NOI18N563 // String resolvedClassName = ctx.getCompletedMemberClassName();564 // Type type = ctx.getSyntaxSupport().getTypeFromName(resolvedClassName, false, null, false);565 // if(type != null) {566 // Entity entity = PersistenceUtils.getEntity(type.getName(), ctx.getEntityMappings());567 // if(entity != null) {568 // //the class is entity => get all its properties569 // ClassDefinition cdef = (ClassDefinition)type;570 // for(Feature f : (List<Feature>)cdef.getFeatures()) {571 // if(f instanceof Field) {572 // if(!Modifier.isTransient(f.getModifiers())) {573 // if(cdef.getMethod("get" + JMIClassIntrospector.capitalize(f.getName()), Collections.EMPTY_LIST, true) == null //NOI18N574 // && cdef.getMethod("is" + JMIClassIntrospector.capitalize(f.getName()), Collections.EMPTY_LIST, true) == null ) { //NOI18N575 // //there is not getter for this field, add CC item576 // results.add(new NNResultItem.EntityPropertyElementItem(f.getName(), nnattr.isValueQuoted(), nnattr.getValueOffset()));577 // }578 // }579 // } else if (f instanceof Method) {580 // if(JMIClassIntrospector.isGetter((Method)f)) {581 // results.add(new NNResultItem.EntityPropertyElementItem(JMIClassIntrospector.getPropertyNameFromGetter((Method)f), nnattr.isValueQuoted(), nnattr.getValueOffset()));582 // }583 // }584 // }585 // 586 // }587 // }588 // 589 // 590 // }591 // 592 // return results;593 // }594 // 595 // 596 // /**597 // * Returns the tables to which this class is mapped.598 // */599 // private Set/*<String>*/ getMappingEntityTableNames(JavaClass clazz) {600 // Set result = new TreeSet();601 // List/*<Annotation>*/ annotations = clazz.getAnnotations();602 // 603 // for (Iterator i = annotations.iterator(); i.hasNext();) {604 // Annotation annotation = (Annotation)i.next();605 // String annotationTypeName = annotation.getType().getName();606 // 607 // if ("javax.persistence.Table".equals(annotationTypeName)) { // NOI18N608 // String tableName = AnnotationUtils.getStringMemberValue(annotation, "name"); // NOI18N609 // if (tableName != null) {610 // result.add(tableName);611 // }612 // } else if ("javax.persistence.SecondaryTable".equals(annotationTypeName)) { // NOI18N613 // String tableName = AnnotationUtils.getStringMemberValue(annotation, "name"); // NOI18N614 // if (tableName != null) {615 // result.add(tableName);616 // }617 // } else if ("javax.persistence.SecondaryTables".equals(annotationTypeName)) { // NOI18N618 // List secondaryTableNNs = AnnotationUtils.getAnnotationsMemberValue(annotation, "value"); // NOI18N619 // for (Iterator j = secondaryTableNNs.iterator(); j.hasNext();) {620 // Annotation secondaryTableNN = (Annotation)j.next();621 // String tableName = AnnotationUtils.getStringMemberValue(secondaryTableNN, "name"); // NOI18N622 // }623 // }624 // }625 // 626 // return result;627 // }628 // 629 // private String getThisOrDefaultCatalog(String catalogName) throws SQLException {630 // assert provider != null;631 // if (catalogName != null) {632 // return catalogName;633 // } else {634 // return provider.getDefaultCatalog();635 // }636 // }637 // 638 // private String getThisOrDefaultSchema(String schemaName) {639 // assert dbconn != null;640 // if (schemaName != null) {641 // return schemaName;642 // } else {643 // // XXX this may be wrong, the persistence provider would use644 // // the default connection's schema as gived by the database server645 // return dbconn.getSchema();646 // }647 // }648 // 649 // private String getAnnotationTypeName(JCExpression exp) {650 // assert exp != null;651 // 652 // String result = null;653 // 654 // if (exp.getParameterCount() < 1) {655 // return result;656 // }657 // JCExpression variable = exp.getParameter(0);658 // if (variable.getExpID() == JCExpression.VARIABLE) {659 // //just @Table (without package specification)660 // return variable.getTokenText(0);661 // }662 // 663 // if(variable.getExpID() == JCExpression.DOT) {664 // //@javax.persistence.Table (with package specification)665 // StringBuffer sb = new StringBuffer();666 // for(int i = 0; i < variable.getParameterCount(); i++) {667 // JCExpression subExp = variable.getParameter(i);668 // sb.append(subExp.getTokenText(0));669 // if(i < variable.getParameterCount() - 1) {670 // sb.append('.');671 // }672 // }673 // return sb.toString();674 // }675 // 676 // // XXX this does not count with an annotation type written like "javax.persistence.Table"677 // // should try to resolve the annotation type678 // 679 // return result;680 // }681 // 682 // 683 // private static final class ResultItemsFilterList extends ArrayList {684 // private NNCompletionQuery.Context ctx;685 // public ResultItemsFilterList(NNCompletionQuery.Context ctx) {686 // super();687 // this.ctx = ctx;688 // }689 // 690 // public boolean add(Object o) {691 // if(!(o instanceof NNResultItem)) return false;692 // 693 // NNResultItem ri = (NNResultItem)o;694 // //check if the pretext corresponds to the result item text695 // try {696 // String preText = ctx.getBaseDocument().getText(ri.getSubstituteOffset(), ctx.getCompletionOffset() - ri.getSubstituteOffset());697 // if(ri.getItemText().startsWith(preText)) {698 // return super.add(ri);699 // }700 // }catch(BadLocationException ble) {701 // //ignore702 // }703 // return false;704 // }705 // }706 // 707 // private static final boolean DEBUG = Boolean.getBoolean("debug." + DBCompletionContextResolver.class.getName());708 //}709