1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.upgrade.util;
16  
17  import com.liferay.portal.kernel.io.unsync.UnsyncBufferedWriter;
18  import com.liferay.portal.kernel.util.FileUtil;
19  import com.liferay.portal.kernel.util.StringPool;
20  
21  import java.io.FileWriter;
22  
23  import java.util.Iterator;
24  
25  /**
26   * <a href="ValueMapperUtil.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Brian Wing Shun Chan
29   */
30  public class ValueMapperUtil {
31  
32      public static void persist(
33              ValueMapper valueMapper, String tmpDir, String fileName)
34          throws Exception {
35  
36          FileUtil.mkdirs(tmpDir);
37  
38          UnsyncBufferedWriter unsyncBufferedWriter = new UnsyncBufferedWriter(
39              new FileWriter(tmpDir + "/" + fileName + ".txt"));
40  
41          try {
42              Iterator<Object> itr = valueMapper.iterator();
43  
44              while (itr.hasNext()) {
45                  Object oldValue = itr.next();
46  
47                  Object newValue = valueMapper.getNewValue(oldValue);
48  
49                  unsyncBufferedWriter.write(
50                      oldValue + StringPool.EQUAL + newValue);
51  
52                  if (itr.hasNext()) {
53                      unsyncBufferedWriter.write(StringPool.NEW_LINE);
54                  }
55              }
56          }
57          finally {
58              unsyncBufferedWriter.close();
59          }
60      }
61  
62  }