001    /**
002     * Copyright (c) 2000-present 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.v7_0_0;
016    
017    import com.liferay.portal.kernel.upgrade.BaseUpgradePortletPreferences;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.portal.util.PortletKeys;
021    import com.liferay.portlet.PortletPreferencesFactoryUtil;
022    
023    import javax.portlet.PortletPreferences;
024    import javax.portlet.ReadOnlyException;
025    
026    /**
027     * @author Ivan Zaera
028     */
029    public class UpgradeWiki extends BaseUpgradePortletPreferences {
030    
031            protected String getEmailSignatureSeparator(
032                    PortletPreferences portletPreferences) {
033    
034                    return StringPool.NEW_LINE;
035            }
036    
037            @Override
038            protected String[] getPortletIds() {
039                    return new String[] {PortletKeys.WIKI};
040            }
041    
042            protected void upgradeEmailSignature(
043                            PortletPreferences portletPreferences,
044                            String emailMessageBodyPortletPreferencesKey,
045                            String emailMessageSignaturePortletPreferencesKey)
046                    throws ReadOnlyException {
047    
048                    String emailMessageSignature = portletPreferences.getValue(
049                            emailMessageSignaturePortletPreferencesKey, StringPool.BLANK);
050    
051                    if (Validator.isNotNull(emailMessageSignature)) {
052                            String emailMessageBody = portletPreferences.getValue(
053                                    emailMessageBodyPortletPreferencesKey, StringPool.BLANK);
054    
055                            String signatureSeparator = getEmailSignatureSeparator(
056                                    portletPreferences);
057    
058                            emailMessageBody += signatureSeparator + emailMessageSignature;
059    
060                            portletPreferences.setValue(
061                                    emailMessageBodyPortletPreferencesKey, emailMessageBody);
062                    }
063    
064                    portletPreferences.reset(emailMessageSignaturePortletPreferencesKey);
065            }
066    
067            @Override
068            protected String upgradePreferences(
069                            long companyId, long ownerId, int ownerType, long plid,
070                            String portletId, String xml)
071                    throws Exception {
072    
073                    PortletPreferences portletPreferences =
074                            PortletPreferencesFactoryUtil.fromXML(
075                                    companyId, ownerId, ownerType, plid, portletId, xml);
076    
077                    upgradeEmailSignature(
078                            portletPreferences, "emailPageAddedBody",
079                            "emailPageAddedSignature");
080                    upgradeEmailSignature(
081                            portletPreferences, "emailPageUpdatedBody",
082                            "emailPageUpdatedSignature");
083    
084                    return PortletPreferencesFactoryUtil.toXML(portletPreferences);
085            }
086    
087    }