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.ArrayUtil;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.PropsKeys;
021    import com.liferay.portal.kernel.util.PropsUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.util.PortletKeys;
026    import com.liferay.portlet.PortletPreferencesFactoryUtil;
027    
028    import javax.portlet.PortletPreferences;
029    import javax.portlet.ReadOnlyException;
030    
031    /**
032     * @author Iv??n Zaera
033     */
034    public class UpgradeMessageBoards extends BaseUpgradePortletPreferences {
035    
036            protected String getEmailSignatureSeparator(
037                    PortletPreferences portletPreferences) {
038    
039                    boolean emailHtmlFormat = _MESSAGE_BOARDS_EMAIL_HTML_FORMAT;
040    
041                    String emailHtmlFormatString = portletPreferences.getValue(
042                            "emailHtmlFormat", StringPool.BLANK);
043    
044                    if (Validator.isNotNull(emailHtmlFormatString)) {
045                            emailHtmlFormat = GetterUtil.getBoolean(emailHtmlFormatString);
046                    }
047    
048                    if (emailHtmlFormat) {
049                            return "<br />--<br />";
050                    }
051    
052                    return "\n--\n";
053            }
054    
055            @Override
056            protected String[] getPortletIds() {
057                    return new String[] {PortletKeys.MESSAGE_BOARDS};
058            }
059    
060            protected void upgradeEmailSignature(
061                            PortletPreferences portletPreferences,
062                            String emailMessageBodyPortletPreferencesKey,
063                            String emailMessageSignaturePortletPreferencesKey)
064                    throws ReadOnlyException {
065    
066                    String emailMessageSignature = portletPreferences.getValue(
067                            emailMessageSignaturePortletPreferencesKey, StringPool.BLANK);
068    
069                    if (Validator.isNotNull(emailMessageSignature)) {
070                            String emailMessageBody = portletPreferences.getValue(
071                                    emailMessageBodyPortletPreferencesKey, StringPool.BLANK);
072    
073                            String signatureSeparator = getEmailSignatureSeparator(
074                                    portletPreferences);
075    
076                            emailMessageBody += signatureSeparator + emailMessageSignature;
077    
078                            portletPreferences.setValue(
079                                    emailMessageBodyPortletPreferencesKey, emailMessageBody);
080                    }
081    
082                    portletPreferences.reset(emailMessageSignaturePortletPreferencesKey);
083            }
084    
085            @Override
086            protected String upgradePreferences(
087                            long companyId, long ownerId, int ownerType, long plid,
088                            String portletId, String xml)
089                    throws Exception {
090    
091                    PortletPreferences portletPreferences =
092                            PortletPreferencesFactoryUtil.fromXML(
093                                    companyId, ownerId, ownerType, plid, portletId, xml);
094    
095                    upgradeEmailSignature(
096                            portletPreferences, "emailMessageAddedBody",
097                            "emailMessageAddedSignature");
098                    upgradeEmailSignature(
099                            portletPreferences, "emailMessageUpdatedBody",
100                            "emailMessageUpdatedSignature");
101                    upgradeThreadPriorities(portletPreferences);
102    
103                    return PortletPreferencesFactoryUtil.toXML(portletPreferences);
104            }
105    
106            protected void upgradeThreadPriorities(
107                            PortletPreferences portletPreferences)
108                    throws ReadOnlyException {
109    
110                    String[] threadPriorities = portletPreferences.getValues(
111                            "priorities", StringPool.EMPTY_ARRAY);
112    
113                    if (ArrayUtil.isNotEmpty(threadPriorities)) {
114                            String[] upgradedThreadPriorities =
115                                    new String[threadPriorities.length];
116    
117                            for (int i = 0; i < threadPriorities.length; i++) {
118                                    String[] parts = StringUtil.split(threadPriorities[i]);
119    
120                                    upgradedThreadPriorities[i] = StringUtil.merge(
121                                            parts, StringPool.PIPE);
122                            }
123    
124                            portletPreferences.setValues(
125                                    "priorities", upgradedThreadPriorities);
126                    }
127            }
128    
129            private static final boolean _MESSAGE_BOARDS_EMAIL_HTML_FORMAT =
130                    GetterUtil.getBoolean(
131                            PropsUtil.get(PropsKeys.MESSAGE_BOARDS_EMAIL_HTML_FORMAT));
132    
133    }