001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.upgrade.util;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncBufferedWriter;
018    import com.liferay.portal.kernel.util.FileUtil;
019    import com.liferay.portal.kernel.util.StringPool;
020    
021    import java.io.FileWriter;
022    
023    import java.util.Iterator;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public class ValueMapperUtil {
029    
030            public static void persist(
031                            ValueMapper valueMapper, String tmpDir, String fileName)
032                    throws Exception {
033    
034                    FileUtil.mkdirs(tmpDir);
035    
036                    UnsyncBufferedWriter unsyncBufferedWriter = new UnsyncBufferedWriter(
037                            new FileWriter(tmpDir + "/" + fileName + ".txt"));
038    
039                    try {
040                            Iterator<Object> itr = valueMapper.iterator();
041    
042                            while (itr.hasNext()) {
043                                    Object oldValue = itr.next();
044    
045                                    Object newValue = valueMapper.getNewValue(oldValue);
046    
047                                    unsyncBufferedWriter.write(
048                                            oldValue + StringPool.EQUAL + newValue);
049    
050                                    if (itr.hasNext()) {
051                                            unsyncBufferedWriter.write(StringPool.NEW_LINE);
052                                    }
053                            }
054                    }
055                    finally {
056                            unsyncBufferedWriter.close();
057                    }
058            }
059    
060    }