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.upgrade.v5_0_0.util;
16  
17  import com.liferay.portal.kernel.upgrade.util.BaseUpgradeColumnImpl;
18  import com.liferay.portal.kernel.upgrade.util.UpgradeColumn;
19  import com.liferay.portal.kernel.util.StringBundler;
20  import com.liferay.portal.kernel.util.StringPool;
21  
22  import java.util.HashSet;
23  import java.util.Set;
24  
25  /**
26   * <a href="IGFolderNameColumnImpl.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Alexander Chow
29   */
30  public class IGFolderNameColumnImpl extends BaseUpgradeColumnImpl {
31  
32      public IGFolderNameColumnImpl(
33          UpgradeColumn groupIdColumn, UpgradeColumn parentFolderIdColumn) {
34  
35          super("name", null);
36  
37          _groupIdColumn = groupIdColumn;
38          _parentFolderIdColumn = parentFolderIdColumn;
39      }
40  
41      public Set<String> getDistintNames() {
42          return _distinctNames;
43      }
44  
45      public Object getNewValue(Object oldValue) throws Exception {
46          String newName = (String)oldValue;
47  
48          while (_distinctNames.contains(_getKey(newName))) {
49              _counter++;
50  
51              newName = newName + StringPool.SPACE + _counter;
52          }
53  
54          _distinctNames.add(_getKey(newName));
55  
56          return newName;
57      }
58  
59      private String _getKey(String name) {
60          StringBundler sb = new StringBundler(5);
61  
62          sb.append(_groupIdColumn.getOldValue());
63          sb.append(StringPool.UNDERLINE);
64          sb.append(_parentFolderIdColumn.getOldValue());
65          sb.append(StringPool.UNDERLINE);
66          sb.append(name);
67  
68          return sb.toString();
69      }
70  
71      private UpgradeColumn _groupIdColumn;
72      private UpgradeColumn _parentFolderIdColumn;
73      private int _counter = 0;
74      private Set<String> _distinctNames = new HashSet<String>();
75  
76  }