1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.upgrade.v4_3_0.util;
24  
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.upgrade.util.BaseUpgradeColumnImpl;
28  import com.liferay.portal.upgrade.util.UpgradeColumn;
29  import com.liferay.portal.upgrade.util.ValueMapper;
30  import com.liferay.portlet.PortletPreferencesImpl;
31  import com.liferay.portlet.PortletPreferencesSerializer;
32  
33  import javax.portlet.PortletPreferences;
34  
35  /**
36   * <a href="PrefsXMLUpgradeColumnImpl.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   *
40   */
41  public class PrefsXMLUpgradeColumnImpl extends BaseUpgradeColumnImpl {
42  
43      public PrefsXMLUpgradeColumnImpl(
44          UpgradeColumn upgradePortletIdColumn, ValueMapper groupIdMapper,
45          ValueMapper pollsQuestionIdMapper, ValueMapper wikiNodeIdMapper) {
46  
47          super("preferences");
48  
49          _upgradePortletIdColumn = upgradePortletIdColumn;
50          _groupIdMapper = groupIdMapper;
51          _pollsQuestionIdMapper = pollsQuestionIdMapper;
52          _wikiNodeIdMapper = wikiNodeIdMapper;
53      }
54  
55      public Object getNewValue(Object oldValue) throws Exception {
56          String xml = (String)oldValue;
57  
58          String portletId = (String)_upgradePortletIdColumn.getOldValue();
59  
60          PortletPreferences prefs =
61              PortletPreferencesSerializer.fromDefaultXML(xml);
62  
63          processPrefs(portletId, prefs);
64  
65          return PortletPreferencesSerializer.toXML(
66              (PortletPreferencesImpl)prefs);
67      }
68  
69      protected void processPrefs(String portletId, PortletPreferences prefs)
70          throws Exception {
71  
72          // Portlet Setup
73  
74          String portletCSS = prefs.getValue("portlet-setup-css", null);
75  
76          if (Validator.isNotNull(portletCSS)) {
77              prefs.reset("portlet-setup-css");
78          }
79  
80          // Journal Articles and Journal Content
81  
82          if (portletId.startsWith("62_INSTANCE_") ||
83              portletId.startsWith("56_INSTANCE_")) {
84  
85              String groupId = prefs.getValue("group-id", null);
86  
87              if (Validator.isNotNull(groupId)) {
88                  groupId = String.valueOf(_groupIdMapper.getNewValue(
89                      new Long(GetterUtil.getLong(groupId))));
90  
91                  prefs.setValue("group-id", groupId);
92              }
93          }
94  
95          // Polls Display
96  
97          else if (portletId.startsWith("59_INSTANCE_")) {
98              String questionId = prefs.getValue("question-id", null);
99  
100             if (Validator.isNotNull(questionId)) {
101                 questionId = String.valueOf(_pollsQuestionIdMapper.getNewValue(
102                     new Long(GetterUtil.getLong(questionId))));
103 
104                 prefs.setValue("question-id", questionId);
105             }
106         }
107 
108         // Wiki Display
109 
110         else if (portletId.startsWith("54_INSTANCE_")) {
111             String nodeId = prefs.getValue("node-id", null);
112 
113             if (Validator.isNotNull(nodeId)) {
114                 nodeId = String.valueOf(_wikiNodeIdMapper.getNewValue(
115                     new Long(GetterUtil.getLong(nodeId))));
116 
117                 prefs.setValue("node-id", nodeId);
118             }
119         }
120     }
121 
122     private UpgradeColumn _upgradePortletIdColumn;
123     private ValueMapper _groupIdMapper;
124     private ValueMapper _pollsQuestionIdMapper;
125     private ValueMapper _wikiNodeIdMapper;
126 
127 }