1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.upgrade.v5_1_0.util;
16  
17  import com.liferay.portal.kernel.upgrade.util.BaseUpgradeColumnImpl;
18  import com.liferay.portal.kernel.upgrade.util.UpgradeColumn;
19  import com.liferay.portal.kernel.util.StringPool;
20  import com.liferay.portal.kernel.util.Validator;
21  import com.liferay.portlet.blogs.util.BlogsUtil;
22  
23  import java.util.HashSet;
24  import java.util.Set;
25  
26  /**
27   * <a href="BlogsEntryUrlTitleUpgradeColumnImpl.java.html"><b><i>View Source</i>
28   * </b></a>
29   *
30   * @author Brian Wing Shun Chan
31   */
32  public class BlogsEntryUrlTitleUpgradeColumnImpl extends BaseUpgradeColumnImpl {
33  
34      public BlogsEntryUrlTitleUpgradeColumnImpl(
35          UpgradeColumn entryIdColumn, UpgradeColumn titleColumn) {
36  
37          super("urlTitle");
38  
39          _entryIdColumn = entryIdColumn;
40          _titleColumn = titleColumn;
41          _urlTitles = new HashSet<String>();
42      }
43  
44      public Object getNewValue(Object oldValue) throws Exception {
45          //String oldUrlTitle = (String)oldValue;
46          String oldUrlTitle = StringPool.BLANK;
47  
48          String newUrlTitle = oldUrlTitle;
49  
50          if (Validator.isNull(oldUrlTitle)) {
51              long entryId = ((Long)_entryIdColumn.getOldValue()).longValue();
52  
53              String title = (String)_titleColumn.getOldValue();
54  
55              newUrlTitle = getUrlTitle(entryId, title);
56  
57              _urlTitles.add(newUrlTitle);
58          }
59  
60          return newUrlTitle;
61      }
62  
63      protected String getUrlTitle(long entryId, String title) {
64          String urlTitle = BlogsUtil.getUrlTitle(entryId, title);
65  
66          String newUrlTitle = urlTitle;
67  
68          for (int i = 1;; i++) {
69              if (!_urlTitles.contains(newUrlTitle)) {
70                  break;
71              }
72  
73              newUrlTitle = urlTitle + "_" + i;
74          }
75  
76          return newUrlTitle;
77      }
78  
79      private UpgradeColumn _entryIdColumn;
80      private UpgradeColumn _titleColumn;
81      private Set<String> _urlTitles;
82  
83  }