001    /**
002     * Copyright (c) 2000-2010 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.documentlibrary.service.DLServiceUtil;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.upgrade.util.BaseUpgradeColumnImpl;
022    import com.liferay.portal.kernel.upgrade.util.UpgradeColumn;
023    import com.liferay.portal.model.CompanyConstants;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public class MBMessageAttachmentsUpgradeColumnImpl
029            extends BaseUpgradeColumnImpl {
030    
031            public MBMessageAttachmentsUpgradeColumnImpl(
032                    UpgradeColumn messageIdColumn, UpgradeColumn companyIdColumn,
033                    UpgradeColumn threadIdColumn) {
034    
035                    super("attachments");
036    
037                    _messageIdColumn = messageIdColumn;
038                    _companyIdColumn = companyIdColumn;
039                    _threadIdColumn = threadIdColumn;
040            }
041    
042            public Object getNewValue(Object oldValue) throws Exception {
043                    Boolean attachments = (Boolean)oldValue;
044    
045                    if (attachments.booleanValue()) {
046                            Long oldMessageId = (Long)_messageIdColumn.getOldValue();
047                            String oldCompanyId = (String)_companyIdColumn.getOldValue();
048                            Long oldThreadId = (Long)_threadIdColumn.getOldValue();
049    
050                            Long newMessageId = (Long)_messageIdColumn.getNewValue();
051                            Long newCompanyId = (Long)_companyIdColumn.getNewValue();
052                            Long newThreadId = (Long)_threadIdColumn.getNewValue();
053    
054                            try {
055                                    DLServiceUtil.addDirectory(
056                                            newCompanyId.longValue(), CompanyConstants.SYSTEM,
057                                            "messageboards/" + newThreadId);
058    
059                                    DLLocalServiceUtil.move(
060                                            "/" + oldCompanyId +
061                                                    "/documentlibrary/system/messageboards/" + oldThreadId +
062                                                            "/" + oldMessageId,
063                                            "/" + newCompanyId + "/documentlibrary/0/messageboards/" +
064                                                    newThreadId + "/" + newMessageId);
065                            }
066                            catch (Exception e) {
067                                    _log.error(e.getMessage());
068                            }
069                    }
070    
071                    return attachments;
072            }
073    
074            private static Log _log = LogFactoryUtil.getLog(
075                    MBMessageAttachmentsUpgradeColumnImpl.class);
076    
077            private UpgradeColumn _messageIdColumn;
078            private UpgradeColumn _companyIdColumn;
079            private UpgradeColumn _threadIdColumn;
080    
081    }