001    /**
002     * Copyright (c) 2000-2010 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.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    }