001
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
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
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 }