1 package com.atlassian.activeobjects.admin.tables;
2
3 import com.atlassian.activeobjects.backup.ActiveObjectsBackup;
4 import com.atlassian.activeobjects.backup.ActiveObjectsTableReader;
5 import com.atlassian.activeobjects.backup.ImportExportErrorServiceImpl;
6 import com.atlassian.activeobjects.backup.PluginInformationFactory;
7 import com.atlassian.activeobjects.internal.DatabaseProviderFactory;
8 import com.atlassian.activeobjects.spi.PluginInformation;
9 import com.atlassian.activeobjects.spi.TenantAwareDataSourceProvider;
10 import com.atlassian.dbexporter.DatabaseInformation;
11 import com.atlassian.dbexporter.Table;
12 import com.atlassian.dbexporter.exporter.TableReader;
13 import com.atlassian.tenancy.api.Tenant;
14 import com.atlassian.tenancy.api.TenantContext;
15 import com.google.common.collect.HashMultimap;
16 import com.google.common.collect.Maps;
17 import com.google.common.collect.Multimap;
18 import net.java.ao.DatabaseProvider;
19 import net.java.ao.schema.NameConverters;
20
21 import static com.google.common.base.Preconditions.checkNotNull;
22
23 public final class TablesController {
24
25 private final DatabaseProviderFactory databaseProviderFactory;
26 private final NameConverters nameConverters;
27 private final TenantAwareDataSourceProvider tenantAwareDataSourceProvider;
28 private final ImportExportErrorServiceImpl errorService;
29 private final PluginInformationFactory pluginInformationFactory;
30 private final TenantContext tenantContext;
31
32 public TablesController(DatabaseProviderFactory databaseProviderFactory,
33 NameConverters nameConverters,
34 TenantAwareDataSourceProvider tenantAwareDataSourceProvider,
35 ImportExportErrorServiceImpl errorService,
36 PluginInformationFactory pluginInformationFactory,
37 TenantContext tenantContext) {
38
39 this.pluginInformationFactory = checkNotNull(pluginInformationFactory);
40 this.nameConverters = checkNotNull(nameConverters);
41 this.databaseProviderFactory = checkNotNull(databaseProviderFactory);
42 this.tenantAwareDataSourceProvider = checkNotNull(tenantAwareDataSourceProvider);
43 this.errorService = checkNotNull(errorService);
44 this.tenantContext = checkNotNull(tenantContext);
45 }
46
47 public Multimap<PluginInformation, TableInformation> list() {
48 final Tenant tenant = tenantContext.getCurrentTenant();
49 final DatabaseProvider databaseProvider = getDatabaseProvider(tenant);
50 final Iterable<Table> tables = readTables(newTableReader(databaseProvider));
51 final RowCounter rowCounter = RowCounter.from(databaseProvider);
52
53 return tablesPerPlugin(tables, rowCounter);
54 }
55
56 private Iterable<Table> readTables(TableReader tableReader) {
57 return tableReader.read(emptyDatabaseInformation(), newEntityNameProcessor());
58 }
59
60 private ActiveObjectsBackup.UpperCaseEntityNameProcessor newEntityNameProcessor() {
61 return new ActiveObjectsBackup.UpperCaseEntityNameProcessor();
62 }
63
64 private DatabaseInformation emptyDatabaseInformation() {
65 return new DatabaseInformation(Maps.<String, String>newHashMap());
66 }
67
68 private ActiveObjectsTableReader newTableReader(DatabaseProvider databaseProvider) {
69 return new ActiveObjectsTableReader(errorService, nameConverters, databaseProvider, ActiveObjectsBackup.schemaConfiguration());
70 }
71
72 private DatabaseProvider getDatabaseProvider(Tenant tenant) {
73 return databaseProviderFactory.getDatabaseProvider(tenantAwareDataSourceProvider.getDataSource(tenant),
74 tenantAwareDataSourceProvider.getDatabaseType(tenant), tenantAwareDataSourceProvider.getSchema(tenant));
75 }
76
77 private Multimap<PluginInformation, TableInformation> tablesPerPlugin(Iterable<Table> tables, final RowCounter rowCounter) {
78 final Multimap<PluginInformation, TableInformation> tablesPerPlugin = HashMultimap.create();
79 for (Table table : tables) {
80 final String tableName = table.getName();
81 tablesPerPlugin.put(newPluginInformation(tableName), newTableInformation(tableName, rowCounter));
82 }
83 return tablesPerPlugin;
84 }
85
86 private PluginInformation newPluginInformation(String tableName) {
87 return pluginInformationFactory.getPluginInformation(tableName);
88 }
89
90 private TableInformation newTableInformation(String tableName, RowCounter rowCounter) {
91 return new TableInformation(tableName, rowCounter.count(tableName));
92 }
93
94 public static final class TableInformation {
95 private final String table;
96 private final String rows;
97
98 public TableInformation(String table, int rows) {
99 this.table = checkNotNull(table);
100 this.rows = String.valueOf(rows);
101 }
102
103 public String getTable() {
104 return table;
105 }
106
107 public String getRows() {
108 return rows;
109 }
110 }
111 }