1
22
23 package com.liferay.portal.tools.samplesqlbuilder;
24
25 import com.liferay.counter.model.Counter;
26 import com.liferay.portal.freemarker.FreeMarkerUtil;
27 import com.liferay.portal.kernel.util.GetterUtil;
28 import com.liferay.portal.kernel.util.ListUtil;
29 import com.liferay.portal.kernel.util.StringPool;
30 import com.liferay.portal.kernel.util.Validator;
31 import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
32 import com.liferay.portal.model.Group;
33 import com.liferay.portal.model.GroupConstants;
34 import com.liferay.portal.model.Organization;
35 import com.liferay.portal.model.Permission;
36 import com.liferay.portal.model.Resource;
37 import com.liferay.portal.model.ResourceCode;
38 import com.liferay.portal.model.ResourceConstants;
39 import com.liferay.portal.model.Role;
40 import com.liferay.portal.model.RoleConstants;
41 import com.liferay.portal.model.User;
42 import com.liferay.portal.model.impl.GroupImpl;
43 import com.liferay.portal.model.impl.RoleImpl;
44 import com.liferay.portal.security.permission.ActionKeys;
45 import com.liferay.portal.tools.sql.DBUtil;
46 import com.liferay.portal.util.InitUtil;
47 import com.liferay.util.SimpleCounter;
48
49 import java.io.BufferedReader;
50 import java.io.File;
51 import java.io.FileWriter;
52 import java.io.StringReader;
53 import java.io.Writer;
54
55 import java.util.ArrayList;
56 import java.util.HashMap;
57 import java.util.List;
58 import java.util.Map;
59
60
66 public class SampleSQLBuilder {
67
68 public static void main(String[] args) {
69 InitUtil.initWithSpring();
70
71 String outputDir = System.getProperty("sample.sql.output.dir");
72 int maxUserCount = GetterUtil.getInteger(
73 System.getProperty("sample.sql.max.user.count"));
74
75 new SampleSQLBuilder(outputDir, maxUserCount);
76 }
77
78 public SampleSQLBuilder(String outputDir, int maxUserCount) {
79 try {
80 _maxUserCount = maxUserCount;
81
82 _mysqlWriter = new FileWriter(new File(
83 outputDir + "/sample-mysql.sql"));
84
85 _mysqlDBUtil = DBUtil.getInstance(DBUtil.DB_TYPE_MYSQL);
86
87 createClassNames();
88 createResourceCodes();
89 createCompany();
90 createRoles();
91 createGroups();
92 createOrganizations();
93 createUsers();
94 createCounters();
95
96 _mysqlWriter.write(_mysqlDBUtil.buildSQL("COMMIT_TRANSACTION"));
97
98 _mysqlWriter.flush();
99 }
100 catch (Exception e) {
101 e.printStackTrace();
102 }
103 }
104
105 protected void createClassName(long classNameId, String value)
106 throws Exception {
107
108 Map<String, Object> context = getContext();
109
110 put(context, "classNameId", classNameId);
111 put(context, "value", value);
112
113 processTemplate(_tplClassName, context);
114 }
115
116 protected void createClassNames() throws Exception {
117 createClassName(_organizationClassNameId, Organization.class.getName());
118 createClassName(_userClassNameId, User.class.getName());
119
120 write(StringPool.NEW_LINE);
121 }
122
123 protected void createCompany() throws Exception {
124 processTemplate(_tplCompany, getContext());
125
126 write(StringPool.NEW_LINE);
127 }
128
129 protected void createCounter(long currentId, String name) throws Exception {
130 Map<String, Object> context = getContext();
131
132 put(context, "currentId", currentId);
133 put(context, "name", name);
134
135 processTemplate(_tplCounter, context);
136
137 write(StringPool.NEW_LINE);
138 }
139
140 protected void createCounters() throws Exception {
141 createCounter(_counter.get(), Counter.class.getName());
142 createCounter(_permissionCounter.get(), Permission.class.getName());
143 createCounter(_resourceCounter.get(), Resource.class.getName());
144 createCounter(_resourceCodeCounter.get(), ResourceCode.class.getName());
145
146 write(StringPool.NEW_LINE);
147 }
148
149 protected void createGroup(String friendlyURL, long groupId, String name)
150 throws Exception {
151
152 Map<String, Object> context = getContext();
153
154 put(context, "friendlyURL", friendlyURL);
155 put(context, "groupId", groupId);
156 put(context, "name", name);
157
158 processTemplate(_tplGroup, context);
159 }
160
161 protected void createGroups() throws Exception {
162 createGroup("/guest", _guestGroupId, GroupConstants.GUEST);
163
164 write(StringPool.NEW_LINE);
165 }
166
167 protected void createOrganization(
168 String friendlyURL, long groupId, String name, long organizationId,
169 long parentOrganizationId, String type)
170 throws Exception {
171
172 Map<String, Object> context = getContext();
173
174 put(context, "friendlyURL", friendlyURL);
175 put(context, "groupId", groupId);
176 put(context, "name", name);
177 put(context, "organizationId", organizationId);
178 put(context, "parentOrganizationId", parentOrganizationId);
179 put(context, "type", type);
180
181 processTemplate(_tplOrganization, context);
182 }
183
184 protected void createOrganizations() throws Exception {
185 createOrganization(
186 "/liferayinc", _counter.get(), "Liferay, Inc.", _counter.get(), -1,
187 "regular-organization");
188
189 write(StringPool.NEW_LINE);
190 }
191
192 protected void createPermission(
193 String actionId, long permissionId, long resourceId)
194 throws Exception {
195
196 Map<String, Object> context = getContext();
197
198 put(context, "actionId", actionId);
199 put(context, "permissionId", permissionId);
200 put(context, "resourceId", resourceId);
201
202 processTemplate(_tplPermission, context);
203 }
204
205 protected void createResource(long codeId, String primKey, long resourceId)
206 throws Exception {
207
208 Map<String, Object> context = getContext();
209
210 put(context, "codeId", codeId);
211 put(context, "primKey", primKey);
212 put(context, "resourceId", resourceId);
213
214 processTemplate(_tplResource, context);
215 }
216
217 protected void createResourceCode(long codeId, String name, int scope)
218 throws Exception {
219
220 Map<String, Object> context = getContext();
221
222 put(context, "codeId", codeId);
223 put(context, "name", name);
224 put(context, "scope", scope);
225
226 processTemplate(_tplResourceCode, context);
227 }
228
229 protected void createResourceCodes() throws Exception {
230 createResourceCode(
231 _userCompanyResourceCodeId, User.class.getName(),
232 ResourceConstants.SCOPE_COMPANY);
233 createResourceCode(
234 _userIndividualResourceCodeId, User.class.getName(),
235 ResourceConstants.SCOPE_INDIVIDUAL);
236
237 write(StringPool.NEW_LINE);
238 }
239
240 protected void createRole(String name, long roleId) throws Exception {
241 Map<String, Object> context = getContext();
242
243 put(context, "name", name);
244 put(context, "roleId", roleId);
245
246 processTemplate(_tplRole, context);
247 }
248
249 protected void createRoles() throws Exception {
250 createRole(RoleConstants.ADMINISTRATOR, _administratorRoleId);
251 createRole(RoleConstants.GUEST, _guestRoleId);
252 createRole(RoleConstants.POWER_USER, _powerUserRoleId);
253 createRole(RoleConstants.USER, _userRoleId);
254
255 write(StringPool.NEW_LINE);
256 }
257
258 protected void createUser(
259 long contactId, String emailAddress, String firstName, long groupId,
260 String lastName, String screenName, List<Group> userGroups,
261 long userId, List<Organization> userOrganizations,
262 List<Role> userRoles)
263 throws Exception {
264
265 String userName = firstName + StringPool.SPACE + lastName;
266
267 if (Validator.isNull(firstName) && Validator.isNull(lastName)) {
268 userName = StringPool.BLANK;
269 }
270
271 String userUuid = PortalUUIDUtil.generate();
272
273 Map<String, Object> context = getContext();
274
275 put(context, "contactId", contactId);
276 put(context, "emailAddress", emailAddress);
277 put(context, "firstName", firstName);
278 put(context, "friendlyURL", "/" + screenName);
279
280 if (groupId > 0) {
281 put(context, "groupId", groupId);
282 }
283
284 put(context, "lastName", lastName);
285 put(context, "screenName", screenName);
286 put(context, "userClassNameId", _userClassNameId);
287 put(context, "userGroups", userGroups);
288 put(context, "userId", userId);
289 put(context, "userName", userName);
290 put(context, "userOrganizations", userOrganizations);
291 put(context, "userRoles", userRoles);
292 put(context, "userUuid", userUuid);
293
294 processTemplate(_tplUser, context);
295
296 write(StringPool.NEW_LINE);
297
298 long resourceId = _resourceCounter.get();
299
300 createResource(
301 _userIndividualResourceCodeId, String.valueOf(userId), resourceId);
302
303 write(StringPool.NEW_LINE);
304
305 String[] actionIds = new String[] {
306 ActionKeys.DELETE, ActionKeys.IMPERSONATE, ActionKeys.PERMISSIONS,
307 ActionKeys.UPDATE, ActionKeys.VIEW
308 };
309
310 for (String actionId : actionIds) {
311 createPermission(actionId, _permissionCounter.get(), resourceId);
312 }
313 }
314
315 protected void createUsers() throws Exception {
316 createUser(
317 _defaultContactId, "default@liferay.com", StringPool.BLANK, 0,
318 StringPool.BLANK, String.valueOf(_defaultUserId), null,
319 _defaultUserId, null, null);
320
321 write(StringPool.NEW_LINE);
322
323 String dependenciesDir =
324 "../portal-impl/src/com/liferay/portal/tools/samplesqlbuilder/" +
325 "dependencies/";
326
327 List<String> firstNames = ListUtil.fromFile(
328 dependenciesDir + "first_names.txt");
329 List<String> lastNames = ListUtil.fromFile(
330 dependenciesDir + "last_names.txt");
331
332 List<Group> userGroups = new ArrayList<Group>();
333
334 Group group = new GroupImpl();
335
336 group.setGroupId(_guestGroupId);
337 group.setName(GroupConstants.GUEST);
338
339 userGroups.add(group);
340
341 List<Role> userRoles = new ArrayList<Role>();
342
343 Role role = new RoleImpl();
344
345 role.setRoleId(_userRoleId);
346 role.setName(RoleConstants.USER);
347
348 userRoles.add(role);
349
350 int userCount = 0;
351
352 for (String lastName : lastNames) {
353 for (String firstName : firstNames) {
354 userCount++;
355
356 long contactId = _counter.get();
357 long groupId = _counter.get();
358 long userId = _counter.get();
359
360 createUser(
361 contactId, userId + "@liferay.com", firstName, groupId,
362 lastName, String.valueOf(userId), userGroups, userId, null,
363 userRoles);
364
365 write(StringPool.NEW_LINE);
366
367 if (userCount >= _maxUserCount) {
368 break;
369 }
370 }
371
372 if (userCount >= _maxUserCount) {
373 break;
374 }
375 }
376 }
377
378 protected Map<String, Object> getContext() {
379 Map<String, Object> context = new HashMap<String, Object>();
380
381 put(context, "accountId", _accountId);
382 put(context, "administratorRoleId", _administratorRoleId);
383 put(context, "counter", _counter);
384 put(context, "defaultContactId", _defaultContactId);
385 put(context, "defaultUserId", _defaultUserId);
386 put(context, "companyId", _companyId);
387 put(context, "guestGroupId", _guestGroupId);
388 put(context, "guestRoleId", _guestRoleId);
389 put(context, "organizationClassNameId", _organizationClassNameId);
390 put(context, "powerUserRoleId", _powerUserRoleId);
391 put(context, "userClassNameId", _userClassNameId);
392 put(context, "userRoleId", _userRoleId);
393
394 return context;
395 }
396
397 protected void processTemplate(String name, Map<String, Object> context)
398 throws Exception {
399
400 String sql = FreeMarkerUtil.process(name, context);
401
402 String mysqlSQL = _mysqlDBUtil.buildSQL(sql);
403
404 boolean previousBlankLine = false;
405
406 BufferedReader br = new BufferedReader(new StringReader(mysqlSQL));
407
408 String s = null;
409
410 while ((s = br.readLine()) != null) {
411 s = s.trim();
412
413 _mysqlWriter.write(s);
414
415 if (previousBlankLine && Validator.isNull(s)) {
416 }
417 else {
418 write(StringPool.NEW_LINE);
419 }
420
421 if (Validator.isNull(s)) {
422 previousBlankLine = true;
423 }
424 }
425
426 br.close();
427 }
428
429 protected void put(Map<String, Object> context, String key, boolean value) {
430 context.put(key, String.valueOf(value));
431 }
432
433 protected void put(Map<String, Object> context, String key, int value) {
434 context.put(key, String.valueOf(value));
435 }
436
437 protected void put(Map<String, Object> context, String key, long value) {
438 context.put(key, String.valueOf(value));
439 }
440
441 protected void put(Map<String, Object> context, String key, Object value) {
442 context.put(key, value);
443 }
444
445 protected void put(Map<String, Object> context, String key, String value) {
446 context.put(key, value);
447 }
448
449 protected void write(String s) throws Exception {
450 _mysqlWriter.write(s);
451 }
452
453 private static final String _TPL_ROOT =
454 "com/liferay/portal/tools/samplesqlbuilder/dependencies/";
455
456 private String _tplClassName = _TPL_ROOT + "class_name.ftl";
457 private String _tplCompany = _TPL_ROOT + "company.ftl";
458 private String _tplCounter = _TPL_ROOT + "counter.ftl";
459 private String _tplGroup = _TPL_ROOT + "group.ftl";
460 private String _tplOrganization = _TPL_ROOT + "organization.ftl";
461 private String _tplPermission = _TPL_ROOT + "permission.ftl";
462 private String _tplResource = _TPL_ROOT + "resource.ftl";
463 private String _tplResourceCode = _TPL_ROOT + "resource_code.ftl";
464 private String _tplRole = _TPL_ROOT + "role.ftl";
465 private String _tplUser = _TPL_ROOT + "user.ftl";
466 private Writer _mysqlWriter;
467 private DBUtil _mysqlDBUtil;
468 private int _maxUserCount;
469 private SimpleCounter _counter = new SimpleCounter();
470 private SimpleCounter _permissionCounter = new SimpleCounter();
471 private SimpleCounter _resourceCodeCounter = new SimpleCounter();
472 private SimpleCounter _resourceCounter = new SimpleCounter();
473 private long _accountId = _counter.get();
474 private long _administratorRoleId = _counter.get();
475 private long _defaultContactId = _counter.get();
476 private long _defaultUserId = _counter.get();
477 private long _companyId = _counter.get();
478 private long _guestGroupId = _counter.get();
479 private long _guestRoleId = _counter.get();
480 private long _organizationClassNameId = _counter.get();
481 private long _powerUserRoleId = _counter.get();
482 private long _userClassNameId = _counter.get();
483 private long _userCompanyResourceCodeId = _resourceCodeCounter.get();
484 private long _userIndividualResourceCodeId = _resourceCodeCounter.get();
485 private long _userRoleId = _counter.get();
486
487 }