1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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 = new String(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  }