001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.upgrade.v4_4_0.util;
016    
017    import com.liferay.portal.kernel.upgrade.util.BaseUpgradeColumnImpl;
018    import com.liferay.portal.kernel.upgrade.util.UpgradeColumn;
019    import com.liferay.portal.kernel.util.StringBundler;
020    import com.liferay.portal.kernel.util.StringPool;
021    
022    import java.util.HashSet;
023    import java.util.Set;
024    
025    /**
026     * @author Alexander Chow
027     */
028    public class DLFolderNameColumnImpl extends BaseUpgradeColumnImpl {
029    
030            public DLFolderNameColumnImpl(
031                    UpgradeColumn groupIdColumn, UpgradeColumn parentFolderIdColumn) {
032    
033                    super("name", null);
034    
035                    _groupIdColumn = groupIdColumn;
036                    _parentFolderIdColumn = parentFolderIdColumn;
037            }
038    
039            public Set<String> getDistintNames() {
040                    return _distinctNames;
041            }
042    
043            public Object getNewValue(Object oldValue) throws Exception {
044                    String newName = (String)oldValue;
045    
046                    while (_distinctNames.contains(_getKey(newName))) {
047                            _counter++;
048    
049                            newName = newName + StringPool.SPACE + _counter;
050                    }
051    
052                    _distinctNames.add(_getKey(newName));
053    
054                    return newName;
055            }
056    
057            private String _getKey(String name) {
058                    StringBundler sb = new StringBundler(5);
059    
060                    sb.append(_groupIdColumn.getOldValue());
061                    sb.append(StringPool.UNDERLINE);
062                    sb.append(_parentFolderIdColumn.getOldValue());
063                    sb.append(StringPool.UNDERLINE);
064                    sb.append(name);
065    
066                    return sb.toString();
067            }
068    
069            private UpgradeColumn _groupIdColumn;
070            private UpgradeColumn _parentFolderIdColumn;
071            private int _counter = 0;
072            private Set<String> _distinctNames = new HashSet<String>();
073    
074    }