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.execute;
17  
18  import java.io.IOException;
19  import java.io.Writer;
20  
21  import org.apache.commons.io.IOUtils;
22  import org.kuali.common.impex.model.Schema;
23  import org.kuali.common.impex.schema.DumpSchemaService;
24  import org.kuali.common.util.Assert;
25  import org.kuali.common.util.LocationUtils;
26  import org.kuali.common.util.execute.Executable;
27  
28  public class ModularSchemaExportExecutable implements Executable {
29  
30  	boolean skip;
31  	String outputLocation;
32  	Schema schema;
33  	DumpSchemaService exportService;
34  	boolean separateForeignKeys;
35  	String foreignKeyOutputLocation;
36  
37  	@Override
38  	public void execute() {
39  
40  		if (skip) {
41  			return;
42  		}
43  
44  		Assert.notNull(schema, "schema is null");
45  
46  		if (separateForeignKeys) {
47  
48  			// build a new schema object with just the foreign keys from our source schema
49  			Schema fkSchema = new Schema();
50  			fkSchema.getForeignKeys().addAll(schema.getForeignKeys());
51  
52  			// create a new schema object with all other schema elements
53  			Schema otherSchema = new Schema();
54  			otherSchema.setSequences(schema.getSequences());
55  			otherSchema.setTables(schema.getTables());
56  			otherSchema.setViews(schema.getViews());
57  
58  			writeSchema(otherSchema, outputLocation);
59  
60  			writeSchema(fkSchema, foreignKeyOutputLocation);
61  
62  		} else {
63  			writeSchema(schema, outputLocation);
64  		}
65  
66  	}
67  
68  	protected void writeSchema(Schema outputSchema, String location) {
69  		Writer writer = null;
70  		try {
71  			writer = LocationUtils.openWriter(location);
72  			exportService.dumpSchema(outputSchema, writer);
73  		} catch (IOException e) {
74  			throw new IllegalStateException("Unexpected IO error", e);
75  		} finally {
76  			IOUtils.closeQuietly(writer);
77  		}
78  	}
79  
80  	public boolean isSkip() {
81  		return skip;
82  	}
83  
84  	public void setSkip(boolean skip) {
85  		this.skip = skip;
86  	}
87  
88  	public DumpSchemaService getExportService() {
89  		return exportService;
90  	}
91  
92  	public void setExportService(DumpSchemaService exportService) {
93  		this.exportService = exportService;
94  	}
95  
96  	public String getOutputLocation() {
97  		return outputLocation;
98  	}
99  
100 	public void setOutputLocation(String outputLocation) {
101 		this.outputLocation = outputLocation;
102 	}
103 
104 	public Schema getSchema() {
105 		return schema;
106 	}
107 
108 	public void setSchema(Schema schema) {
109 		this.schema = schema;
110 	}
111 
112 	public boolean isSeparateForeignKeys() {
113 		return separateForeignKeys;
114 	}
115 
116 	public void setSeparateForeignKeys(boolean separateForeignKeys) {
117 		this.separateForeignKeys = separateForeignKeys;
118 	}
119 
120 	public void setForeignKeyOutputLocation(String fkOutpuLocation) {
121 		this.foreignKeyOutputLocation = fkOutpuLocation;
122 	}
123 
124 	public String getForeignKeyOutputLocation() {
125 		return foreignKeyOutputLocation;
126 	}
127 }