001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.upgrade.v4_3_0.util;
016    
017    import com.liferay.documentlibrary.service.DLLocalServiceUtil;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.upgrade.util.UpgradeColumn;
021    import com.liferay.portal.kernel.upgrade.util.ValueMapper;
022    import com.liferay.portal.kernel.upgrade.util.ValueMapperFactoryUtil;
023    import com.liferay.portal.upgrade.util.PKUpgradeColumnImpl;
024    
025    import java.util.HashSet;
026    import java.util.Set;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class DLFileEntryIdUpgradeColumnImpl extends PKUpgradeColumnImpl {
032    
033            public DLFileEntryIdUpgradeColumnImpl(
034                    UpgradeColumn companyIdColumn, UpgradeColumn folderIdColumn,
035                    UpgradeColumn nameColumn) {
036    
037                    super("fileEntryId", false);
038    
039                    _companyIdColumn = companyIdColumn;
040                    _folderIdColumn = folderIdColumn;
041                    _nameColumn = nameColumn;
042                    _dlFileEntryIdMapper = ValueMapperFactoryUtil.getValueMapper();
043                    _movedFolderIds = new HashSet<Long>();
044            }
045    
046            public Object getNewValue(Object oldValue) throws Exception {
047                    Object newValue = super.getNewValue(oldValue);
048    
049                    String oldCompanyId = (String)_companyIdColumn.getOldValue();
050                    Long oldFolderId = (Long)_folderIdColumn.getOldValue();
051    
052                    Long newCompanyId = (Long)_companyIdColumn.getNewValue();
053                    Long newFolderId = (Long)_folderIdColumn.getNewValue();
054    
055                    String name = (String)_nameColumn.getOldValue();
056    
057                    String oldPageIdValue =
058                            "{folderId=" + oldFolderId + ", name=" + name + "}";
059    
060                    _dlFileEntryIdMapper.mapValue(oldPageIdValue, newValue);
061    
062                    if (!_movedFolderIds.contains(oldFolderId)) {
063                            try {
064                                    DLLocalServiceUtil.move(
065                                            "/" + oldCompanyId + "/documentlibrary/" + oldFolderId,
066                                            "/" + newCompanyId + "/documentlibrary/" + newFolderId);
067                            }
068                            catch (Exception e) {
069                                    _log.error(e.getMessage());
070                            }
071    
072                            _movedFolderIds.add(oldFolderId);
073                    }
074    
075                    return newValue;
076            }
077    
078            public ValueMapper getValueMapper() {
079                    return _dlFileEntryIdMapper;
080            }
081    
082            private static Log _log = LogFactoryUtil.getLog(
083                    DLFileEntryIdUpgradeColumnImpl.class);
084    
085            private UpgradeColumn _companyIdColumn;
086            private UpgradeColumn _folderIdColumn;
087            private UpgradeColumn _nameColumn;
088            private ValueMapper _dlFileEntryIdMapper;
089            private Set<Long> _movedFolderIds;
090    
091    }