1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.impex.schema.service.impl;
17
18 import java.sql.SQLException;
19 import java.util.List;
20
21 import org.kuali.common.impex.model.ForeignKey;
22 import org.kuali.common.impex.model.Sequence;
23 import org.kuali.common.impex.model.Table;
24 import org.kuali.common.impex.model.View;
25 import org.kuali.common.impex.schema.service.ExtractSchemaService;
26 import org.kuali.common.threads.ElementHandler;
27 import org.kuali.common.threads.ListIteratorContext;
28
29 public class ExtractSchemaBucketHandler implements ElementHandler<ExtractSchemaBucket> {
30
31 public static final ExtractSchemaService DEFAULT_SERVICE = new DefaultExtractSchemaService();
32
33 ExtractSchemaService service = DEFAULT_SERVICE;
34
35 public ExtractSchemaBucketHandler() {
36 this(DEFAULT_SERVICE);
37 }
38
39 public ExtractSchemaBucketHandler(ExtractSchemaService service) {
40 this.service = service;
41 }
42
43 @Override
44 public void handleElement(ListIteratorContext<ExtractSchemaBucket> context, int index, ExtractSchemaBucket element) {
45
46 if (element instanceof ExtractViewsAndSequencesBucket) {
47 doViewsAndSequences(element);
48 } else {
49 doTablesAndForeignKeys(element);
50 }
51 }
52
53 protected void doTablesAndForeignKeys(ExtractSchemaBucket bucket) {
54 try {
55 List<Table> tables = service.extractTables(bucket.getTableNames(), bucket.getContext());
56 List<ForeignKey> foreignKeys = service.extractForeignKeys(bucket.getTableNames(), bucket.getContext());
57 synchronized (bucket.getSchema()) {
58 bucket.getSchema().getTables().addAll(tables);
59 bucket.getSchema().getForeignKeys().addAll(foreignKeys);
60 }
61 } catch (SQLException e) {
62 throw new IllegalStateException("Unexpected SQL error");
63 }
64
65 }
66
67 protected void doViewsAndSequences(ExtractSchemaBucket bucket) {
68 try {
69 List<View> views = service.extractViews(bucket.getContext());
70 bucket.getInformer().incrementProgress();
71
72 List<Sequence> sequences = service.extractSequences(bucket.getContext());
73 bucket.getInformer().incrementProgress();
74
75 synchronized (bucket.getSchema()) {
76 bucket.getSchema().getViews().addAll(views);
77 bucket.getSchema().getSequences().addAll(sequences);
78 }
79 } catch (SQLException e) {
80 throw new IllegalStateException("Unexpected SQL error");
81 }
82 }
83 }