1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.upgrade.v4_4_0.util;
21  
22  import com.liferay.portal.kernel.util.StringPool;
23  import com.liferay.portal.upgrade.util.BaseUpgradeColumnImpl;
24  import com.liferay.portal.upgrade.util.UpgradeColumn;
25  
26  import java.util.HashSet;
27  import java.util.Set;
28  
29  /**
30   * <a href="DLFolderNameColumnImpl.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Alexander Chow
33   *
34   */
35  public class DLFolderNameColumnImpl extends BaseUpgradeColumnImpl {
36  
37      public DLFolderNameColumnImpl(
38          UpgradeColumn groupIdColumn, UpgradeColumn parentFolderIdColumn) {
39  
40          super("name", null);
41  
42          _groupIdColumn = groupIdColumn;
43          _parentFolderIdColumn = parentFolderIdColumn;
44      }
45  
46      public Set<String> getDistintNames() {
47          return _distinctNames;
48      }
49  
50      public Object getNewValue(Object oldValue) throws Exception {
51          String newName = (String)oldValue;
52  
53          while (_distinctNames.contains(_getKey(newName))) {
54              _counter++;
55  
56              newName = newName + StringPool.SPACE + _counter;
57          }
58  
59          _distinctNames.add(_getKey(newName));
60  
61          return newName;
62      }
63  
64      private String _getKey(String name) {
65          StringBuilder sb = new StringBuilder();
66  
67          sb.append(_groupIdColumn.getOldValue());
68          sb.append(StringPool.UNDERLINE);
69          sb.append(_parentFolderIdColumn.getOldValue());
70          sb.append(StringPool.UNDERLINE);
71          sb.append(name);
72  
73          return sb.toString();
74      }
75  
76      private UpgradeColumn _groupIdColumn;
77      private UpgradeColumn _parentFolderIdColumn;
78      private int _counter = 0;
79      private Set<String> _distinctNames = new HashSet<String>();
80  
81  }