1   /**
2    * Copyright (c) 2000-2009 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 preferences =
61              PortletPreferencesSerializer.fromDefaultXML(xml);
62  
63          processPreferences(portletId, preferences);
64  
65          return PortletPreferencesSerializer.toXML(
66              (PortletPreferencesImpl)preferences);
67      }
68  
69      protected void processPreferences(
70              String portletId, PortletPreferences preferences)
71          throws Exception {
72  
73          // Portlet Setup
74  
75          String portletCSS = preferences.getValue("portlet-setup-css", null);
76  
77          if (Validator.isNotNull(portletCSS)) {
78              preferences.reset("portlet-setup-css");
79          }
80  
81          // Journal Articles and Journal Content
82  
83          if (portletId.startsWith("62_INSTANCE_") ||
84              portletId.startsWith("56_INSTANCE_")) {
85  
86              String groupId = preferences.getValue("group-id", null);
87  
88              if (Validator.isNotNull(groupId)) {
89                  groupId = String.valueOf(_groupIdMapper.getNewValue(
90                      new Long(GetterUtil.getLong(groupId))));
91  
92                  preferences.setValue("group-id", groupId);
93              }
94          }
95  
96          // Polls Display
97  
98          else if (portletId.startsWith("59_INSTANCE_")) {
99              String questionId = preferences.getValue("question-id", null);
100 
101             if (Validator.isNotNull(questionId)) {
102                 questionId = String.valueOf(_pollsQuestionIdMapper.getNewValue(
103                     new Long(GetterUtil.getLong(questionId))));
104 
105                 preferences.setValue("question-id", questionId);
106             }
107         }
108 
109         // Wiki Display
110 
111         else if (portletId.startsWith("54_INSTANCE_")) {
112             String nodeId = preferences.getValue("node-id", null);
113 
114             if (Validator.isNotNull(nodeId)) {
115                 nodeId = String.valueOf(_wikiNodeIdMapper.getNewValue(
116                     new Long(GetterUtil.getLong(nodeId))));
117 
118                 preferences.setValue("node-id", nodeId);
119             }
120         }
121     }
122 
123     private UpgradeColumn _upgradePortletIdColumn;
124     private ValueMapper _groupIdMapper;
125     private ValueMapper _pollsQuestionIdMapper;
126     private ValueMapper _wikiNodeIdMapper;
127 
128 }