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_3_0.util;
16  
17  import com.liferay.documentlibrary.service.DLLocalServiceUtil;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.upgrade.util.UpgradeColumn;
21  import com.liferay.portal.kernel.upgrade.util.ValueMapper;
22  import com.liferay.portal.kernel.upgrade.util.ValueMapperFactoryUtil;
23  import com.liferay.portal.upgrade.util.PKUpgradeColumnImpl;
24  
25  import java.util.HashSet;
26  import java.util.Set;
27  
28  /**
29   * <a href="DLFileEntryIdUpgradeColumnImpl.java.html"><b><i>View Source</i></b>
30   * </a>
31   *
32   * @author Brian Wing Shun Chan
33   */
34  public class DLFileEntryIdUpgradeColumnImpl extends PKUpgradeColumnImpl {
35  
36      public DLFileEntryIdUpgradeColumnImpl(
37          UpgradeColumn companyIdColumn, UpgradeColumn folderIdColumn,
38          UpgradeColumn nameColumn) {
39  
40          super("fileEntryId", false);
41  
42          _companyIdColumn = companyIdColumn;
43          _folderIdColumn = folderIdColumn;
44          _nameColumn = nameColumn;
45          _dlFileEntryIdMapper = ValueMapperFactoryUtil.getValueMapper();
46          _movedFolderIds = new HashSet<Long>();
47      }
48  
49      public Object getNewValue(Object oldValue) throws Exception {
50          Object newValue = super.getNewValue(oldValue);
51  
52          String oldCompanyId = (String)_companyIdColumn.getOldValue();
53          Long oldFolderId = (Long)_folderIdColumn.getOldValue();
54  
55          Long newCompanyId = (Long)_companyIdColumn.getNewValue();
56          Long newFolderId = (Long)_folderIdColumn.getNewValue();
57  
58          String name = (String)_nameColumn.getOldValue();
59  
60          String oldPageIdValue =
61              "{folderId=" + oldFolderId + ", name=" + name + "}";
62  
63          _dlFileEntryIdMapper.mapValue(oldPageIdValue, newValue);
64  
65          if (!_movedFolderIds.contains(oldFolderId)) {
66              try {
67                  DLLocalServiceUtil.move(
68                      "/" + oldCompanyId + "/documentlibrary/" + oldFolderId,
69                      "/" + newCompanyId + "/documentlibrary/" + newFolderId);
70              }
71              catch (Exception e) {
72                  _log.error(e.getMessage());
73              }
74  
75              _movedFolderIds.add(oldFolderId);
76          }
77  
78          return newValue;
79      }
80  
81      public ValueMapper getValueMapper() {
82          return _dlFileEntryIdMapper;
83      }
84  
85      private static Log _log = LogFactoryUtil.getLog(
86          DLFileEntryIdUpgradeColumnImpl.class);
87  
88      private UpgradeColumn _companyIdColumn;
89      private UpgradeColumn _folderIdColumn;
90      private UpgradeColumn _nameColumn;
91      private ValueMapper _dlFileEntryIdMapper;
92      private Set<Long> _movedFolderIds;
93  
94  }