001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.upgrade;
016    
017    import com.liferay.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 (String name : preferenceNamesMap.keySet()) {
045                            String[] values = preferencesMap.get(name);
046    
047                            if (values == null) {
048                                    continue;
049                            }
050    
051                            preferences.reset(name);
052    
053                            preferences.setValues(preferenceNamesMap.get(name), values);
054                    }
055    
056                    return PortletPreferencesFactoryUtil.toXML(preferences);
057            }
058    
059    }