001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.upgrade.v5_1_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.StringPool;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portlet.blogs.util.BlogsUtil;
022    
023    import java.util.HashSet;
024    import java.util.Set;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class BlogsEntryUrlTitleUpgradeColumnImpl extends BaseUpgradeColumnImpl {
030    
031            public BlogsEntryUrlTitleUpgradeColumnImpl(
032                    UpgradeColumn entryIdColumn, UpgradeColumn titleColumn) {
033    
034                    super("urlTitle");
035    
036                    _entryIdColumn = entryIdColumn;
037                    _titleColumn = titleColumn;
038                    _urlTitles = new HashSet<String>();
039            }
040    
041            public Object getNewValue(Object oldValue) throws Exception {
042                    //String oldUrlTitle = (String)oldValue;
043                    String oldUrlTitle = StringPool.BLANK;
044    
045                    String newUrlTitle = oldUrlTitle;
046    
047                    if (Validator.isNull(oldUrlTitle)) {
048                            long entryId = ((Long)_entryIdColumn.getOldValue()).longValue();
049    
050                            String title = (String)_titleColumn.getOldValue();
051    
052                            newUrlTitle = getUrlTitle(entryId, title);
053    
054                            _urlTitles.add(newUrlTitle);
055                    }
056    
057                    return newUrlTitle;
058            }
059    
060            protected String getUrlTitle(long entryId, String title) {
061                    String urlTitle = BlogsUtil.getUrlTitle(entryId, title);
062    
063                    String newUrlTitle = urlTitle;
064    
065                    for (int i = 1;; i++) {
066                            if (!_urlTitles.contains(newUrlTitle)) {
067                                    break;
068                            }
069    
070                            newUrlTitle = urlTitle + "_" + i;
071                    }
072    
073                    return newUrlTitle;
074            }
075    
076            private UpgradeColumn _entryIdColumn;
077            private UpgradeColumn _titleColumn;
078            private Set<String> _urlTitles;
079    
080    }