1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.upgrade.v4_4_0.util;
21  
22  import com.liferay.portal.kernel.util.FileUtil;
23  import com.liferay.portal.kernel.util.StringPool;
24  import com.liferay.portal.kernel.util.Validator;
25  import com.liferay.portal.upgrade.util.BaseUpgradeColumnImpl;
26  import com.liferay.portal.upgrade.util.UpgradeColumn;
27  import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
28  
29  import java.util.Set;
30  
31  /**
32   * <a href="DLFileEntryTitleColumnImpl.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Alexander Chow
35   *
36   */
37  public class DLFileEntryTitleColumnImpl extends BaseUpgradeColumnImpl {
38  
39      public DLFileEntryTitleColumnImpl(
40          UpgradeColumn groupIdColumn, UpgradeColumn folderIdColumn,
41          UpgradeColumn nameColumn, Set<String> distinctTitles) {
42  
43          super("title", null);
44  
45          _groupIdColumn = groupIdColumn;
46          _folderIdColumn = folderIdColumn;
47          _nameColumn = nameColumn;
48          _distinctTitles = distinctTitles;
49      }
50  
51      public Object getNewValue(Object oldValue) throws Exception {
52          String newTitle = (String)oldValue;
53  
54          String name = (String)_nameColumn.getOldValue();
55          String extension = FileUtil.getExtension(name);
56  
57          newTitle = DLFileEntryImpl.stripExtension(name, newTitle);
58  
59          while (_distinctTitles.contains(_getKey(newTitle, extension))) {
60              _counter++;
61  
62              StringBuilder sb = new StringBuilder();
63  
64              sb.append(newTitle);
65              sb.append(StringPool.SPACE);
66              sb.append(_counter);
67  
68              newTitle = sb.toString();
69          }
70  
71          _distinctTitles.add(_getKey(newTitle, extension));
72  
73          return newTitle;
74      }
75  
76      private String _getKey(String title, String extension) {
77          StringBuilder sb = new StringBuilder();
78  
79          sb.append(_groupIdColumn.getOldValue());
80          sb.append(StringPool.UNDERLINE);
81          sb.append(_folderIdColumn.getOldValue());
82          sb.append(StringPool.UNDERLINE);
83          sb.append(title);
84  
85          if (Validator.isNotNull(extension)) {
86              sb.append(StringPool.PERIOD);
87              sb.append(extension);
88          }
89  
90          return sb.toString();
91      }
92  
93      private UpgradeColumn _groupIdColumn;
94      private UpgradeColumn _folderIdColumn;
95      private UpgradeColumn _nameColumn;
96      private int _counter = 0;
97      private Set<String> _distinctTitles;
98  
99  }