1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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  }