/** * Indicates whether active record CRUD methods should be generated for the given entities (this * being an all or nothing decision) * * @param database the database being reverse-engineered (required) * @param managedEntities any existing DB-managed entities in the user project (can be <code>null * </code> or empty) * @return see above */ private boolean isActiveRecord( final Database database, final Collection<ClassOrInterfaceTypeDetails> managedEntities) { if (CollectionUtils.isEmpty(managedEntities)) { // There are no existing entities; use the given setting return database.isActiveRecord(); } /* * There are one or more existing entities; preserve the existing * decision, based on the first such entity. This saves the user from * having to enter the same value for the "activeRecord" option each * time they run the database reverse engineer command. */ return managedEntities.iterator().next().getAnnotation(ROO_JPA_ACTIVE_RECORD) != null; }
private void reverseEngineer(final Database database) { final Set<ClassOrInterfaceTypeDetails> managedEntities = typeLocationService.findClassesOrInterfaceDetailsWithAnnotation(ROO_DB_MANAGED); // Determine whether to create "active record" CRUD methods database.setActiveRecord(isActiveRecord(database, managedEntities)); // Lookup the relevant destination package if not explicitly given final JavaPackage destinationPackage = getDestinationPackage(database, managedEntities); // Set the destination package in the database database.setDestinationPackage(destinationPackage); // Get tables from database final Set<Table> tables = new LinkedHashSet<Table>(database.getTables()); // Manage existing entities with @RooDbManaged annotation for (final ClassOrInterfaceTypeDetails managedEntity : managedEntities) { // Remove table from set as each managed entity is processed. // The tables that remain in the set will be used for creation of // new entities later final Table table = updateOrDeleteManagedEntity(managedEntity, database); if (table != null) { tables.remove(table); } } // Create new entities from tables final List<ClassOrInterfaceTypeDetails> newEntities = new ArrayList<ClassOrInterfaceTypeDetails>(); for (final Table table : tables) { // Don't create types from join tables in many-to-many associations if (!table.isJoinTable()) { JavaPackage schemaPackage = destinationPackage; if (database.hasMultipleSchemas()) { schemaPackage = new JavaPackage( destinationPackage.getFullyQualifiedPackageName() + "." + DbreTypeUtils.suggestPackageName(table.getSchema().getName())); } final JavaType javaType = DbreTypeUtils.suggestTypeNameForNewTable(table.getName(), schemaPackage); final boolean activeRecord = database.isActiveRecord() && !database.isRepository(); if (typeLocationService.getTypeDetails(javaType) == null) { table.setIncludeNonPortableAttributes(database.isIncludeNonPortableAttributes()); table.setDisableVersionFields(database.isDisableVersionFields()); table.setDisableGeneratedIdentifiers(database.isDisableGeneratedIdentifiers()); newEntities.add(createNewManagedEntityFromTable(javaType, table, activeRecord)); } } } // Create repositories if required if (database.isRepository()) { for (final ClassOrInterfaceTypeDetails entity : newEntities) { final JavaType type = entity.getType(); repositoryJpaOperations.setupRepository( new JavaType(type.getFullyQualifiedTypeName() + "Repository"), type); } } // Create services if required if (database.isService()) { for (final ClassOrInterfaceTypeDetails entity : newEntities) { final JavaType type = entity.getType(); final String typeName = type.getFullyQualifiedTypeName(); serviceOperations.setupService( new JavaType(typeName + "Service"), new JavaType(typeName + "ServiceImpl"), type, false, "", false, false); } } // Create integration tests if required if (database.isTestAutomatically()) { for (final ClassOrInterfaceTypeDetails entity : newEntities) { integrationTestOperations.newIntegrationTest(entity.getType()); } } // Notify final List<ClassOrInterfaceTypeDetails> allEntities = new ArrayList<ClassOrInterfaceTypeDetails>(); allEntities.addAll(newEntities); allEntities.addAll(managedEntities); notify(allEntities); }