View Javadoc
1   package com.atlassian.activeobjects.backup;
2   
3   import com.atlassian.dbexporter.EntityNameProcessor;
4   import com.atlassian.dbexporter.ForeignKey;
5   import com.atlassian.dbexporter.ImportExportErrorService;
6   import net.java.ao.DatabaseProvider;
7   import net.java.ao.schema.NameConverters;
8   import net.java.ao.schema.ddl.DDLAction;
9   import net.java.ao.schema.ddl.DDLActionType;
10  import net.java.ao.schema.ddl.DDLForeignKey;
11  import net.java.ao.schema.ddl.SQLAction;
12  
13  import java.sql.Connection;
14  import java.sql.SQLException;
15  import java.sql.Statement;
16  
17  import static com.atlassian.activeobjects.backup.SqlUtils.executeUpdate;
18  import static com.atlassian.dbexporter.jdbc.JdbcUtils.closeQuietly;
19  import static com.google.common.base.Preconditions.checkNotNull;
20  
21  final class ActiveObjectsForeignKeyCreator implements ForeignKeyCreator {
22      private final ImportExportErrorService errorService;
23      private final NameConverters converters;
24      private final DatabaseProvider provider;
25  
26      public ActiveObjectsForeignKeyCreator(ImportExportErrorService errorService, NameConverters converters, DatabaseProvider provider) {
27          this.errorService = checkNotNull(errorService);
28          this.converters = checkNotNull(converters);
29          this.provider = checkNotNull(provider);
30      }
31  
32      public void create(Iterable<ForeignKey> foreignKeys, EntityNameProcessor entityNameProcessor) {
33          Connection conn = null;
34          Statement stmt = null;
35          try {
36              conn = provider.getConnection();
37              stmt = conn.createStatement();
38              for (ForeignKey foreignKey : foreignKeys) {
39                  final DDLAction a = new DDLAction(DDLActionType.ALTER_ADD_KEY);
40                  a.setKey(toDdlForeignKey(foreignKey, entityNameProcessor));
41                  final Iterable<SQLAction> sqlActions = provider.renderAction(converters, a);
42                  for (SQLAction sql : sqlActions) {
43                      executeUpdate(errorService, tableName(a), stmt, sql.getStatement());
44                  }
45              }
46          } catch (SQLException e) {
47              throw errorService.newImportExportSqlException(null, "", e);
48          } finally {
49              closeQuietly(stmt);
50              closeQuietly(conn);
51          }
52      }
53  
54      private String tableName(DDLAction a) {
55          if (a == null) {
56              return null;
57          }
58          if (a.getTable() == null) {
59              return null;
60          }
61          return a.getTable().getName();
62      }
63  
64      private DDLForeignKey toDdlForeignKey(ForeignKey foreignKey, EntityNameProcessor entityNameProcessor) {
65          final DDLForeignKey ddlForeignKey = new DDLForeignKey();
66          ddlForeignKey.setDomesticTable(entityNameProcessor.tableName(foreignKey.getFromTable()));
67          ddlForeignKey.setField(entityNameProcessor.columnName(foreignKey.getFromField()));
68          ddlForeignKey.setTable(entityNameProcessor.tableName(foreignKey.getToTable()));
69          ddlForeignKey.setForeignField(entityNameProcessor.columnName(foreignKey.getToField()));
70          return ddlForeignKey;
71      }
72  }