View Javadoc

1   /**
2    * Copyright 2011 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.common.impex.schema.service.impl;
17  
18  import java.sql.SQLException;
19  import java.util.Collection;
20  
21  import org.kuali.common.impex.model.ForeignKey;
22  import org.kuali.common.impex.model.Schema;
23  import org.kuali.common.impex.model.Sequence;
24  import org.kuali.common.impex.model.Table;
25  import org.kuali.common.impex.model.View;
26  import org.kuali.common.impex.schema.service.SchemaExtractionContext;
27  import org.kuali.common.impex.schema.service.SchemaExtractionService;
28  import org.kuali.common.threads.ElementHandler;
29  import org.kuali.common.threads.ListIteratorContext;
30  
31  public class ExtractSchemaBucketHandler implements ElementHandler<ExtractSchemaBucket> {
32  
33  	public static final SchemaExtractionService DEFAULT_SERVICE = new DefaultSchemaExtractionService();
34  
35  	SchemaExtractionService service = DEFAULT_SERVICE;
36  
37  	public ExtractSchemaBucketHandler() {
38  		this(DEFAULT_SERVICE);
39  	}
40  
41  	public ExtractSchemaBucketHandler(SchemaExtractionService service) {
42  		this.service = service;
43  	}
44  
45  	@Override
46  	public void handleElement(ListIteratorContext<ExtractSchemaBucket> context, int index, ExtractSchemaBucket element) {
47  		Schema schema = element.getSchema();
48  		SchemaExtractionContext extractionContext = element.getContext();
49  
50  		if (element instanceof ExtractViewsAndSequencesBucket) {
51  			Collection<View> views;
52  			Collection<Sequence> sequences;
53  
54  			try {
55  				views = service.extractViews(extractionContext);
56  			} catch (SQLException e) {
57  				throw new IllegalStateException("Exception thrown by extraction service attempting to extract view metadata: " + e.getMessage(), e);
58  			}
59  
60  			try {
61  				sequences = service.extractSequences(extractionContext);
62  			} catch (SQLException e) {
63  				throw new IllegalStateException("Exception thrown by extraction service attempting to extract sequence metadata: " + e.getMessage(), e);
64  			}
65  
66  			schema.getViews().addAll(views);
67  			schema.getSequences().addAll(sequences);
68  		} else {
69  
70  			Collection<Table> tables;
71  
72  			Collection<ForeignKey> foreignKeys;
73  
74  			try {
75  				tables = service.extractTables(element.getTableNames(), extractionContext);
76  			} catch (SQLException e) {
77  				throw new IllegalStateException("Exception thrown by extraction service attempting to extract table metadata: " + e.getMessage(), e);
78  			}
79  
80  			try {
81  				foreignKeys = service.extractForeignKeys(element.getTableNames(), extractionContext);
82  			} catch (SQLException e) {
83  				throw new IllegalStateException("Exception thrown by extraction service attempting to extract foreign key metadata: " + e.getMessage(), e);
84  			}
85  
86  			schema.getTables().addAll(tables);
87  			schema.getForeignKeys().addAll(foreignKeys);
88  		}
89  	}
90  }