001    /**
002     * Copyright (c) 2000-2013 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.samplesqlbuilder;
016    
017    import com.liferay.portal.dao.db.MySQLDB;
018    import com.liferay.portal.freemarker.FreeMarkerUtil;
019    import com.liferay.portal.kernel.dao.db.DB;
020    import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
021    import com.liferay.portal.kernel.io.CharPipe;
022    import com.liferay.portal.kernel.io.OutputStreamWriter;
023    import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
024    import com.liferay.portal.kernel.io.unsync.UnsyncBufferedWriter;
025    import com.liferay.portal.kernel.io.unsync.UnsyncTeeWriter;
026    import com.liferay.portal.kernel.util.DateUtil_IW;
027    import com.liferay.portal.kernel.util.FileUtil;
028    import com.liferay.portal.kernel.util.GetterUtil;
029    import com.liferay.portal.kernel.util.ListUtil;
030    import com.liferay.portal.kernel.util.StringBundler;
031    import com.liferay.portal.kernel.util.StringPool;
032    import com.liferay.portal.kernel.util.StringUtil_IW;
033    import com.liferay.portal.model.Company;
034    import com.liferay.portal.model.Contact;
035    import com.liferay.portal.model.Group;
036    import com.liferay.portal.model.Layout;
037    import com.liferay.portal.model.Resource;
038    import com.liferay.portal.model.Role;
039    import com.liferay.portal.model.User;
040    import com.liferay.portal.tools.ArgumentsUtil;
041    import com.liferay.portal.util.InitUtil;
042    import com.liferay.portlet.blogs.model.BlogsEntry;
043    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
044    import com.liferay.portlet.documentlibrary.model.DLFolder;
045    import com.liferay.portlet.dynamicdatalists.model.DDLRecord;
046    import com.liferay.portlet.dynamicdatalists.model.DDLRecordSet;
047    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
048    import com.liferay.portlet.messageboards.model.MBCategory;
049    import com.liferay.portlet.messageboards.model.MBMessage;
050    import com.liferay.portlet.wiki.model.WikiNode;
051    import com.liferay.portlet.wiki.model.WikiPage;
052    import com.liferay.util.SimpleCounter;
053    
054    import java.io.File;
055    import java.io.FileInputStream;
056    import java.io.FileOutputStream;
057    import java.io.FileWriter;
058    import java.io.IOException;
059    import java.io.Reader;
060    import java.io.Writer;
061    
062    import java.nio.channels.FileChannel;
063    
064    import java.util.ArrayList;
065    import java.util.Collections;
066    import java.util.HashMap;
067    import java.util.List;
068    import java.util.Map;
069    import java.util.Set;
070    import java.util.concurrent.ConcurrentHashMap;
071    
072    /**
073     * @author Brian Wing Shun Chan
074     * @author Shuyang Zhou
075     */
076    public class SampleSQLBuilder {
077    
078            public static void main(String[] args) {
079                    Map<String, String> arguments = ArgumentsUtil.parseArguments(args);
080    
081                    InitUtil.initWithSpring();
082    
083                    String baseDir = arguments.get("sample.sql.base.dir");
084                    String dbType = arguments.get("sample.sql.db.type");
085                    int maxBlogsEntryCommentCount = GetterUtil.getInteger(
086                            arguments.get("sample.sql.max.blogs.entry.comment.count"));
087                    int maxBlogsEntryCount = GetterUtil.getInteger(
088                            arguments.get("sample.sql.max.blogs.entry.count"));
089                    int maxDDLRecordCount = GetterUtil.getInteger(
090                            arguments.get("sample.sql.max.ddl.record.count"));
091                    int maxDDLRecordSetCount = GetterUtil.getInteger(
092                            arguments.get("sample.sql.max.ddl.record.set.count"));
093                    int maxDLFileEntryCount = GetterUtil.getInteger(
094                            arguments.get("sample.sql.max.dl.file.entry.count"));
095                    int maxDLFileEntrySize = GetterUtil.getInteger(
096                            arguments.get("sample.sql.max.dl.file.entry.size"));
097                    int maxDLFolderCount = GetterUtil.getInteger(
098                            arguments.get("sample.sql.max.dl.folder.count"));
099                    int maxDLFolderDepth = GetterUtil.getInteger(
100                            arguments.get("sample.sql.max.dl.folder.depth"));
101                    int maxGroupCount = GetterUtil.getInteger(
102                            arguments.get("sample.sql.max.group.count"));
103                    int maxJournalArticleCount = GetterUtil.getInteger(
104                            arguments.get("sample.sql.max.journal.article.count"));
105                    int maxJournalArticleSize = GetterUtil.getInteger(
106                            arguments.get("sample.sql.max.journal.article.size"));
107                    int maxMBCategoryCount = GetterUtil.getInteger(
108                            arguments.get("sample.sql.max.mb.category.count"));
109                    int maxMBMessageCount = GetterUtil.getInteger(
110                            arguments.get("sample.sql.max.mb.message.count"));
111                    int maxMBThreadCount = GetterUtil.getInteger(
112                            arguments.get("sample.sql.max.mb.thread.count"));
113                    int maxUserCount = GetterUtil.getInteger(
114                            arguments.get("sample.sql.max.user.count"));
115                    int maxUserToGroupCount = GetterUtil.getInteger(
116                            arguments.get("sample.sql.max.user.to.group.count"));
117                    int maxWikiNodeCount = GetterUtil.getInteger(
118                            arguments.get("sample.sql.max.wiki.node.count"));
119                    int maxWikiPageCommentCount = GetterUtil.getInteger(
120                            arguments.get("sample.sql.max.wiki.page.comment.count"));
121                    int maxWikiPageCount = GetterUtil.getInteger(
122                            arguments.get("sample.sql.max.wiki.page.count"));
123                    int optimizeBufferSize = GetterUtil.getInteger(
124                            arguments.get("sample.sql.optimize.buffer.size"));
125                    String outputDir = arguments.get("sample.sql.output.dir");
126                    boolean outputMerge = GetterUtil.getBoolean(
127                            arguments.get("sample.sql.output.merge"));
128                    boolean securityEnabled = GetterUtil.getBoolean(
129                            arguments.get("sample.sql.security.enabled"));
130    
131                    new SampleSQLBuilder(
132                            arguments, baseDir, dbType, maxBlogsEntryCommentCount,
133                            maxBlogsEntryCount, maxDDLRecordCount, maxDDLRecordSetCount,
134                            maxDLFileEntryCount, maxDLFileEntrySize, maxDLFolderCount,
135                            maxDLFolderDepth, maxGroupCount, maxJournalArticleCount,
136                            maxJournalArticleSize, maxMBCategoryCount, maxMBMessageCount,
137                            maxMBThreadCount, maxUserCount, maxUserToGroupCount,
138                            maxWikiNodeCount, maxWikiPageCommentCount, maxWikiPageCount,
139                            optimizeBufferSize, outputDir, outputMerge, securityEnabled);
140            }
141    
142            public SampleSQLBuilder(
143                    Map<String, String> arguments, String baseDir, String dbType,
144                    int maxBlogsEntryCommentCount, int maxBlogsEntryCount,
145                    int maxDDLRecordCount, int maxDDLRecordSetCount,
146                    int maxDLFileEntryCount, int maxDLFileEntrySize, int maxDLFolderCount,
147                    int maxDLFolderDepth, int maxGroupCount, int maxJournalArticleCount,
148                    int maxJournalArticleSize, int maxMBCategoryCount,
149                    int maxMBMessageCount, int maxMBThreadCount, int maxUserCount,
150                    int maxUserToGroupCount, int maxWikiNodeCount,
151                    int maxWikiPageCommentCount, int maxWikiPageCount,
152                    int optimizeBufferSize, String outputDir, boolean outputMerge,
153                    boolean securityEnabled) {
154    
155                    try {
156                            _dbType = dbType;
157                            _maxBlogsEntryCommentCount = maxBlogsEntryCommentCount;
158                            _maxBlogsEntryCount = maxBlogsEntryCount;
159                            _maxDDLRecordCount = maxDDLRecordCount;
160                            _maxDDLRecordSetCount = maxDDLRecordSetCount;
161                            _maxDLFileEntryCount = maxDLFileEntryCount;
162                            _maxDLFileEntrySize = maxDLFileEntrySize;
163                            _maxDLFolderCount = maxDLFolderCount;
164                            _maxDLFolderDepth = maxDLFolderDepth;
165                            _maxGroupCount = maxGroupCount;
166                            _maxJournalArticleCount = maxJournalArticleCount;
167                            _maxMBCategoryCount = maxMBCategoryCount;
168                            _maxMBMessageCount = maxMBMessageCount;
169                            _maxMBThreadCount = maxMBThreadCount;
170                            _maxUserCount = maxUserCount;
171                            _maxUserToGroupCount = maxUserToGroupCount;
172                            _maxWikiNodeCount = maxWikiNodeCount;
173                            _maxWikiPageCommentCount = maxWikiPageCommentCount;
174                            _maxWikiPageCount = maxWikiPageCount;
175                            _optimizeBufferSize = optimizeBufferSize;
176                            _outputDir = outputDir;
177                            _outputMerge = outputMerge;
178                            _securityEnabled = securityEnabled;
179    
180                            int totalMThreadCount = maxMBCategoryCount * maxMBThreadCount;
181                            int totalMBMessageCount = totalMThreadCount * maxMBMessageCount;
182    
183                            int counterOffset =
184                                    _maxGroupCount +
185                                    (_maxGroupCount *
186                                            (maxMBCategoryCount + totalMThreadCount +
187                                                    totalMBMessageCount)
188                                    ) + 1;
189    
190                            _counter = new SimpleCounter(counterOffset);
191                            _dlDateCounter = new SimpleCounter();
192                            _permissionCounter = new SimpleCounter();
193                            _resourceCounter = new SimpleCounter();
194                            _resourceCodeCounter = new SimpleCounter();
195                            _resourcePermissionCounter = new SimpleCounter();
196                            _socialActivityCounter = new SimpleCounter();
197    
198                            _userScreenNameIncrementer = new SimpleCounter();
199    
200                            _dataFactory = new DataFactory(
201                                    baseDir, _maxGroupCount, maxJournalArticleSize,
202                                    _maxUserToGroupCount, _counter, _dlDateCounter,
203                                    _permissionCounter, _resourceCounter, _resourceCodeCounter,
204                                    _resourcePermissionCounter, _socialActivityCounter);
205    
206                            _db = DBFactoryUtil.getDB(_dbType);
207    
208                            if (_db instanceof MySQLDB) {
209                                    _db = new SampleMySQLDB();
210                            }
211    
212                            // Clean up previous output
213    
214                            FileUtil.delete(_outputDir + "/sample-" + _dbType + ".sql");
215                            FileUtil.deltree(_outputDir + "/output");
216    
217                            // Generic
218    
219                            _tempDir = new File(_outputDir, "temp");
220    
221                            _tempDir.mkdirs();
222    
223                            final CharPipe charPipe = new CharPipe(_PIPE_BUFFER_SIZE);
224    
225                            generateSQL(charPipe);
226    
227                            try {
228    
229                                    // Specific
230    
231                                    compressSQL(charPipe.getReader());
232    
233                                    // Merge
234    
235                                    mergeSQL();
236                            }
237                            finally {
238                                    FileUtil.deltree(_tempDir);
239                            }
240    
241                            StringBundler sb = new StringBundler();
242    
243                            List<String> keys = ListUtil.fromMapKeys(arguments);
244    
245                            Collections.sort(keys);
246    
247                            for (String key : keys) {
248                                    if (!key.startsWith("sample.sql")) {
249                                            continue;
250                                    }
251    
252                                    String value = arguments.get(key);
253    
254                                    sb.append(key);
255                                    sb.append(StringPool.EQUAL);
256                                    sb.append(value);
257                                    sb.append(StringPool.NEW_LINE);
258                            }
259    
260                            FileUtil.write(
261                                    new File(_outputDir, "benchmarks-actual.properties"),
262                                    sb.toString());
263                    }
264                    catch (Exception e) {
265                            e.printStackTrace();
266                    }
267            }
268    
269            public void insertBlogsEntry(BlogsEntry blogsEntry) throws Exception {
270                    Map<String, Object> context = getContext();
271    
272                    put(context, "blogsEntry", blogsEntry);
273    
274                    processTemplate(_tplBlogsEntry, context);
275            }
276    
277            public void insertDDLRecord(
278                            DDLRecord ddlRecord, DDLRecordSet ddlRecordSet, int ddlRecordCount)
279                    throws Exception {
280    
281                    Map<String, Object> context = getContext();
282    
283                    put(context, "ddlRecord", ddlRecord);
284                    put(context, "ddlRecordCount", ddlRecordCount);
285                    put(context, "ddlRecordSet", ddlRecordSet);
286    
287                    processTemplate(_tplDDLRecord, context);
288            }
289    
290            public void insertDDLRecordSet(DDMStructure ddmStructure) throws Exception {
291                    Map<String, Object> context = getContext();
292    
293                    put(context, "ddmStructure", ddmStructure);
294    
295                    processTemplate(_tplDLFolders, context);
296            }
297    
298            public void insertDLFileEntry(
299                            DLFileEntry dlFileEntry, DDMStructure ddmStructure)
300                    throws Exception {
301    
302                    Map<String, Object> context = getContext();
303    
304                    put(context, "ddmStructure", ddmStructure);
305                    put(context, "dlFileEntry", dlFileEntry);
306    
307                    processTemplate(_tplDLFileEntry, context);
308            }
309    
310            public void insertDLFolder(DLFolder dlFolder, DDMStructure ddmStructure)
311                    throws Exception {
312    
313                    Map<String, Object> context = getContext();
314    
315                    put(context, "ddmStructure", ddmStructure);
316                    put(context, "dlFolder", dlFolder);
317    
318                    processTemplate(_tplDLFolder, context);
319            }
320    
321            public void insertDLFolders(
322                            long parentDLFolderId, int dlFolderDepth, DDMStructure ddmStructure)
323                    throws Exception {
324    
325                    Map<String, Object> context = getContext();
326    
327                    put(context, "ddmStructure", ddmStructure);
328                    put(context, "dlFolderDepth", dlFolderDepth);
329                    put(context, "parentDLFolderId", parentDLFolderId);
330    
331                    processTemplate(_tplDLFolders, context);
332            }
333    
334            public void insertGroup(
335                            Group group, List<Layout> privateLayouts,
336                            List<Layout> publicLayouts)
337                    throws Exception {
338    
339                    Map<String, Object> context = getContext();
340    
341                    put(context, "group", group);
342                    put(context, "privateLayouts", privateLayouts);
343                    put(context, "publicLayouts", publicLayouts);
344    
345                    processTemplate(_tplGroup, context);
346            }
347    
348            public void insertMBCategory(MBCategory mbCategory) throws Exception {
349                    Map<String, Object> context = getContext();
350    
351                    put(context, "mbCategory", mbCategory);
352    
353                    processTemplate(_tplMBCategory, context);
354            }
355    
356            public void insertMBMessage(MBMessage mbMessage) throws Exception {
357                    Map<String, Object> context = getContext();
358    
359                    put(context, "mbMessage", mbMessage);
360    
361                    processTemplate(_tplMBMessage, context);
362            }
363    
364            public void insertResourcePermission(String name, String primKey)
365                    throws Exception {
366    
367                    Map<String, Object> context = getContext();
368    
369                    put(context, "resourceName", name);
370                    put(context, "resourcePrimkey", primKey);
371    
372                    processTemplate(_tplResourcePermission, context);
373            }
374    
375            public void insertSecurity(String name, long primKey) throws Exception {
376                    insertSecurity(name, String.valueOf(primKey));
377            }
378    
379            public void insertSecurity(String name, String primKey) throws Exception {
380                    if (!_securityEnabled) {
381                            return;
382                    }
383    
384                    Map<String, Object> context = getContext();
385    
386                    Resource resource = _dataFactory.addResource(name, primKey);
387    
388                    put(context, "resource", resource);
389    
390                    processTemplate(_tplSecurity, context);
391            }
392    
393            public void insertUser(
394                            Contact contact, List<Long> groupIds, List<Long> organizationIds,
395                            List<Role> roleIds, User user)
396                    throws Exception {
397    
398                    Map<String, Object> context = getContext();
399    
400                    put(context, "contact", contact);
401                    put(context, "groupIds", groupIds);
402                    put(context, "organizationIds", organizationIds);
403                    put(context, "roleIds", roleIds);
404                    put(context, "user", user);
405    
406                    processTemplate(_tplUser, context);
407            }
408    
409            public void insertWikiPage(WikiNode wikiNode, WikiPage wikiPage)
410                    throws Exception {
411    
412                    Map<String, Object> context = getContext();
413    
414                    put(context, "wikiNode", wikiNode);
415                    put(context, "wikiPage", wikiPage);
416    
417                    processTemplate(_tplWikiPage, context);
418            }
419    
420            protected void compressInsertSQL(String insertSQL) throws IOException {
421                    String tableName = insertSQL.substring(0, insertSQL.indexOf(' '));
422    
423                    int pos = insertSQL.indexOf(" values ") + 8;
424    
425                    String values = insertSQL.substring(pos, insertSQL.length() - 1);
426    
427                    StringBundler sb = _insertSQLs.get(tableName);
428    
429                    if ((sb == null) || (sb.index() == 0)) {
430                            sb = new StringBundler();
431    
432                            _insertSQLs.put(tableName, sb);
433    
434                            sb.append("insert into ");
435                            sb.append(insertSQL.substring(0, pos));
436                            sb.append("\n");
437                    }
438                    else {
439                            sb.append(",\n");
440                    }
441    
442                    sb.append(values);
443    
444                    if (sb.index() >= _optimizeBufferSize) {
445                            sb.append(";\n");
446    
447                            String sql = _db.buildSQL(sb.toString());
448    
449                            sb.setIndex(0);
450    
451                            writeToInsertSQLFile(tableName, sql);
452                    }
453            }
454    
455            protected void compressSQL(Reader reader) throws IOException {
456                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
457                            reader);
458    
459                    String s = null;
460    
461                    while ((s = unsyncBufferedReader.readLine()) != null) {
462                            s = s.trim();
463    
464                            if (s.length() > 0) {
465                                    if (s.startsWith("insert into ")) {
466                                            compressInsertSQL(s.substring(12));
467                                    }
468                                    else if (s.length() > 0) {
469                                            _otherSQLs.add(s);
470                                    }
471                            }
472                    }
473    
474                    unsyncBufferedReader.close();
475            }
476    
477            protected Writer createFileWriter(File file) throws IOException {
478                    FileOutputStream fileOutputStream = new FileOutputStream(file);
479    
480                    Writer writer = new OutputStreamWriter(fileOutputStream);
481    
482                    return createUnsyncBufferedWriter(writer);
483            }
484    
485            protected Writer createFileWriter(String fileName) throws IOException {
486                    File file = new File(fileName);
487    
488                    return createFileWriter(file);
489            }
490    
491            protected Writer createUnsyncBufferedWriter(Writer writer) {
492                    return new UnsyncBufferedWriter(writer, _WRITER_BUFFER_SIZE) {
493    
494                            @Override
495                            public void flush() {
496    
497                                    // Disable FreeMarker from flushing
498    
499                            }
500    
501                    };
502            }
503    
504            protected void generateSQL(final CharPipe charPipe) {
505                    final Writer writer = createUnsyncBufferedWriter(charPipe.getWriter());
506    
507                    Thread thread = new Thread() {
508    
509                            @Override
510                            public void run() {
511                                    try {
512                                            _writerSampleSQL = new UnsyncTeeWriter(
513                                                    writer, createFileWriter(_outputDir + "/sample.sql"));
514    
515                                            createSample();
516    
517                                            _writerSampleSQL.close();
518    
519                                            charPipe.close();
520                                    }
521                                    catch (Exception e) {
522                                            e.printStackTrace();
523                                    }
524                            }
525    
526                            protected void createSample() throws Exception {
527                                    _writerBlogsCSV = getWriter("blogs.csv");
528                                    _writerCompanyCSV = getWriter("company.csv");
529                                    _writerDocumentLibraryCSV = getWriter("document_library.csv");
530                                    _writerDynamicDataListsCSV = getWriter(
531                                            "dynamic_data_lists.csv");
532                                    _writerLayoutCSV = getWriter("layout.csv");
533                                    _writerMessageBoardsCSV = getWriter("message_boards.csv");
534                                    _writerRepositoryCSV = getWriter("repository.csv");
535                                    _writerUserCSV = getWriter("user.csv");
536                                    _writerWikiCSV = getWriter("wiki.csv");
537    
538                                    Map<String, Object> context = getContext();
539    
540                                    processTemplate(_tplSample, context);
541    
542                                    _writerBlogsCSV.close();
543                                    _writerCompanyCSV.close();
544                                    _writerDocumentLibraryCSV.close();
545                                    _writerDynamicDataListsCSV.close();
546                                    _writerLayoutCSV.close();
547                                    _writerMessageBoardsCSV.close();
548                                    _writerRepositoryCSV.close();
549                                    _writerUserCSV.close();
550                                    _writerWikiCSV.close();
551                            }
552    
553                            protected Writer getWriter(String fileName) throws Exception {
554                                    return createFileWriter(new File(_outputDir + "/" + fileName));
555                            }
556    
557                    };
558    
559                    thread.start();
560            }
561    
562            protected Map<String, Object> getContext() {
563                    Map<String, Object> context = new HashMap<String, Object>();
564    
565                    Company company = _dataFactory.getCompany();
566                    User defaultUser = _dataFactory.getDefaultUser();
567    
568                    put(context, "companyId", company.getCompanyId());
569                    put(context, "counter", _counter);
570                    put(context, "dataFactory", _dataFactory);
571                    put(context, "dateUtil", DateUtil_IW.getInstance());
572                    put(context, "defaultUserId", defaultUser.getCompanyId());
573                    put(context, "maxDLFileEntrySize", _maxDLFileEntrySize);
574                    put(context, "maxBlogsEntryCommentCount", _maxBlogsEntryCommentCount);
575                    put(context, "maxBlogsEntryCount", _maxBlogsEntryCount);
576                    put(context, "maxDDLRecordCount", _maxDDLRecordCount);
577                    put(context, "maxDDLRecordSetCount", _maxDDLRecordSetCount);
578                    put(context, "maxDLFileEntryCount", _maxDLFileEntryCount);
579                    put(context, "maxDLFolderCount", _maxDLFolderCount);
580                    put(context, "maxDLFolderDepth", _maxDLFolderDepth);
581                    put(context, "maxGroupCount", _maxGroupCount);
582                    put(context, "maxJournalArticleCount", _maxJournalArticleCount);
583                    put(context, "maxMBCategoryCount", _maxMBCategoryCount);
584                    put(context, "maxMBMessageCount", _maxMBMessageCount);
585                    put(context, "maxMBThreadCount", _maxMBThreadCount);
586                    put(context, "maxUserCount", _maxUserCount);
587                    put(context, "maxUserToGroupCount", _maxUserToGroupCount);
588                    put(context, "maxWikiNodeCount", _maxWikiNodeCount);
589                    put(context, "maxWikiPageCommentCount", _maxWikiPageCommentCount);
590                    put(context, "maxWikiPageCount", _maxWikiPageCount);
591                    put(context, "portalUUIDUtil", SequentialUUID.getSequentialUUID());
592                    put(context, "sampleSQLBuilder", this);
593                    put(context, "stringUtil", StringUtil_IW.getInstance());
594                    put(context, "userScreenNameIncrementer", _userScreenNameIncrementer);
595                    put(context, "writerBlogsCSV", _writerBlogsCSV);
596                    put(context, "writerCompanyCSV", _writerCompanyCSV);
597                    put(context, "writerDocumentLibraryCSV", _writerDocumentLibraryCSV);
598                    put(context, "writerDynamicDataListsCSV", _writerDynamicDataListsCSV);
599                    put(context, "writerLayoutCSV", _writerLayoutCSV);
600                    put(context, "writerMessageBoardsCSV", _writerMessageBoardsCSV);
601                    put(context, "writerRepositoryCSV", _writerRepositoryCSV);
602                    put(context, "writerUserCSV", _writerUserCSV);
603                    put(context, "writerWikiCSV", _writerWikiCSV);
604    
605                    return context;
606            }
607    
608            protected File getInsertSQLFile(String tableName) {
609                    return new File(_tempDir, tableName + ".sql");
610            }
611    
612            protected void mergeSQL() throws IOException {
613                    File outputFile = new File(_outputDir + "/sample-" + _dbType + ".sql");
614    
615                    FileOutputStream fileOutputStream = null;
616                    FileChannel fileChannel = null;
617    
618                    if (_outputMerge) {
619                            fileOutputStream = new FileOutputStream(outputFile);
620                            fileChannel = fileOutputStream.getChannel();
621                    }
622    
623                    Set<Map.Entry<String, StringBundler>> insertSQLs =
624                            _insertSQLs.entrySet();
625    
626                    for (Map.Entry<String, StringBundler> entry : insertSQLs) {
627                            String tableName = entry.getKey();
628    
629                            String sql = _db.buildSQL(entry.getValue().toString());
630    
631                            writeToInsertSQLFile(tableName, sql);
632    
633                            Writer insertSQLWriter = _insertSQLWriters.remove(tableName);
634    
635                            insertSQLWriter.write(";\n");
636    
637                            insertSQLWriter.close();
638    
639                            if (_outputMerge) {
640                                    File insertSQLFile = getInsertSQLFile(tableName);
641    
642                                    FileInputStream insertSQLFileInputStream = new FileInputStream(
643                                            insertSQLFile);
644    
645                                    FileChannel insertSQLFileChannel =
646                                            insertSQLFileInputStream.getChannel();
647    
648                                    insertSQLFileChannel.transferTo(
649                                            0, insertSQLFileChannel.size(), fileChannel);
650    
651                                    insertSQLFileChannel.close();
652    
653                                    insertSQLFile.delete();
654                            }
655                    }
656    
657                    Writer writer = null;
658    
659                    if (_outputMerge) {
660                            writer = new OutputStreamWriter(fileOutputStream);
661                    }
662                    else {
663                            writer = new FileWriter(getInsertSQLFile("others"));
664                    }
665    
666                    for (String sql : _otherSQLs) {
667                            sql = _db.buildSQL(sql);
668    
669                            writer.write(sql);
670                            writer.write(StringPool.NEW_LINE);
671                    }
672    
673                    writer.close();
674    
675                    File outputFolder = new File(_outputDir, "output");
676    
677                    if (!_outputMerge && !_tempDir.renameTo(outputFolder)) {
678    
679                            // This will only happen when temp and output folders are on
680                            // different file systems
681    
682                            FileUtil.copyDirectory(_tempDir, outputFolder);
683                    }
684            }
685    
686            protected void processTemplate(String name, Map<String, Object> context)
687                    throws Exception {
688    
689                    FreeMarkerUtil.process(name, context, _writerSampleSQL);
690            }
691    
692            protected void put(Map<String, Object> context, String key, Object value) {
693                    context.put(key, value);
694            }
695    
696            protected void writeToInsertSQLFile(String tableName, String sql)
697                    throws IOException {
698    
699                    Writer writer = _insertSQLWriters.get(tableName);
700    
701                    if (writer == null) {
702                            File file = getInsertSQLFile(tableName);
703    
704                            writer = createFileWriter(file);
705    
706                            _insertSQLWriters.put(tableName, writer);
707                    }
708    
709                    writer.write(sql);
710            }
711    
712            private static final int _PIPE_BUFFER_SIZE = 16 * 1024 * 1024;
713    
714            private static final String _TPL_ROOT =
715                    "com/liferay/portal/tools/samplesqlbuilder/dependencies/";
716    
717            private static final int _WRITER_BUFFER_SIZE = 16 * 1024;
718    
719            private SimpleCounter _counter;
720            private DataFactory _dataFactory;
721            private DB _db;
722            private String _dbType;
723            private SimpleCounter _dlDateCounter;
724            private Map<String, StringBundler> _insertSQLs =
725                    new ConcurrentHashMap<String, StringBundler>();
726            private Map<String, Writer> _insertSQLWriters =
727                    new ConcurrentHashMap<String, Writer>();
728            private int _maxBlogsEntryCommentCount;
729            private int _maxBlogsEntryCount;
730            private int _maxDDLRecordCount;
731            private int _maxDDLRecordSetCount;
732            private int _maxDLFileEntryCount;
733            private int _maxDLFileEntrySize;
734            private int _maxDLFolderCount;
735            private int _maxDLFolderDepth;
736            private int _maxGroupCount;
737            private int _maxJournalArticleCount;
738            private int _maxMBCategoryCount;
739            private int _maxMBMessageCount;
740            private int _maxMBThreadCount;
741            private int _maxUserCount;
742            private int _maxUserToGroupCount;
743            private int _maxWikiNodeCount;
744            private int _maxWikiPageCommentCount;
745            private int _maxWikiPageCount;
746            private int _optimizeBufferSize;
747            private List<String> _otherSQLs = new ArrayList<String>();
748            private String _outputDir;
749            private boolean _outputMerge;
750            private SimpleCounter _permissionCounter;
751            private SimpleCounter _resourceCodeCounter;
752            private SimpleCounter _resourceCounter;
753            private SimpleCounter _resourcePermissionCounter;
754            private boolean _securityEnabled;
755            private SimpleCounter _socialActivityCounter;
756            private File _tempDir;
757            private String _tplBlogsEntry = _TPL_ROOT + "blogs_entry.ftl";
758            private String _tplDDLRecord = _TPL_ROOT + "ddl_record.ftl";
759            private String _tplDLFileEntry = _TPL_ROOT + "dl_file_entry.ftl";
760            private String _tplDLFolder = _TPL_ROOT + "dl_folder.ftl";
761            private String _tplDLFolders = _TPL_ROOT + "dl_folders.ftl";
762            private String _tplGroup = _TPL_ROOT + "group.ftl";
763            private String _tplMBCategory = _TPL_ROOT + "mb_category.ftl";
764            private String _tplMBMessage = _TPL_ROOT + "mb_message.ftl";;
765            private String _tplResourcePermission =
766                    _TPL_ROOT + "resource_permission.ftl";
767            private String _tplSample = _TPL_ROOT + "sample.ftl";
768            private String _tplSecurity = _TPL_ROOT + "security.ftl";
769            private String _tplUser = _TPL_ROOT + "user.ftl";
770            private String _tplWikiPage = _TPL_ROOT + "wiki_page.ftl";
771            private SimpleCounter _userScreenNameIncrementer;
772            private Writer _writerBlogsCSV;
773            private Writer _writerCompanyCSV;
774            private Writer _writerDocumentLibraryCSV;
775            private Writer _writerDynamicDataListsCSV;
776            private Writer _writerLayoutCSV;
777            private Writer _writerMessageBoardsCSV;
778            private Writer _writerRepositoryCSV;
779            private Writer _writerSampleSQL;
780            private Writer _writerUserCSV;
781            private Writer _writerWikiCSV;
782    
783    }