001    /**
002     * Copyright (c) 2000-2013 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.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    }