1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.tools;
24  
25  import com.liferay.portal.kernel.util.StringUtil;
26  import com.liferay.portal.util.FileImpl;
27  
28  import org.apache.tools.ant.DirectoryScanner;
29  
30  /**
31   * <a href="UpgradeTableBuilder.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   */
35  public class UpgradeTableBuilder {
36  
37      public static void main(String[] args) {
38          try {
39              new UpgradeTableBuilder(args[0]);
40          }
41          catch (Exception e) {
42              e.printStackTrace();
43          }
44      }
45  
46      public UpgradeTableBuilder(String upgradeTableDir) throws Exception {
47          DirectoryScanner ds = new DirectoryScanner();
48  
49          ds.setBasedir(".");
50          ds.setIncludes(new String[] {"**\\upgrade\\v**\\util\\*Table.java"});
51  
52          ds.scan();
53  
54          String[] fileNames = ds.getIncludedFiles();
55  
56          for (String fileName : fileNames) {
57              fileName = StringUtil.replace(fileName, "\\", "/");
58  
59              int x = fileName.indexOf("upgrade/v");
60              int y = fileName.indexOf("/util", x);
61  
62              String version = StringUtil.replace(
63                  fileName.substring(x + 9, y), "_", ".");
64  
65              x = fileName.indexOf("/", y + 1);
66              y = fileName.indexOf("Table.java", x);
67  
68              String upgradeFileName =
69                  upgradeTableDir + "/" + version + "/" +
70                      fileName.substring(x, y) + "ModelImpl.java";
71  
72              if (!_fileUtil.exists(upgradeFileName)) {
73                  continue;
74              }
75  
76              String content = _fileUtil.read(upgradeFileName);
77  
78              String packagePath =
79                  "com.liferay.portal.upgrade.v" +
80                      StringUtil.replace(version, ".", "_") + ".util";
81              String className = fileName.substring(x + 1, y) + "Table";
82  
83              content = _getContent(packagePath, className, content);
84  
85              _fileUtil.write(fileName, content);
86          }
87      }
88  
89      private String _getContent(
90              String packagePath, String className, String content)
91          throws Exception {
92  
93          int x = content.indexOf("public static final String TABLE_NAME =");
94  
95          if (x == -1) {
96              x = content.indexOf("public static String TABLE_NAME =");
97          }
98  
99          int y = content.indexOf("public static final String TABLE_SQL_DROP =");
100 
101         if (y == -1) {
102             y = content.indexOf("public static String TABLE_SQL_DROP =");
103         }
104 
105         y = content.indexOf(";", y);
106 
107         content = content.substring(x, y + 1);
108 
109         content = StringUtil.replace(
110             content,
111             new String[] {
112                 "\t", "{ \"", ") }"
113             },
114             new String[] {
115                 "", "{\"", ")}"
116             });
117 
118         while (content.contains("\n\n")) {
119             content = StringUtil.replace(content, "\n\n", "\n");
120         }
121 
122         StringBuilder sb = new StringBuilder();
123 
124         sb.append(_fileUtil.read("../copyright.txt"));
125 
126         sb.append("\n\npackage " + packagePath + ";\n\n");
127 
128         sb.append("import java.sql.Types;\n\n");
129 
130         sb.append("/**\n");
131         sb.append(
132             " * <a href=\"" + className +
133                 ".java.html\"><b><i>View Source</i></b></a>\n");
134         sb.append(" *\n");
135         sb.append(" * @author Brian Wing Shun Chan\n");
136         sb.append(" */\n");
137         sb.append("public class " + className + " {\n\n");
138 
139         String[] lines = StringUtil.split(content, "\n");
140 
141         for (String line : lines) {
142             if (line.startsWith("public static") ||
143                 line.startsWith("};")) {
144 
145                 sb.append("\t");
146             }
147             else if (line.startsWith("{\"")) {
148                 sb.append("\t\t");
149             }
150 
151             sb.append(line);
152             sb.append("\n");
153 
154             if (line.endsWith(";")) {
155                 sb.append("\n");
156             }
157         }
158 
159         sb.append("}");
160 
161         return sb.toString();
162     }
163 
164     private static FileImpl _fileUtil = FileImpl.getInstance();
165 
166 }