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