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.upgrade.v6_1_0;
016    
017    import com.liferay.portal.kernel.upgrade.BaseUpgradePortletPreferences;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.StringUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portlet.PortletPreferencesFactoryUtil;
022    
023    import java.util.HashMap;
024    import java.util.Map;
025    
026    import javax.portlet.PortletPreferences;
027    
028    /**
029     * @author Eudaldo Alonso
030     */
031    public class UpgradeCamelCasePortletPreferences
032            extends BaseUpgradePortletPreferences {
033    
034            public UpgradeCamelCasePortletPreferences() {
035                    _camelCasePreferenceNames.put(
036                            "lfr-app-show-share-with-friends-link",
037                            "lfrAppShowShareWithFriendsLink");
038                    _camelCasePreferenceNames.put(
039                            "lfr-facebook-api-key", "lfrFacebookApiKey");
040                    _camelCasePreferenceNames.put(
041                            "lfr-facebook-canvas-page-url", "lfrFacebookCanvasPageUrl");
042                    _camelCasePreferenceNames.put(
043                            "lfr-facebook-show-add-app-link", "lfrFacebookShowAddAppLink");
044                    _camelCasePreferenceNames.put(
045                            "lfr-igoogle-show-add-app-link", "lfrIgoogleShowAddAppLink");
046                    _camelCasePreferenceNames.put(
047                            "lfr-netvibes-show-add-app-link", "lfrNetvibesShowAddAppLink");
048                    _camelCasePreferenceNames.put("lfr-scope-type", "lfrScopeType");
049                    _camelCasePreferenceNames.put("lfr-scope-uuid", "lfrScopeUuid");
050                    _camelCasePreferenceNames.put("lfr-sharing", "lfrSharing");
051                    _camelCasePreferenceNames.put(
052                            "lfr-wap-initial-window-state", "lfrWapInitialWindowState");
053                    _camelCasePreferenceNames.put("lfr-wap-title", "lfrWapTitle");
054                    _camelCasePreferenceNames.put(
055                            "lfr-widget-show-add-app-link", "lfrWidgetShowAddAppLink");
056                    _camelCasePreferenceNames.put("portlet-setup-css", "portletSetupCss");
057                    _camelCasePreferenceNames.put(
058                            "portlet-setup-link-to-layout-uuid",
059                            "portletSetupLinkToLayoutUuid");
060                    _camelCasePreferenceNames.put(
061                            "portlet-setup-show-borders", "portletSetupShowBorders");
062                    _camelCasePreferenceNames.put(
063                            "portlet-setup-use-custom-title", "portletSetupUseCustomTitle");
064            }
065    
066            @Override
067            protected String getUpdatePortletPreferencesWhereClause() {
068                    return StringPool.BLANK;
069            }
070    
071            @Override
072            protected String upgradePreferences(
073                            long companyId, long ownerId, int ownerType, long plid,
074                            String portletId, String xml)
075                    throws Exception {
076    
077                    PortletPreferences portletPreferences =
078                            PortletPreferencesFactoryUtil.fromXML(
079                                    companyId, ownerId, ownerType, plid, portletId, xml);
080    
081                    Map<String, String[]> preferencesMap = portletPreferences.getMap();
082    
083                    for (String oldName : preferencesMap.keySet()) {
084                            String newName = _camelCasePreferenceNames.get(oldName);
085    
086                            if (Validator.isNull(newName)) {
087                                    if (oldName.startsWith(
088                                                    "portlet-setup-supported-clients-mobile-devices-")) {
089    
090                                            newName = StringUtil.replaceFirst(
091                                                    oldName,
092                                                    "portlet-setup-supported-clients-mobile-devices-",
093                                                    "portletSetupSupportedClientsMobileDevices_");
094                                    }
095                                    else if (oldName.startsWith("portlet-setup-title-")) {
096                                            newName = StringUtil.replaceFirst(
097                                                    oldName, "portlet-setup-title-", "portletSetupTitle_");
098                                    }
099                            }
100    
101                            if (Validator.isNotNull(newName)) {
102                                    String[] values = preferencesMap.get(oldName);
103    
104                                    portletPreferences.reset(oldName);
105                                    portletPreferences.setValues(newName, values);
106                            }
107                    }
108    
109                    return PortletPreferencesFactoryUtil.toXML(portletPreferences);
110            }
111    
112            private Map<String, String> _camelCasePreferenceNames =
113                    new HashMap<String, String>();
114    
115    }