001    /**
002     * Copyright (c) 2000-2013 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.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            @Override
042            public Object getNewValue(Object oldValue) throws Exception {
043                    //String oldUrlTitle = (String)oldValue;
044                    String oldUrlTitle = StringPool.BLANK;
045    
046                    String newUrlTitle = oldUrlTitle;
047    
048                    if (Validator.isNull(oldUrlTitle)) {
049                            long entryId = ((Long)_entryIdColumn.getOldValue()).longValue();
050    
051                            String title = (String)_titleColumn.getOldValue();
052    
053                            newUrlTitle = getUrlTitle(entryId, title);
054    
055                            _urlTitles.add(newUrlTitle);
056                    }
057    
058                    return newUrlTitle;
059            }
060    
061            protected String getUrlTitle(long entryId, String title) {
062                    String urlTitle = BlogsUtil.getUrlTitle(entryId, title);
063    
064                    String newUrlTitle = urlTitle;
065    
066                    for (int i = 1;; i++) {
067                            if (!_urlTitles.contains(newUrlTitle)) {
068                                    break;
069                            }
070    
071                            newUrlTitle = urlTitle + "_" + i;
072                    }
073    
074                    return newUrlTitle;
075            }
076    
077            private UpgradeColumn _entryIdColumn;
078            private UpgradeColumn _titleColumn;
079            private Set<String> _urlTitles;
080    
081    }