1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.upgrade.v5_1_0.util;
24  
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.upgrade.util.BaseUpgradeColumnImpl;
28  import com.liferay.portal.upgrade.util.UpgradeColumn;
29  import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
30  
31  import java.util.HashSet;
32  import java.util.Set;
33  
34  /**
35   * <a href="BlogsEntryUrlTitleUpgradeColumnImpl.java.html"><b><i>View Source</i>
36   * </b></a>
37   *
38   * @author Brian Wing Shun Chan
39   */
40  public class BlogsEntryUrlTitleUpgradeColumnImpl extends BaseUpgradeColumnImpl {
41  
42      public BlogsEntryUrlTitleUpgradeColumnImpl(
43          UpgradeColumn entryIdColumn, UpgradeColumn titleColumn) {
44  
45          super("urlTitle");
46  
47          _entryIdColumn = entryIdColumn;
48          _titleColumn = titleColumn;
49          _urlTitles = new HashSet<String>();
50      }
51  
52      public Object getNewValue(Object oldValue) throws Exception {
53          //String oldUrlTitle = (String)oldValue;
54          String oldUrlTitle = StringPool.BLANK;
55  
56          String newUrlTitle = oldUrlTitle;
57  
58          if (Validator.isNull(oldUrlTitle)) {
59              long entryId = ((Long)_entryIdColumn.getOldValue()).longValue();
60  
61              String title = (String)_titleColumn.getOldValue();
62  
63              newUrlTitle = getUrlTitle(entryId, title);
64  
65              _urlTitles.add(newUrlTitle);
66          }
67  
68          return newUrlTitle;
69      }
70  
71      protected String getUrlTitle(long entryId, String title) {
72          String urlTitle = BlogsEntryLocalServiceUtil.getUrlTitle(
73              entryId, title);
74  
75          String newUrlTitle = new String(urlTitle);
76  
77          for (int i = 1;; i++) {
78              if (!_urlTitles.contains(newUrlTitle)) {
79                  break;
80              }
81  
82              newUrlTitle = urlTitle + "_" + i;
83          }
84  
85          return newUrlTitle;
86      }
87  
88      private UpgradeColumn _entryIdColumn;
89      private UpgradeColumn _titleColumn;
90      private Set<String> _urlTitles;
91  
92  }