1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portal.upgrade.v4_3_0.util;
16  
17  import com.liferay.portal.kernel.upgrade.util.BaseUpgradeColumnImpl;
18  import com.liferay.portal.kernel.upgrade.util.UpgradeColumn;
19  import com.liferay.portal.kernel.upgrade.util.ValueMapper;
20  import com.liferay.portal.kernel.util.GetterUtil;
21  import com.liferay.portal.kernel.util.Validator;
22  import com.liferay.portlet.PortletPreferencesImpl;
23  import com.liferay.portlet.PortletPreferencesSerializer;
24  
25  import javax.portlet.PortletPreferences;
26  
27  /**
28   * <a href="PrefsXMLUpgradeColumnImpl.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Brian Wing Shun Chan
31   */
32  public class PrefsXMLUpgradeColumnImpl extends BaseUpgradeColumnImpl {
33  
34      public PrefsXMLUpgradeColumnImpl(
35          UpgradeColumn upgradePortletIdColumn, ValueMapper groupIdMapper,
36          ValueMapper pollsQuestionIdMapper, ValueMapper wikiNodeIdMapper) {
37  
38          super("preferences");
39  
40          _upgradePortletIdColumn = upgradePortletIdColumn;
41          _groupIdMapper = groupIdMapper;
42          _pollsQuestionIdMapper = pollsQuestionIdMapper;
43          _wikiNodeIdMapper = wikiNodeIdMapper;
44      }
45  
46      public Object getNewValue(Object oldValue) throws Exception {
47          String xml = (String)oldValue;
48  
49          String portletId = (String)_upgradePortletIdColumn.getOldValue();
50  
51          PortletPreferences prefs =
52              PortletPreferencesSerializer.fromDefaultXML(xml);
53  
54          processPrefs(portletId, prefs);
55  
56          return PortletPreferencesSerializer.toXML(
57              (PortletPreferencesImpl)prefs);
58      }
59  
60      protected void processPrefs(String portletId, PortletPreferences prefs)
61          throws Exception {
62  
63          // Portlet Setup
64  
65          String portletCSS = prefs.getValue("portlet-setup-css", null);
66  
67          if (Validator.isNotNull(portletCSS)) {
68              prefs.reset("portlet-setup-css");
69          }
70  
71          // Journal Articles and Journal Content
72  
73          if (portletId.startsWith("62_INSTANCE_") ||
74              portletId.startsWith("56_INSTANCE_")) {
75  
76              String groupId = prefs.getValue("group-id", null);
77  
78              if (Validator.isNotNull(groupId)) {
79                  groupId = String.valueOf(_groupIdMapper.getNewValue(
80                      new Long(GetterUtil.getLong(groupId))));
81  
82                  prefs.setValue("group-id", groupId);
83              }
84          }
85  
86          // Polls Display
87  
88          else if (portletId.startsWith("59_INSTANCE_")) {
89              String questionId = prefs.getValue("question-id", null);
90  
91              if (Validator.isNotNull(questionId)) {
92                  questionId = String.valueOf(_pollsQuestionIdMapper.getNewValue(
93                      new Long(GetterUtil.getLong(questionId))));
94  
95                  prefs.setValue("question-id", questionId);
96              }
97          }
98  
99          // Wiki Display
100 
101         else if (portletId.startsWith("54_INSTANCE_")) {
102             String nodeId = prefs.getValue("node-id", null);
103 
104             if (Validator.isNotNull(nodeId)) {
105                 nodeId = String.valueOf(_wikiNodeIdMapper.getNewValue(
106                     new Long(GetterUtil.getLong(nodeId))));
107 
108                 prefs.setValue("node-id", nodeId);
109             }
110         }
111     }
112 
113     private UpgradeColumn _upgradePortletIdColumn;
114     private ValueMapper _groupIdMapper;
115     private ValueMapper _pollsQuestionIdMapper;
116     private ValueMapper _wikiNodeIdMapper;
117 
118 }