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.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  }