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.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  		// TODO This instanceof check is pretty awful. Do something smarter here
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  }