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