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