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.v6_1_0;
016    
017    import com.liferay.portal.kernel.portlet.PortletPreferencesFactoryUtil;
018    import com.liferay.portal.kernel.upgrade.BaseUpgradePortletPreferences;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.kernel.util.Validator;
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 (Map.Entry<String, String[]> entry : preferencesMap.entrySet()) {
084                            String oldName = entry.getKey();
085    
086                            String newName = _camelCasePreferenceNames.get(oldName);
087    
088                            if (Validator.isNull(newName)) {
089                                    if (oldName.startsWith(
090                                                    "portlet-setup-supported-clients-mobile-devices-")) {
091    
092                                            newName = StringUtil.replaceFirst(
093                                                    oldName,
094                                                    "portlet-setup-supported-clients-mobile-devices-",
095                                                    "portletSetupSupportedClientsMobileDevices_");
096                                    }
097                                    else if (oldName.startsWith("portlet-setup-title-")) {
098                                            newName = StringUtil.replaceFirst(
099                                                    oldName, "portlet-setup-title-", "portletSetupTitle_");
100                                    }
101                            }
102    
103                            if (Validator.isNotNull(newName)) {
104                                    String[] values = entry.getValue();
105    
106                                    portletPreferences.reset(oldName);
107                                    portletPreferences.setValues(newName, values);
108                            }
109                    }
110    
111                    return PortletPreferencesFactoryUtil.toXML(portletPreferences);
112            }
113    
114            private final Map<String, String> _camelCasePreferenceNames =
115                    new HashMap<>();
116    
117    }