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