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