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.FileUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    
024    import java.util.Set;
025    
026    /**
027     * @author Alexander Chow
028     */
029    public class DLFileEntryTitleColumnImpl extends BaseUpgradeColumnImpl {
030    
031            public DLFileEntryTitleColumnImpl(
032                    UpgradeColumn groupIdColumn, UpgradeColumn folderIdColumn,
033                    UpgradeColumn nameColumn, Set<String> distinctTitles) {
034    
035                    super("title", null);
036    
037                    _groupIdColumn = groupIdColumn;
038                    _folderIdColumn = folderIdColumn;
039                    _nameColumn = nameColumn;
040                    _distinctTitles = distinctTitles;
041            }
042    
043            public Object getNewValue(Object oldValue) throws Exception {
044                    String newTitle = (String)oldValue;
045    
046                    String name = (String)_nameColumn.getOldValue();
047                    String extension = FileUtil.getExtension(name);
048    
049                    newTitle = _stripExtension(name, newTitle);
050    
051                    while (_distinctTitles.contains(_getKey(newTitle, extension))) {
052                            _counter++;
053    
054                            newTitle = newTitle.concat(StringPool.SPACE).concat(
055                                    String.valueOf(_counter));
056                    }
057    
058                    _distinctTitles.add(_getKey(newTitle, extension));
059    
060                    return newTitle;
061            }
062    
063            private String _getKey(String title, String extension) {
064                    StringBundler sb = new StringBundler();
065    
066                    sb.append(_groupIdColumn.getOldValue());
067                    sb.append(StringPool.UNDERLINE);
068                    sb.append(_folderIdColumn.getOldValue());
069                    sb.append(StringPool.UNDERLINE);
070                    sb.append(title);
071    
072                    if (Validator.isNotNull(extension)) {
073                            sb.append(StringPool.PERIOD);
074                            sb.append(extension);
075                    }
076    
077                    return sb.toString();
078            }
079    
080            private String _stripExtension(String name, String title) {
081                    String extension = FileUtil.getExtension(name);
082    
083                    if (Validator.isNull(extension)) {
084                            return title;
085                    }
086    
087                    int pos = title.toLowerCase().lastIndexOf(
088                            StringPool.PERIOD + extension);
089    
090                    if (pos > 0) {
091                            title = title.substring(0, pos);
092                    }
093    
094                    return title;
095            }
096    
097            private UpgradeColumn _groupIdColumn;
098            private UpgradeColumn _folderIdColumn;
099            private UpgradeColumn _nameColumn;
100            private int _counter = 0;
101            private Set<String> _distinctTitles;
102    
103    }