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_1;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.portlet.PortletPreferencesFactoryUtil;
019    import com.liferay.portal.kernel.upgrade.BaseUpgradePortletPreferences;
020    import com.liferay.portal.kernel.util.ArrayUtil;
021    import com.liferay.portal.kernel.util.CharPool;
022    import com.liferay.portal.kernel.util.PortletKeys;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.StringUtil;
025    
026    import java.util.Locale;
027    import java.util.Set;
028    
029    import javax.portlet.PortletPreferences;
030    
031    /**
032     * @author Roberto D??az
033     */
034    public class UpgradeMessageBoards extends BaseUpgradePortletPreferences {
035    
036            @Override
037            protected String[] getPortletIds() {
038                    return new String[] {
039                            PortletKeys.MESSAGE_BOARDS, PortletKeys.MESSAGE_BOARDS_ADMIN
040                    };
041            }
042    
043            protected void upgradeLocalizedThreadPriorities(
044                            PortletPreferences portletPreferences)
045                    throws Exception {
046    
047                    Set<Locale> availableLocales = LanguageUtil.getAvailableLocales();
048    
049                    for (Locale availableLocale : availableLocales) {
050                            String key =
051                                    "priorities" + CharPool.UNDERLINE +
052                                            LanguageUtil.getLanguageId(availableLocale);
053    
054                            String[] oldThreadPriorities = portletPreferences.getValues(
055                                    key, StringPool.EMPTY_ARRAY);
056    
057                            if (ArrayUtil.isEmpty(oldThreadPriorities)) {
058                                    continue;
059                            }
060    
061                            String[] newThreadPriorities =
062                                    new String[oldThreadPriorities.length];
063    
064                            for (int i = 0; i < oldThreadPriorities.length; i++) {
065                                    newThreadPriorities[i] = StringUtil.replace(
066                                            oldThreadPriorities[i], CharPool.COMMA, CharPool.PIPE);
067                            }
068    
069                            portletPreferences.setValues(key, newThreadPriorities);
070                    }
071            }
072    
073            @Override
074            protected String upgradePreferences(
075                            long companyId, long ownerId, int ownerType, long plid,
076                            String portletId, String xml)
077                    throws Exception {
078    
079                    PortletPreferences portletPreferences =
080                            PortletPreferencesFactoryUtil.fromXML(
081                                    companyId, ownerId, ownerType, plid, portletId, xml);
082    
083                    upgradeLocalizedThreadPriorities(portletPreferences);
084    
085                    return PortletPreferencesFactoryUtil.toXML(portletPreferences);
086            }
087    
088    }