001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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    }