001    /**
002     * Copyright 2010-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.common.util.execute;
017    
018    import java.io.File;
019    import java.io.IOException;
020    import java.util.ArrayList;
021    import java.util.Arrays;
022    import java.util.List;
023    
024    import org.apache.commons.io.FileUtils;
025    import org.kuali.common.util.LocationUtils;
026    import org.springframework.util.Assert;
027    
028    public class ConvertTableListingTextFileExecutable implements Executable {
029    
030            private static String SQL = "sql";
031            private static String METAINF = "META-INF";
032    
033            List<String> vendors = Arrays.asList("mysql", "oracle");
034            String suffix = SQL;
035            String prefix = SQL;
036            String artifactId;
037            File outputDir;
038            boolean skip;
039    
040            @Override
041            public void execute() {
042                    Assert.notNull(artifactId, "artifactId is null");
043                    Assert.notNull(vendors, "vendors is null");
044                    Assert.notNull(suffix, "suffix is null");
045                    Assert.notNull(prefix, "prefix is null");
046                    Assert.notNull(outputDir, "outputDir is null");
047    
048                    for (String vendor : vendors) {
049                            String tableListing = SQL + "/" + vendor + "/" + artifactId + "-tables.txt";
050                            List<String> tableNames = LocationUtils.readLines(tableListing);
051                            List<String> resources = getResources(tableNames, vendor, prefix, suffix);
052                            validateResources(resources);
053                            File outputFile = getOutputFile(outputDir, prefix, vendor, artifactId);
054                            writeLines(outputFile, resources);
055                    }
056            }
057    
058            protected void validateResources(List<String> resources) {
059                    for (String resource : resources) {
060                            Assert.isTrue(LocationUtils.exists(resource), "[" + resource + "] does not exist");
061                    }
062            }
063    
064            protected void writeLines(File file, List<String> lines) {
065                    try {
066                            FileUtils.writeLines(file, lines, "\n");
067                    } catch (IOException e) {
068                            throw new IllegalArgumentException(e);
069                    }
070            }
071    
072            protected File getOutputFile(File outputDir, String prefix, String vendor, String artifactId) {
073                    String filename = METAINF + "/" + prefix + "/" + vendor + "/" + artifactId + "-data.resources";
074                    return new File(outputDir, filename);
075    
076            }
077    
078            protected List<String> getResources(List<String> tableNames, String vendor, String prefix, String suffix) {
079                    List<String> resources = new ArrayList<String>();
080                    for (String tableName : tableNames) {
081                            String resource = "classpath:" + prefix + "/" + vendor + "/" + tableName + "." + suffix;
082                            resources.add(resource);
083                    }
084                    return resources;
085            }
086    
087            public List<String> getVendors() {
088                    return vendors;
089            }
090    
091            public void setVendors(List<String> vendors) {
092                    this.vendors = vendors;
093            }
094    
095            public String getSuffix() {
096                    return suffix;
097            }
098    
099            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    }