001    /**
002     * Copyright (c) 2000-2010 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.upgrade.v4_3_0.util;
016    
017    import com.liferay.portal.kernel.upgrade.util.BaseUpgradeColumnImpl;
018    import com.liferay.portal.kernel.upgrade.util.UpgradeColumn;
019    import com.liferay.portal.kernel.upgrade.util.ValueMapper;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portlet.PortletPreferencesImpl;
023    import com.liferay.portlet.PortletPreferencesSerializer;
024    
025    import javax.portlet.PortletPreferences;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class PrefsXMLUpgradeColumnImpl extends BaseUpgradeColumnImpl {
031    
032            public PrefsXMLUpgradeColumnImpl(
033                    UpgradeColumn upgradePortletIdColumn, ValueMapper groupIdMapper,
034                    ValueMapper pollsQuestionIdMapper, ValueMapper wikiNodeIdMapper) {
035    
036                    super("preferences");
037    
038                    _upgradePortletIdColumn = upgradePortletIdColumn;
039                    _groupIdMapper = groupIdMapper;
040                    _pollsQuestionIdMapper = pollsQuestionIdMapper;
041                    _wikiNodeIdMapper = wikiNodeIdMapper;
042            }
043    
044            public Object getNewValue(Object oldValue) throws Exception {
045                    String xml = (String)oldValue;
046    
047                    String portletId = (String)_upgradePortletIdColumn.getOldValue();
048    
049                    PortletPreferences preferences =
050                            PortletPreferencesSerializer.fromDefaultXML(xml);
051    
052                    processPreferences(portletId, preferences);
053    
054                    return PortletPreferencesSerializer.toXML(
055                            (PortletPreferencesImpl)preferences);
056            }
057    
058            protected void processPreferences(
059                            String portletId, PortletPreferences preferences)
060                    throws Exception {
061    
062                    // Portlet Setup
063    
064                    String portletCSS = preferences.getValue("portlet-setup-css", null);
065    
066                    if (Validator.isNotNull(portletCSS)) {
067                            preferences.reset("portlet-setup-css");
068                    }
069    
070                    // Journal Articles and Journal Content
071    
072                    if (portletId.startsWith("62_INSTANCE_") ||
073                            portletId.startsWith("56_INSTANCE_")) {
074    
075                            String groupId = preferences.getValue("group-id", null);
076    
077                            if (Validator.isNotNull(groupId)) {
078                                    groupId = String.valueOf(_groupIdMapper.getNewValue(
079                                            new Long(GetterUtil.getLong(groupId))));
080    
081                                    preferences.setValue("group-id", groupId);
082                            }
083                    }
084    
085                    // Polls Display
086    
087                    else if (portletId.startsWith("59_INSTANCE_")) {
088                            String questionId = preferences.getValue("question-id", null);
089    
090                            if (Validator.isNotNull(questionId)) {
091                                    questionId = String.valueOf(_pollsQuestionIdMapper.getNewValue(
092                                            new Long(GetterUtil.getLong(questionId))));
093    
094                                    preferences.setValue("question-id", questionId);
095                            }
096                    }
097    
098                    // Wiki Display
099    
100                    else if (portletId.startsWith("54_INSTANCE_")) {
101                            String nodeId = preferences.getValue("node-id", null);
102    
103                            if (Validator.isNotNull(nodeId)) {
104                                    nodeId = String.valueOf(_wikiNodeIdMapper.getNewValue(
105                                            new Long(GetterUtil.getLong(nodeId))));
106    
107                                    preferences.setValue("node-id", nodeId);
108                            }
109                    }
110            }
111    
112            private UpgradeColumn _upgradePortletIdColumn;
113            private ValueMapper _groupIdMapper;
114            private ValueMapper _pollsQuestionIdMapper;
115            private ValueMapper _wikiNodeIdMapper;
116    
117    }