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