001    /**
002     * Copyright (c) 2000-2010 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.util.StringBundler;
018    import com.liferay.portal.kernel.util.StringUtil;
019    import com.liferay.portal.tools.servicebuilder.ServiceBuilder;
020    import com.liferay.portal.util.FileImpl;
021    
022    import org.apache.tools.ant.DirectoryScanner;
023    
024    /**
025     * @author Brian Wing Shun Chan
026     */
027    public class UpgradeTableBuilder {
028    
029            public static void main(String[] args) {
030                    try {
031                            new UpgradeTableBuilder(args[0]);
032                    }
033                    catch (Exception e) {
034                            e.printStackTrace();
035                    }
036            }
037    
038            public UpgradeTableBuilder(String upgradeTableDir) throws Exception {
039                    DirectoryScanner ds = new DirectoryScanner();
040    
041                    ds.setBasedir(".");
042                    ds.setIncludes(new String[] {"**\\upgrade\\v**\\util\\*Table.java"});
043    
044                    ds.scan();
045    
046                    String[] fileNames = ds.getIncludedFiles();
047    
048                    for (String fileName : fileNames) {
049                            fileName = StringUtil.replace(fileName, "\\", "/");
050    
051                            int x = fileName.indexOf("upgrade/v");
052                            int y = fileName.indexOf("/util", x);
053    
054                            String version = StringUtil.replace(
055                                    fileName.substring(x + 9, y), "_", ".");
056    
057                            String upgradeFileVersion = version;
058    
059                            int z = upgradeFileVersion.indexOf(".to.");
060    
061                            if (z != -1) {
062                                    upgradeFileVersion = upgradeFileVersion.substring(z + 4);
063                            }
064    
065                            x = fileName.indexOf("/", y + 1);
066                            y = fileName.indexOf("Table.java", x);
067    
068                            String upgradeFileName =
069                                    upgradeTableDir + "/" + upgradeFileVersion + "/" +
070                                            fileName.substring(x, y) + "ModelImpl.java";
071    
072                            if (!_fileUtil.exists(upgradeFileName)) {
073                                    upgradeFileName = _findUpgradeFileName(
074                                            fileName.substring(x, y));
075    
076                                    if (upgradeFileName == null) {
077                                            continue;
078                                    }
079                            }
080    
081                            String content = _fileUtil.read(upgradeFileName);
082    
083                            String packagePath =
084                                    "com.liferay.portal.upgrade.v" +
085                                            StringUtil.replace(version, ".", "_") + ".util";
086                            String className = fileName.substring(x + 1, y) + "Table";
087    
088                            String author = _getAuthor(fileName);
089    
090                            content = _getContent(packagePath, className, content, author);
091    
092                            _fileUtil.write(fileName, content);
093                    }
094            }
095    
096            private String _findUpgradeFileName(String modelName) {
097                    DirectoryScanner ds = new DirectoryScanner();
098    
099                    ds.setBasedir(".");
100                    ds.setIncludes(new String[] {"**\\" + modelName + "ModelImpl.java"});
101    
102                    ds.scan();
103    
104                    String[] fileNames = ds.getIncludedFiles();
105    
106                    if (fileNames.length > 0) {
107                            return fileNames[0];
108                    }
109                    else {
110                            return null;
111                    }
112            }
113    
114            private String _getAuthor(String fileName) throws Exception {
115                    if (_fileUtil.exists(fileName)) {
116                            String content = _fileUtil.read(fileName);
117    
118                            int x = content.indexOf("* @author ");
119    
120                            if (x != -1) {
121                                    int y = content.indexOf("*", x + 1);
122    
123                                    if (y != -1) {
124                                            return content.substring(x + 10, y).trim();
125                                    }
126                            }
127                    }
128    
129                    return ServiceBuilder.AUTHOR;
130            }
131    
132            private String _getContent(
133                            String packagePath, String className, String content, String author)
134                    throws Exception {
135    
136                    int x = content.indexOf("public static final String TABLE_NAME =");
137    
138                    if (x == -1) {
139                            x = content.indexOf("public static String TABLE_NAME =");
140                    }
141    
142                    int y = content.indexOf("public static final String TABLE_SQL_DROP =");
143    
144                    if (y == -1) {
145                            y = content.indexOf("public static String TABLE_SQL_DROP =");
146                    }
147    
148                    y = content.indexOf(";", y);
149    
150                    content = content.substring(x, y + 1);
151    
152                    content = StringUtil.replace(
153                            content,
154                            new String[] {
155                                    "\t", "{ \"", ") }"
156                            },
157                            new String[] {
158                                    "", "{\"", ")}"
159                            });
160    
161                    while (content.contains("\n\n")) {
162                            content = StringUtil.replace(content, "\n\n", "\n");
163                    }
164    
165                    StringBundler sb = new StringBundler();
166    
167                    sb.append(_fileUtil.read("../copyright.txt"));
168    
169                    sb.append("\n\npackage ");
170                    sb.append(packagePath);
171                    sb.append(";\n\n");
172    
173                    sb.append("import java.sql.Types;\n\n");
174    
175                    sb.append("/**\n");
176                    sb.append(" * @author\t  ");
177                    sb.append(author);
178                    sb.append("\n");
179                    sb.append(" * @generated\n");
180                    sb.append(" */\n");
181                    sb.append("public class ");
182                    sb.append(className);
183                    sb.append(" {\n\n");
184    
185                    String[] lines = StringUtil.split(content, "\n");
186    
187                    for (String line : lines) {
188                            if (line.startsWith("public static") ||
189                                    line.startsWith("};")) {
190    
191                                    sb.append("\t");
192                            }
193                            else if (line.startsWith("{\"")) {
194                                    sb.append("\t\t");
195                            }
196    
197                            sb.append(line);
198                            sb.append("\n");
199    
200                            if (line.endsWith(";")) {
201                                    sb.append("\n");
202                            }
203                    }
204    
205                    sb.append("}");
206    
207                    return sb.toString();
208            }
209    
210            private static FileImpl _fileUtil = FileImpl.getInstance();
211    
212    }