001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.tools;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
018    import com.liferay.portal.kernel.util.ReleaseInfo;
019    import com.liferay.portal.kernel.util.StringBundler;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.tools.servicebuilder.ServiceBuilder;
022    import com.liferay.portal.util.FileImpl;
023    
024    import java.io.File;
025    import java.io.FileInputStream;
026    import java.io.InputStreamReader;
027    
028    import java.util.ArrayList;
029    import java.util.List;
030    
031    import org.apache.tools.ant.DirectoryScanner;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public class UpgradeTableBuilder {
037    
038            public static void main(String[] args) {
039                    try {
040                            new UpgradeTableBuilder(args[0]);
041                    }
042                    catch (Exception e) {
043                            e.printStackTrace();
044                    }
045            }
046    
047            public UpgradeTableBuilder(String upgradeTableDir) throws Exception {
048                    DirectoryScanner ds = new DirectoryScanner();
049    
050                    ds.setBasedir(".");
051                    ds.setIncludes(new String[] {"**\\upgrade\\v**\\util\\*Table.java"});
052    
053                    ds.scan();
054    
055                    String[] fileNames = ds.getIncludedFiles();
056    
057                    for (String fileName : fileNames) {
058                            fileName = StringUtil.replace(fileName, "\\", "/");
059    
060                            int x = fileName.indexOf("upgrade/v");
061                            int y = fileName.indexOf("/util", x);
062    
063                            String version = StringUtil.replace(
064                                    fileName.substring(x + 9, y), "_", ".");
065    
066                            String upgradeFileVersion = version;
067    
068                            int z = upgradeFileVersion.indexOf(".to.");
069    
070                            if (z != -1) {
071                                    upgradeFileVersion = upgradeFileVersion.substring(z + 4);
072                            }
073    
074                            x = fileName.indexOf("/", y + 1);
075                            y = fileName.indexOf("Table.java", x);
076    
077                            String upgradeFileName =
078                                    upgradeTableDir + "/" + upgradeFileVersion + "/" +
079                                            fileName.substring(x, y) + "ModelImpl.java";
080    
081                            if (!_fileUtil.exists(upgradeFileName)) {
082                                    if (!upgradeFileVersion.equals(ReleaseInfo.getVersion())) {
083                                            continue;
084                                    }
085    
086                                    upgradeFileName = _findUpgradeFileName(
087                                            fileName.substring(x, y));
088    
089                                    if (upgradeFileName == null) {
090                                            continue;
091                                    }
092                            }
093    
094                            String content = _fileUtil.read(upgradeFileName);
095    
096                            String packagePath =
097                                    "com.liferay.portal.upgrade.v" +
098                                            StringUtil.replace(version, ".", "_") + ".util";
099                            String className = fileName.substring(x + 1, y) + "Table";
100    
101                            String author = _getAuthor(fileName);
102    
103                            String[] addIndexes = _getAddIndexes(
104                                    upgradeTableDir + "/" + upgradeFileVersion + "/indexes.sql",
105                                    fileName.substring(x + 1, y));
106    
107                            content = _getContent(
108                                    packagePath, className, content, author, addIndexes);
109    
110                            _fileUtil.write(fileName, content);
111                    }
112            }
113    
114            private String _findUpgradeFileName(String modelName) {
115                    DirectoryScanner ds = new DirectoryScanner();
116    
117                    ds.setBasedir(".");
118                    ds.setIncludes(new String[] {"**\\" + modelName + "ModelImpl.java"});
119    
120                    ds.scan();
121    
122                    String[] fileNames = ds.getIncludedFiles();
123    
124                    if (fileNames.length > 0) {
125                            return fileNames[0];
126                    }
127                    else {
128                            return null;
129                    }
130            }
131    
132            private String[] _getAddIndexes(String indexesFileName, String tableName)
133                    throws Exception {
134    
135                    List<String> addIndexes = new ArrayList<String>();
136    
137                    File indexesFile = new File(indexesFileName);
138    
139                    if (!indexesFile.exists()) {
140                            indexesFile = new File("../sql/indexes.sql");
141                    }
142    
143                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
144                            new InputStreamReader(new FileInputStream(indexesFile)));
145    
146                    String line = null;
147    
148                    while ((line = unsyncBufferedReader.readLine()) != null) {
149                            if (line.contains(" on " + tableName + " (") ||
150                                    line.contains(" on " + tableName + "_ (")) {
151    
152                                    String sql = line.trim();
153    
154                                    if (sql.endsWith(";")) {
155                                            sql = sql.substring(0, sql.length() - 1);
156                                    }
157    
158                                    addIndexes.add(sql);
159                            }
160                    }
161    
162                    unsyncBufferedReader.close();
163    
164                    return addIndexes.toArray(new String[addIndexes.size()]);
165            }
166    
167            private String _getAuthor(String fileName) throws Exception {
168                    if (_fileUtil.exists(fileName)) {
169                            String content = _fileUtil.read(fileName);
170    
171                            int x = content.indexOf("* @author ");
172    
173                            if (x != -1) {
174                                    int y = content.indexOf("*", x + 1);
175    
176                                    if (y != -1) {
177                                            return content.substring(x + 10, y).trim();
178                                    }
179                            }
180                    }
181    
182                    return ServiceBuilder.AUTHOR;
183            }
184    
185            private String _getContent(
186                            String packagePath, String className, String content, String author,
187                            String[] addIndexes)
188                    throws Exception {
189    
190                    int x = content.indexOf("public static final String TABLE_NAME =");
191    
192                    if (x == -1) {
193                            x = content.indexOf("public static String TABLE_NAME =");
194                    }
195    
196                    int y = content.indexOf("public static final String TABLE_SQL_DROP =");
197    
198                    if (y == -1) {
199                            y = content.indexOf("public static String TABLE_SQL_DROP =");
200                    }
201    
202                    y = content.indexOf(";", y);
203    
204                    content = content.substring(x, y + 1);
205    
206                    content = StringUtil.replace(
207                            content,
208                            new String[] {
209                                    "\t", "{ \"", "new Integer(Types.", ") }", " }"
210                            },
211                            new String[] {
212                                    "", "{\"", "Types.", "}", "}"
213                            });
214    
215                    while (content.contains("\n\n")) {
216                            content = StringUtil.replace(content, "\n\n", "\n");
217                    }
218    
219                    StringBundler sb = new StringBundler();
220    
221                    sb.append(_fileUtil.read("../copyright.txt"));
222    
223                    sb.append("\n\npackage ");
224                    sb.append(packagePath);
225                    sb.append(";\n\n");
226    
227                    sb.append("import java.sql.Types;\n\n");
228    
229                    sb.append("/**\n");
230                    sb.append(" * @author\t  ");
231                    sb.append(author);
232                    sb.append("\n");
233                    sb.append(" * @generated\n");
234                    sb.append(" */\n");
235                    sb.append("public class ");
236                    sb.append(className);
237                    sb.append(" {\n\n");
238    
239                    String[] lines = StringUtil.splitLines(content);
240    
241                    for (String line : lines) {
242                            if (line.startsWith("public static") ||
243                                    line.startsWith("};")) {
244    
245                                    sb.append("\t");
246                            }
247                            else if (line.startsWith("{\"")) {
248                                    sb.append("\t\t");
249                            }
250    
251                            sb.append(line);
252                            sb.append("\n");
253    
254                            if (line.endsWith(";")) {
255                                    sb.append("\n");
256                            }
257                    }
258    
259                    sb.append("\tpublic static final String[] TABLE_SQL_ADD_INDEXES = {\n");
260    
261                    for (int i = 0; i < addIndexes.length; i++) {
262                            String addIndex = addIndexes[i];
263    
264                            sb.append("\t\t\"");
265                            sb.append(addIndex);
266                            sb.append("\"");
267    
268                            if ((i + 1) < addIndexes.length) {
269                                    sb.append(",");
270                            }
271    
272                            sb.append("\n");
273                    }
274    
275                    sb.append("\t};\n\n");
276    
277                    sb.append("}");
278    
279                    return sb.toString();
280            }
281    
282            private static FileImpl _fileUtil = FileImpl.getInstance();
283    
284    }