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.kernel.upgrade;
016    
017    import com.liferay.portal.kernel.portlet.PortletPreferencesFactoryUtil;
018    
019    import java.util.Map;
020    
021    import javax.portlet.PortletPreferences;
022    
023    /**
024     * @author Marcellus Tavares
025     */
026    public abstract class RenameUpgradePortletPreferences
027            extends BaseUpgradePortletPreferences {
028    
029            protected abstract Map<String, String> getPreferenceNamesMap();
030    
031            @Override
032            protected String upgradePreferences(
033                            long companyId, long ownerId, int ownerType, long plid,
034                            String portletId, String xml)
035                    throws Exception {
036    
037                    PortletPreferences preferences = PortletPreferencesFactoryUtil.fromXML(
038                            companyId, ownerId, ownerType, plid, portletId, xml);
039    
040                    Map<String, String[]> preferencesMap = preferences.getMap();
041    
042                    Map<String, String> preferenceNamesMap = getPreferenceNamesMap();
043    
044                    for (Map.Entry<String, String> entry : preferenceNamesMap.entrySet()) {
045                            String name = entry.getKey();
046    
047                            String[] values = preferencesMap.get(name);
048    
049                            if (values == null) {
050                                    continue;
051                            }
052    
053                            preferences.reset(name);
054    
055                            preferences.setValues(entry.getValue(), values);
056                    }
057    
058                    return PortletPreferencesFactoryUtil.toXML(preferences);
059            }
060    
061    }