1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.tools;
16  
17  import com.liferay.portal.kernel.util.StringBundler;
18  import com.liferay.portal.kernel.util.StringUtil;
19  import com.liferay.portal.tools.servicebuilder.ServiceBuilder;
20  import com.liferay.portal.util.FileImpl;
21  
22  import org.apache.tools.ant.DirectoryScanner;
23  
24  /**
25   * <a href="UpgradeTableBuilder.java.html"><b><i>View Source</i></b></a>
26   *
27   * @author Brian Wing Shun Chan
28   */
29  public class UpgradeTableBuilder {
30  
31      public static void main(String[] args) {
32          try {
33              new UpgradeTableBuilder(args[0]);
34          }
35          catch (Exception e) {
36              e.printStackTrace();
37          }
38      }
39  
40      public UpgradeTableBuilder(String upgradeTableDir) throws Exception {
41          DirectoryScanner ds = new DirectoryScanner();
42  
43          ds.setBasedir(".");
44          ds.setIncludes(new String[] {"**\\upgrade\\v**\\util\\*Table.java"});
45  
46          ds.scan();
47  
48          String[] fileNames = ds.getIncludedFiles();
49  
50          for (String fileName : fileNames) {
51              fileName = StringUtil.replace(fileName, "\\", "/");
52  
53              int x = fileName.indexOf("upgrade/v");
54              int y = fileName.indexOf("/util", x);
55  
56              String version = StringUtil.replace(
57                  fileName.substring(x + 9, y), "_", ".");
58  
59              String upgradeFileVersion = version;
60  
61              int z = upgradeFileVersion.indexOf(".to.");
62  
63              if (z != -1) {
64                  upgradeFileVersion = upgradeFileVersion.substring(z + 4);
65              }
66  
67              x = fileName.indexOf("/", y + 1);
68              y = fileName.indexOf("Table.java", x);
69  
70              String upgradeFileName =
71                  upgradeTableDir + "/" + upgradeFileVersion + "/" +
72                      fileName.substring(x, y) + "ModelImpl.java";
73  
74              if (!_fileUtil.exists(upgradeFileName)) {
75                  upgradeFileName = _findUpgradeFileName(
76                      fileName.substring(x, y));
77  
78                  if (upgradeFileName == null) {
79                      continue;
80                  }
81              }
82  
83              String content = _fileUtil.read(upgradeFileName);
84  
85              String packagePath =
86                  "com.liferay.portal.upgrade.v" +
87                      StringUtil.replace(version, ".", "_") + ".util";
88              String className = fileName.substring(x + 1, y) + "Table";
89  
90              String author = _getAuthor(fileName);
91  
92              content = _getContent(packagePath, className, content, author);
93  
94              _fileUtil.write(fileName, content);
95          }
96      }
97  
98      private String _findUpgradeFileName(String modelName) {
99          DirectoryScanner ds = new DirectoryScanner();
100 
101         ds.setBasedir(".");
102         ds.setIncludes(new String[] {"**\\" + modelName + "ModelImpl.java"});
103 
104         ds.scan();
105 
106         String[] fileNames = ds.getIncludedFiles();
107 
108         if (fileNames.length > 0) {
109             return fileNames[0];
110         }
111         else {
112             return null;
113         }
114     }
115 
116     private String _getAuthor(String fileName) throws Exception {
117         if (_fileUtil.exists(fileName)) {
118             String content = _fileUtil.read(fileName);
119 
120             int x = content.indexOf("* @author ");
121 
122             if (x != -1) {
123                 int y = content.indexOf("*", x + 1);
124 
125                 if (y != -1) {
126                     return content.substring(x + 10, y).trim();
127                 }
128             }
129         }
130 
131         return ServiceBuilder.AUTHOR;
132     }
133 
134     private String _getContent(
135             String packagePath, String className, String content, String author)
136         throws Exception {
137 
138         int x = content.indexOf("public static final String TABLE_NAME =");
139 
140         if (x == -1) {
141             x = content.indexOf("public static String TABLE_NAME =");
142         }
143 
144         int y = content.indexOf("public static final String TABLE_SQL_DROP =");
145 
146         if (y == -1) {
147             y = content.indexOf("public static String TABLE_SQL_DROP =");
148         }
149 
150         y = content.indexOf(";", y);
151 
152         content = content.substring(x, y + 1);
153 
154         content = StringUtil.replace(
155             content,
156             new String[] {
157                 "\t", "{ \"", ") }"
158             },
159             new String[] {
160                 "", "{\"", ")}"
161             });
162 
163         while (content.contains("\n\n")) {
164             content = StringUtil.replace(content, "\n\n", "\n");
165         }
166 
167         StringBundler sb = new StringBundler();
168 
169         sb.append(_fileUtil.read("../copyright.txt"));
170 
171         sb.append("\n\npackage ");
172         sb.append(packagePath);
173         sb.append(";\n\n");
174 
175         sb.append("import java.sql.Types;\n\n");
176 
177         sb.append("/**\n");
178         sb.append(" * <a href=\"");
179         sb.append(className);
180         sb.append(".java.html\"><b><i>View Source</i></b></a>\n");
181         sb.append(" *\n");
182         sb.append(" * @author\t  ");
183         sb.append(author);
184         sb.append("\n");
185         sb.append(" * @generated\n");
186         sb.append(" */\n");
187         sb.append("public class ");
188         sb.append(className);
189         sb.append(" {\n\n");
190 
191         String[] lines = StringUtil.split(content, "\n");
192 
193         for (String line : lines) {
194             if (line.startsWith("public static") ||
195                 line.startsWith("};")) {
196 
197                 sb.append("\t");
198             }
199             else if (line.startsWith("{\"")) {
200                 sb.append("\t\t");
201             }
202 
203             sb.append(line);
204             sb.append("\n");
205 
206             if (line.endsWith(";")) {
207                 sb.append("\n");
208             }
209         }
210 
211         sb.append("}");
212 
213         return sb.toString();
214     }
215 
216     private static FileImpl _fileUtil = FileImpl.getInstance();
217 
218 }