View Javadoc
1   /**
2    * Copyright 2010-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.util.execute;
17  
18  import java.io.File;
19  import java.io.IOException;
20  import java.util.ArrayList;
21  import java.util.Arrays;
22  import java.util.List;
23  
24  import org.apache.commons.io.FileUtils;
25  import org.kuali.common.util.LocationUtils;
26  import org.springframework.util.Assert;
27  
28  public class ConvertTableListingTextFileExecutable implements Executable {
29  
30  	private static String SQL = "sql";
31  	private static String METAINF = "META-INF";
32  
33  	List<String> vendors = Arrays.asList("mysql", "oracle");
34  	String suffix = SQL;
35  	String prefix = SQL;
36  	String artifactId;
37  	File outputDir;
38  	boolean skip;
39  
40  	@Override
41  	public void execute() {
42  		Assert.notNull(artifactId, "artifactId is null");
43  		Assert.notNull(vendors, "vendors is null");
44  		Assert.notNull(suffix, "suffix is null");
45  		Assert.notNull(prefix, "prefix is null");
46  		Assert.notNull(outputDir, "outputDir is null");
47  
48  		for (String vendor : vendors) {
49  			String tableListing = SQL + "/" + vendor + "/" + artifactId + "-tables.txt";
50  			List<String> tableNames = LocationUtils.readLines(tableListing);
51  			List<String> resources = getResources(tableNames, vendor, prefix, suffix);
52  			validateResources(resources);
53  			File outputFile = getOutputFile(outputDir, prefix, vendor, artifactId);
54  			writeLines(outputFile, resources);
55  		}
56  	}
57  
58  	protected void validateResources(List<String> resources) {
59  		for (String resource : resources) {
60  			Assert.isTrue(LocationUtils.exists(resource), "[" + resource + "] does not exist");
61  		}
62  	}
63  
64  	protected void writeLines(File file, List<String> lines) {
65  		try {
66  			FileUtils.writeLines(file, lines, "\n");
67  		} catch (IOException e) {
68  			throw new IllegalArgumentException(e);
69  		}
70  	}
71  
72  	protected File getOutputFile(File outputDir, String prefix, String vendor, String artifactId) {
73  		String filename = METAINF + "/" + prefix + "/" + vendor + "/" + artifactId + "-data.resources";
74  		return new File(outputDir, filename);
75  
76  	}
77  
78  	protected List<String> getResources(List<String> tableNames, String vendor, String prefix, String suffix) {
79  		List<String> resources = new ArrayList<String>();
80  		for (String tableName : tableNames) {
81  			String resource = "classpath:" + prefix + "/" + vendor + "/" + tableName + "." + suffix;
82  			resources.add(resource);
83  		}
84  		return resources;
85  	}
86  
87  	public List<String> getVendors() {
88  		return vendors;
89  	}
90  
91  	public void setVendors(List<String> vendors) {
92  		this.vendors = vendors;
93  	}
94  
95  	public String getSuffix() {
96  		return suffix;
97  	}
98  
99  	public void setSuffix(String suffix) {
100 		this.suffix = suffix;
101 	}
102 
103 	public String getPrefix() {
104 		return prefix;
105 	}
106 
107 	public void setPrefix(String prefix) {
108 		this.prefix = prefix;
109 	}
110 
111 	public String getArtifactId() {
112 		return artifactId;
113 	}
114 
115 	public void setArtifactId(String artifactId) {
116 		this.artifactId = artifactId;
117 	}
118 
119 	public File getOutputDir() {
120 		return outputDir;
121 	}
122 
123 	public void setOutputDir(File outputDir) {
124 		this.outputDir = outputDir;
125 	}
126 
127 	public boolean isSkip() {
128 		return skip;
129 	}
130 
131 	public void setSkip(boolean skip) {
132 		this.skip = skip;
133 	}
134 
135 }