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.v4_4_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.FileUtil;
20  import com.liferay.portal.kernel.util.StringBundler;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.kernel.util.Validator;
23  
24  import java.util.Set;
25  
26  /**
27   * <a href="DLFileEntryTitleColumnImpl.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Alexander Chow
30   */
31  public class DLFileEntryTitleColumnImpl extends BaseUpgradeColumnImpl {
32  
33      public DLFileEntryTitleColumnImpl(
34          UpgradeColumn groupIdColumn, UpgradeColumn folderIdColumn,
35          UpgradeColumn nameColumn, Set<String> distinctTitles) {
36  
37          super("title", null);
38  
39          _groupIdColumn = groupIdColumn;
40          _folderIdColumn = folderIdColumn;
41          _nameColumn = nameColumn;
42          _distinctTitles = distinctTitles;
43      }
44  
45      public Object getNewValue(Object oldValue) throws Exception {
46          String newTitle = (String)oldValue;
47  
48          String name = (String)_nameColumn.getOldValue();
49          String extension = FileUtil.getExtension(name);
50  
51          newTitle = _stripExtension(name, newTitle);
52  
53          while (_distinctTitles.contains(_getKey(newTitle, extension))) {
54              _counter++;
55  
56              newTitle = newTitle.concat(StringPool.SPACE).concat(
57                  String.valueOf(_counter));
58          }
59  
60          _distinctTitles.add(_getKey(newTitle, extension));
61  
62          return newTitle;
63      }
64  
65      private String _getKey(String title, String extension) {
66          StringBundler sb = new StringBundler();
67  
68          sb.append(_groupIdColumn.getOldValue());
69          sb.append(StringPool.UNDERLINE);
70          sb.append(_folderIdColumn.getOldValue());
71          sb.append(StringPool.UNDERLINE);
72          sb.append(title);
73  
74          if (Validator.isNotNull(extension)) {
75              sb.append(StringPool.PERIOD);
76              sb.append(extension);
77          }
78  
79          return sb.toString();
80      }
81  
82      private String _stripExtension(String name, String title) {
83          String extension = FileUtil.getExtension(name);
84  
85          if (Validator.isNull(extension)) {
86              return title;
87          }
88  
89          int pos = title.toLowerCase().lastIndexOf(
90              StringPool.PERIOD + extension);
91  
92          if (pos > 0) {
93              title = title.substring(0, pos);
94          }
95  
96          return title;
97      }
98  
99      private UpgradeColumn _groupIdColumn;
100     private UpgradeColumn _folderIdColumn;
101     private UpgradeColumn _nameColumn;
102     private int _counter = 0;
103     private Set<String> _distinctTitles;
104 
105 }