001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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(DDLRecord ddlRecord, DDLRecordSet ddlRecordSet)
278                    throws Exception {
279    
280                    Map<String, Object> context = getContext();
281    
282                    put(context, "ddlRecord", ddlRecord);
283                    put(context, "ddlRecordSet", ddlRecordSet);
284    
285                    processTemplate(_tplDDLRecord, context);
286            }
287    
288            public void insertDDLRecordSet(DDMStructure ddmStructure) throws Exception {
289                    Map<String, Object> context = getContext();
290    
291                    put(context, "ddmStructure", ddmStructure);
292    
293                    processTemplate(_tplDLFolders, context);
294            }
295    
296            public void insertDLFileEntry(
297                            DLFileEntry dlFileEntry, DDMStructure ddmStructure)
298                    throws Exception {
299    
300                    Map<String, Object> context = getContext();
301    
302                    put(context, "ddmStructure", ddmStructure);
303                    put(context, "dlFileEntry", dlFileEntry);
304    
305                    processTemplate(_tplDLFileEntry, context);
306            }
307    
308            public void insertDLFolder(DLFolder dlFolder, DDMStructure ddmStructure)
309                    throws Exception {
310    
311                    Map<String, Object> context = getContext();
312    
313                    put(context, "ddmStructure", ddmStructure);
314                    put(context, "dlFolder", dlFolder);
315    
316                    processTemplate(_tplDLFolder, context);
317            }
318    
319            public void insertDLFolders(
320                            long parentDLFolderId, int dlFolderDepth, DDMStructure ddmStructure)
321                    throws Exception {
322    
323                    Map<String, Object> context = getContext();
324    
325                    put(context, "ddmStructure", ddmStructure);
326                    put(context, "dlFolderDepth", dlFolderDepth);
327                    put(context, "parentDLFolderId", parentDLFolderId);
328    
329                    processTemplate(_tplDLFolders, context);
330            }
331    
332            public void insertGroup(
333                            Group group, List<Layout> privateLayouts,
334                            List<Layout> publicLayouts)
335                    throws Exception {
336    
337                    Map<String, Object> context = getContext();
338    
339                    put(context, "group", group);
340                    put(context, "privateLayouts", privateLayouts);
341                    put(context, "publicLayouts", publicLayouts);
342    
343                    processTemplate(_tplGroup, context);
344            }
345    
346            public void insertJournalArticle(
347                            long groupId, List<Layout> journalArticleLayouts)
348                    throws Exception {
349    
350                    if ((journalArticleLayouts == null) ||
351                            journalArticleLayouts.isEmpty()) {
352    
353                            return;
354                    }
355    
356                    Map<String, Object> context = getContext();
357    
358                    put(context, "groupId", groupId);
359                    put(context, "journalArticleLayouts", journalArticleLayouts);
360    
361                    processTemplate(_tplJournalArticle, context);
362            }
363    
364            public void insertMBCategory(MBCategory mbCategory) throws Exception {
365                    Map<String, Object> context = getContext();
366    
367                    put(context, "mbCategory", mbCategory);
368    
369                    processTemplate(_tplMBCategory, context);
370            }
371    
372            public void insertMBMessage(MBMessage mbMessage) throws Exception {
373                    Map<String, Object> context = getContext();
374    
375                    put(context, "mbMessage", mbMessage);
376    
377                    processTemplate(_tplMBMessage, context);
378            }
379    
380            public void insertResourcePermission(String name, String primKey)
381                    throws Exception {
382    
383                    Map<String, Object> context = getContext();
384    
385                    put(context, "resourceName", name);
386                    put(context, "resourcePrimkey", primKey);
387    
388                    processTemplate(_tplResourcePermission, context);
389            }
390    
391            public void insertSecurity(String name, long primKey) throws Exception {
392                    insertSecurity(name, String.valueOf(primKey));
393            }
394    
395            public void insertSecurity(String name, String primKey) throws Exception {
396                    if (!_securityEnabled) {
397                            return;
398                    }
399    
400                    Map<String, Object> context = getContext();
401    
402                    Resource resource = _dataFactory.addResource(name, primKey);
403    
404                    put(context, "resource", resource);
405    
406                    processTemplate(_tplSecurity, context);
407            }
408    
409            public void insertUser(
410                            Contact contact, Group group, List<Long> groupIds,
411                            List<Long> organizationIds, List<Layout> privateLayouts,
412                            List<Layout> publicLayouts, List<Role> roleIds, User user)
413                    throws Exception {
414    
415                    Map<String, Object> context = getContext();
416    
417                    put(context, "contact", contact);
418                    put(context, "group", group);
419                    put(context, "groupIds", groupIds);
420                    put(context, "organizationIds", organizationIds);
421                    put(context, "privateLayouts", privateLayouts);
422                    put(context, "publicLayouts", publicLayouts);
423                    put(context, "roleIds", roleIds);
424                    put(context, "user", user);
425    
426                    processTemplate(_tplUser, context);
427            }
428    
429            public void insertWikiPage(WikiNode wikiNode, WikiPage wikiPage)
430                    throws Exception {
431    
432                    Map<String, Object> context = getContext();
433    
434                    put(context, "wikiNode", wikiNode);
435                    put(context, "wikiPage", wikiPage);
436    
437                    processTemplate(_tplWikiPage, context);
438            }
439    
440            protected void compressInsertSQL(String insertSQL) throws IOException {
441                    String tableName = insertSQL.substring(0, insertSQL.indexOf(' '));
442    
443                    int pos = insertSQL.indexOf(" values ") + 8;
444    
445                    String values = insertSQL.substring(pos, insertSQL.length() - 1);
446    
447                    StringBundler sb = _insertSQLs.get(tableName);
448    
449                    if ((sb == null) || (sb.index() == 0)) {
450                            sb = new StringBundler();
451    
452                            _insertSQLs.put(tableName, sb);
453    
454                            sb.append("insert into ");
455                            sb.append(insertSQL.substring(0, pos));
456                            sb.append("\n");
457                    }
458                    else {
459                            sb.append(",\n");
460                    }
461    
462                    sb.append(values);
463    
464                    if (sb.index() >= _optimizeBufferSize) {
465                            sb.append(";\n");
466    
467                            String sql = _db.buildSQL(sb.toString());
468    
469                            sb.setIndex(0);
470    
471                            writeToInsertSQLFile(tableName, sql);
472                    }
473            }
474    
475            protected void compressSQL(Reader reader) throws IOException {
476                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
477                            reader);
478    
479                    String s = null;
480    
481                    while ((s = unsyncBufferedReader.readLine()) != null) {
482                            s = s.trim();
483    
484                            if (s.length() > 0) {
485                                    if (s.startsWith("insert into ")) {
486                                            compressInsertSQL(s.substring(12));
487                                    }
488                                    else if (s.length() > 0) {
489                                            _otherSQLs.add(s);
490                                    }
491                            }
492                    }
493    
494                    unsyncBufferedReader.close();
495            }
496    
497            protected Writer createFileWriter(File file) throws IOException {
498                    FileOutputStream fileOutputStream = new FileOutputStream(file);
499    
500                    Writer writer = new OutputStreamWriter(fileOutputStream);
501    
502                    return createUnsyncBufferedWriter(writer);
503            }
504    
505            protected Writer createFileWriter(String fileName) throws IOException {
506                    File file = new File(fileName);
507    
508                    return createFileWriter(file);
509            }
510    
511            protected Writer createUnsyncBufferedWriter(Writer writer) {
512                    return new UnsyncBufferedWriter(writer, _WRITER_BUFFER_SIZE) {
513    
514                            @Override
515                            public void flush() {
516    
517                                    // Disable FreeMarker from flushing
518    
519                            }
520    
521                    };
522            }
523    
524            protected void generateSQL(final CharPipe charPipe) {
525                    final Writer writer = createUnsyncBufferedWriter(charPipe.getWriter());
526    
527                    Thread thread = new Thread() {
528    
529                            @Override
530                            public void run() {
531                                    try {
532                                            _writerSampleSQL = new UnsyncTeeWriter(
533                                                    writer, createFileWriter(_outputDir + "/sample.sql"));
534    
535                                            createSample();
536    
537                                            _writerSampleSQL.close();
538    
539                                            charPipe.close();
540                                    }
541                                    catch (Exception e) {
542                                            e.printStackTrace();
543                                    }
544                            }
545    
546                            protected void createSample() throws Exception {
547                                    _writerBlogsCSV = getWriter("blogs.csv");
548                                    _writerCompanyCSV = getWriter("company.csv");
549                                    _writerDocumentLibraryCSV = getWriter("document_library.csv");
550                                    _writerDynamicDataListsCSV = getWriter(
551                                            "dynamic_data_lists.csv");
552                                    _writerLayoutCSV = getWriter("layout.csv");
553                                    _writerMessageBoardsCSV = getWriter("message_boards.csv");
554                                    _writerRepositoryCSV = getWriter("repository.csv");
555                                    _writerUserCSV = getWriter("user.csv");
556                                    _writerWikiCSV = getWriter("wiki.csv");
557    
558                                    Map<String, Object> context = getContext();
559    
560                                    processTemplate(_tplSample, context);
561    
562                                    _writerBlogsCSV.close();
563                                    _writerCompanyCSV.close();
564                                    _writerDocumentLibraryCSV.close();
565                                    _writerDynamicDataListsCSV.close();
566                                    _writerLayoutCSV.close();
567                                    _writerMessageBoardsCSV.close();
568                                    _writerRepositoryCSV.close();
569                                    _writerUserCSV.close();
570                                    _writerWikiCSV.close();
571                            }
572    
573                            protected Writer getWriter(String fileName) throws Exception {
574                                    return createFileWriter(new File(_outputDir + "/" + fileName));
575                            }
576    
577                    };
578    
579                    thread.start();
580            }
581    
582            protected Map<String, Object> getContext() {
583                    Map<String, Object> context = new HashMap<String, Object>();
584    
585                    Company company = _dataFactory.getCompany();
586                    User defaultUser = _dataFactory.getDefaultUser();
587    
588                    put(context, "companyId", company.getCompanyId());
589                    put(context, "counter", _counter);
590                    put(context, "dataFactory", _dataFactory);
591                    put(context, "dateUtil", DateUtil_IW.getInstance());
592                    put(context, "defaultUserId", defaultUser.getCompanyId());
593                    put(context, "maxDLFileEntrySize", _maxDLFileEntrySize);
594                    put(context, "maxBlogsEntryCommentCount", _maxBlogsEntryCommentCount);
595                    put(context, "maxBlogsEntryCount", _maxBlogsEntryCount);
596                    put(context, "maxDDLRecordCount", _maxDDLRecordCount);
597                    put(context, "maxDDLRecordSetCount", _maxDDLRecordSetCount);
598                    put(context, "maxDLFileEntryCount", _maxDLFileEntryCount);
599                    put(context, "maxDLFolderCount", _maxDLFolderCount);
600                    put(context, "maxDLFolderDepth", _maxDLFolderDepth);
601                    put(context, "maxGroupCount", _maxGroupCount);
602                    put(context, "maxJournalArticleCount", _maxJournalArticleCount);
603                    put(context, "maxMBCategoryCount", _maxMBCategoryCount);
604                    put(context, "maxMBMessageCount", _maxMBMessageCount);
605                    put(context, "maxMBThreadCount", _maxMBThreadCount);
606                    put(context, "maxUserCount", _maxUserCount);
607                    put(context, "maxUserToGroupCount", _maxUserToGroupCount);
608                    put(context, "maxWikiNodeCount", _maxWikiNodeCount);
609                    put(context, "maxWikiPageCommentCount", _maxWikiPageCommentCount);
610                    put(context, "maxWikiPageCount", _maxWikiPageCount);
611                    put(context, "portalUUIDUtil", SequentialUUID.getSequentialUUID());
612                    put(context, "sampleSQLBuilder", this);
613                    put(context, "stringUtil", StringUtil_IW.getInstance());
614                    put(context, "userScreenNameIncrementer", _userScreenNameIncrementer);
615                    put(context, "writerBlogsCSV", _writerBlogsCSV);
616                    put(context, "writerCompanyCSV", _writerCompanyCSV);
617                    put(context, "writerDocumentLibraryCSV", _writerDocumentLibraryCSV);
618                    put(context, "writerDynamicDataListsCSV", _writerDynamicDataListsCSV);
619                    put(context, "writerLayoutCSV", _writerLayoutCSV);
620                    put(context, "writerMessageBoardsCSV", _writerMessageBoardsCSV);
621                    put(context, "writerRepositoryCSV", _writerRepositoryCSV);
622                    put(context, "writerUserCSV", _writerUserCSV);
623                    put(context, "writerWikiCSV", _writerWikiCSV);
624    
625                    return context;
626            }
627    
628            protected File getInsertSQLFile(String tableName) {
629                    return new File(_tempDir, tableName + ".sql");
630            }
631    
632            protected void mergeSQL() throws IOException {
633                    File outputFile = new File(_outputDir + "/sample-" + _dbType + ".sql");
634    
635                    FileOutputStream fileOutputStream = null;
636                    FileChannel fileChannel = null;
637    
638                    if (_outputMerge) {
639                            fileOutputStream = new FileOutputStream(outputFile);
640                            fileChannel = fileOutputStream.getChannel();
641                    }
642    
643                    Set<Map.Entry<String, StringBundler>> insertSQLs =
644                            _insertSQLs.entrySet();
645    
646                    for (Map.Entry<String, StringBundler> entry : insertSQLs) {
647                            String tableName = entry.getKey();
648    
649                            String sql = _db.buildSQL(entry.getValue().toString());
650    
651                            writeToInsertSQLFile(tableName, sql);
652    
653                            Writer insertSQLWriter = _insertSQLWriters.remove(tableName);
654    
655                            insertSQLWriter.write(";\n");
656    
657                            insertSQLWriter.close();
658    
659                            if (_outputMerge) {
660                                    File insertSQLFile = getInsertSQLFile(tableName);
661    
662                                    FileInputStream insertSQLFileInputStream = new FileInputStream(
663                                            insertSQLFile);
664    
665                                    FileChannel insertSQLFileChannel =
666                                            insertSQLFileInputStream.getChannel();
667    
668                                    insertSQLFileChannel.transferTo(
669                                            0, insertSQLFileChannel.size(), fileChannel);
670    
671                                    insertSQLFileChannel.close();
672    
673                                    insertSQLFile.delete();
674                            }
675                    }
676    
677                    Writer writer = null;
678    
679                    if (_outputMerge) {
680                            writer = new OutputStreamWriter(fileOutputStream);
681                    }
682                    else {
683                            writer = new FileWriter(getInsertSQLFile("others"));
684                    }
685    
686                    for (String sql : _otherSQLs) {
687                            sql = _db.buildSQL(sql);
688    
689                            writer.write(sql);
690                            writer.write(StringPool.NEW_LINE);
691                    }
692    
693                    writer.close();
694    
695                    File outputFolder = new File(_outputDir, "output");
696    
697                    if (!_outputMerge && !_tempDir.renameTo(outputFolder)) {
698    
699                            // This will only happen when temp and output folders are on
700                            // different file systems
701    
702                            FileUtil.copyDirectory(_tempDir, outputFolder);
703                    }
704            }
705    
706            protected void processTemplate(String name, Map<String, Object> context)
707                    throws Exception {
708    
709                    FreeMarkerUtil.process(name, context, _writerSampleSQL);
710            }
711    
712            protected void put(Map<String, Object> context, String key, Object value) {
713                    context.put(key, value);
714            }
715    
716            protected void writeToInsertSQLFile(String tableName, String sql)
717                    throws IOException {
718    
719                    Writer writer = _insertSQLWriters.get(tableName);
720    
721                    if (writer == null) {
722                            File file = getInsertSQLFile(tableName);
723    
724                            writer = createFileWriter(file);
725    
726                            _insertSQLWriters.put(tableName, writer);
727                    }
728    
729                    writer.write(sql);
730            }
731    
732            private static final int _PIPE_BUFFER_SIZE = 16 * 1024 * 1024;
733    
734            private static final String _TPL_ROOT =
735                    "com/liferay/portal/tools/samplesqlbuilder/dependencies/";
736    
737            private static final int _WRITER_BUFFER_SIZE = 16 * 1024;
738    
739            private SimpleCounter _counter;
740            private DataFactory _dataFactory;
741            private DB _db;
742            private String _dbType;
743            private SimpleCounter _dlDateCounter;
744            private Map<String, StringBundler> _insertSQLs =
745                    new ConcurrentHashMap<String, StringBundler>();
746            private Map<String, Writer> _insertSQLWriters =
747                    new ConcurrentHashMap<String, Writer>();
748            private int _maxBlogsEntryCommentCount;
749            private int _maxBlogsEntryCount;
750            private int _maxDDLRecordCount;
751            private int _maxDDLRecordSetCount;
752            private int _maxDLFileEntryCount;
753            private int _maxDLFileEntrySize;
754            private int _maxDLFolderCount;
755            private int _maxDLFolderDepth;
756            private int _maxGroupCount;
757            private int _maxJournalArticleCount;
758            private int _maxMBCategoryCount;
759            private int _maxMBMessageCount;
760            private int _maxMBThreadCount;
761            private int _maxUserCount;
762            private int _maxUserToGroupCount;
763            private int _maxWikiNodeCount;
764            private int _maxWikiPageCommentCount;
765            private int _maxWikiPageCount;
766            private int _optimizeBufferSize;
767            private List<String> _otherSQLs = new ArrayList<String>();
768            private String _outputDir;
769            private boolean _outputMerge;
770            private SimpleCounter _permissionCounter;
771            private SimpleCounter _resourceCodeCounter;
772            private SimpleCounter _resourceCounter;
773            private SimpleCounter _resourcePermissionCounter;
774            private boolean _securityEnabled;
775            private SimpleCounter _socialActivityCounter;
776            private File _tempDir;
777            private String _tplBlogsEntry = _TPL_ROOT + "blogs_entry.ftl";
778            private String _tplDDLRecord = _TPL_ROOT + "ddl_record.ftl";
779            private String _tplDLFileEntry = _TPL_ROOT + "dl_file_entry.ftl";
780            private String _tplDLFolder = _TPL_ROOT + "dl_folder.ftl";
781            private String _tplDLFolders = _TPL_ROOT + "dl_folders.ftl";
782            private String _tplGroup = _TPL_ROOT + "group.ftl";
783            private String _tplJournalArticle = _TPL_ROOT + "journal_article.ftl";
784            private String _tplMBCategory = _TPL_ROOT + "mb_category.ftl";
785            private String _tplMBMessage = _TPL_ROOT + "mb_message.ftl";;
786            private String _tplResourcePermission =
787                    _TPL_ROOT + "resource_permission.ftl";
788            private String _tplSample = _TPL_ROOT + "sample.ftl";
789            private String _tplSecurity = _TPL_ROOT + "security.ftl";
790            private String _tplUser = _TPL_ROOT + "user.ftl";
791            private String _tplWikiPage = _TPL_ROOT + "wiki_page.ftl";
792            private SimpleCounter _userScreenNameIncrementer;
793            private Writer _writerBlogsCSV;
794            private Writer _writerCompanyCSV;
795            private Writer _writerDocumentLibraryCSV;
796            private Writer _writerDynamicDataListsCSV;
797            private Writer _writerLayoutCSV;
798            private Writer _writerMessageBoardsCSV;
799            private Writer _writerRepositoryCSV;
800            private Writer _writerSampleSQL;
801            private Writer _writerUserCSV;
802            private Writer _writerWikiCSV;
803    
804    }