1
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
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 x = fileName.indexOf("/", y + 1);
60 y = fileName.indexOf("Table.java", x);
61
62 String upgradeFileName =
63 upgradeTableDir + "/" + version + "/" +
64 fileName.substring(x, y) + "ModelImpl.java";
65
66 if (!_fileUtil.exists(upgradeFileName)) {
67 upgradeFileName = _findUpgradeFileName(
68 fileName.substring(x, y));
69
70 if (upgradeFileName == null) {
71 continue;
72 }
73 }
74
75 String content = _fileUtil.read(upgradeFileName);
76
77 String packagePath =
78 "com.liferay.portal.upgrade.v" +
79 StringUtil.replace(version, ".", "_") + ".util";
80 String className = fileName.substring(x + 1, y) + "Table";
81
82 String author = _getAuthor(fileName);
83
84 content = _getContent(packagePath, className, content, author);
85
86 _fileUtil.write(fileName, content);
87 }
88 }
89
90 private String _findUpgradeFileName(String modelName) {
91 DirectoryScanner ds = new DirectoryScanner();
92
93 ds.setBasedir(".");
94 ds.setIncludes(new String[] {"**\\" + modelName + "ModelImpl.java"});
95
96 ds.scan();
97
98 String[] fileNames = ds.getIncludedFiles();
99
100 if (fileNames.length > 0) {
101 return fileNames[0];
102 }
103 else {
104 return null;
105 }
106 }
107
108 private String _getAuthor(String fileName) throws Exception {
109 if (_fileUtil.exists(fileName)) {
110 String content = _fileUtil.read(fileName);
111
112 int x = content.indexOf("* @author ");
113
114 if (x != -1) {
115 int y = content.indexOf("*", x + 1);
116
117 if (y != -1) {
118 return content.substring(x + 10, y).trim();
119 }
120 }
121 }
122
123 return ServiceBuilder.AUTHOR;
124 }
125
126 private String _getContent(
127 String packagePath, String className, String content, String author)
128 throws Exception {
129
130 int x = content.indexOf("public static final String TABLE_NAME =");
131
132 if (x == -1) {
133 x = content.indexOf("public static String TABLE_NAME =");
134 }
135
136 int y = content.indexOf("public static final String TABLE_SQL_DROP =");
137
138 if (y == -1) {
139 y = content.indexOf("public static String TABLE_SQL_DROP =");
140 }
141
142 y = content.indexOf(";", y);
143
144 content = content.substring(x, y + 1);
145
146 content = StringUtil.replace(
147 content,
148 new String[] {
149 "\t", "{ \"", ") }"
150 },
151 new String[] {
152 "", "{\"", ")}"
153 });
154
155 while (content.contains("\n\n")) {
156 content = StringUtil.replace(content, "\n\n", "\n");
157 }
158
159 StringBundler sb = new StringBundler();
160
161 sb.append(_fileUtil.read("../copyright.txt"));
162
163 sb.append("\n\npackage ");
164 sb.append(packagePath);
165 sb.append(";\n\n");
166
167 sb.append("import java.sql.Types;\n\n");
168
169 sb.append("/**\n");
170 sb.append(" * <a href=\"");
171 sb.append(className);
172 sb.append(".java.html\"><b><i>View Source</i></b></a>\n");
173 sb.append(" *\n");
174 sb.append(" * @author\t ");
175 sb.append(author);
176 sb.append("\n");
177 sb.append(" * @generated\n");
178 sb.append(" */\n");
179 sb.append("public class ");
180 sb.append(className);
181 sb.append(" {\n\n");
182
183 String[] lines = StringUtil.split(content, "\n");
184
185 for (String line : lines) {
186 if (line.startsWith("public static") ||
187 line.startsWith("};")) {
188
189 sb.append("\t");
190 }
191 else if (line.startsWith("{\"")) {
192 sb.append("\t\t");
193 }
194
195 sb.append(line);
196 sb.append("\n");
197
198 if (line.endsWith(";")) {
199 sb.append("\n");
200 }
201 }
202
203 sb.append("}");
204
205 return sb.toString();
206 }
207
208 private static FileImpl _fileUtil = FileImpl.getInstance();
209
210 }