1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.upgrade.v4_3_0.util;
21  
22  import com.liferay.portal.kernel.util.GetterUtil;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.upgrade.util.BaseUpgradeColumnImpl;
25  import com.liferay.portal.upgrade.util.UpgradeColumn;
26  import com.liferay.portal.upgrade.util.ValueMapper;
27  import com.liferay.portlet.PortletPreferencesImpl;
28  import com.liferay.portlet.PortletPreferencesSerializer;
29  
30  import javax.portlet.PortletPreferences;
31  
32  /**
33   * <a href="PrefsXMLUpgradeColumnImpl.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   *
37   */
38  public class PrefsXMLUpgradeColumnImpl extends BaseUpgradeColumnImpl {
39  
40      public PrefsXMLUpgradeColumnImpl(
41          UpgradeColumn upgradePortletIdColumn, ValueMapper groupIdMapper,
42          ValueMapper pollsQuestionIdMapper, ValueMapper wikiNodeIdMapper) {
43  
44          super("preferences");
45  
46          _upgradePortletIdColumn = upgradePortletIdColumn;
47          _groupIdMapper = groupIdMapper;
48          _pollsQuestionIdMapper = pollsQuestionIdMapper;
49          _wikiNodeIdMapper = wikiNodeIdMapper;
50      }
51  
52      public Object getNewValue(Object oldValue) throws Exception {
53          String xml = (String)oldValue;
54  
55          String portletId = (String)_upgradePortletIdColumn.getOldValue();
56  
57          PortletPreferences prefs =
58              PortletPreferencesSerializer.fromDefaultXML(xml);
59  
60          processPrefs(portletId, prefs);
61  
62          return PortletPreferencesSerializer.toXML(
63              (PortletPreferencesImpl)prefs);
64      }
65  
66      protected void processPrefs(String portletId, PortletPreferences prefs)
67          throws Exception {
68  
69          // Portlet Setup
70  
71          String portletCSS = prefs.getValue("portlet-setup-css", null);
72  
73          if (Validator.isNotNull(portletCSS)) {
74              prefs.reset("portlet-setup-css");
75          }
76  
77          // Journal Articles and Journal Content
78  
79          if (portletId.startsWith("62_INSTANCE_") ||
80              portletId.startsWith("56_INSTANCE_")) {
81  
82              String groupId = prefs.getValue("group-id", null);
83  
84              if (Validator.isNotNull(groupId)) {
85                  groupId = String.valueOf(_groupIdMapper.getNewValue(
86                      new Long(GetterUtil.getLong(groupId))));
87  
88                  prefs.setValue("group-id", groupId);
89              }
90          }
91  
92          // Polls Display
93  
94          else if (portletId.startsWith("59_INSTANCE_")) {
95              String questionId = prefs.getValue("question-id", null);
96  
97              if (Validator.isNotNull(questionId)) {
98                  questionId = String.valueOf(_pollsQuestionIdMapper.getNewValue(
99                      new Long(GetterUtil.getLong(questionId))));
100 
101                 prefs.setValue("question-id", questionId);
102             }
103         }
104 
105         // Wiki Display
106 
107         else if (portletId.startsWith("54_INSTANCE_")) {
108             String nodeId = prefs.getValue("node-id", null);
109 
110             if (Validator.isNotNull(nodeId)) {
111                 nodeId = String.valueOf(_wikiNodeIdMapper.getNewValue(
112                     new Long(GetterUtil.getLong(nodeId))));
113 
114                 prefs.setValue("node-id", nodeId);
115             }
116         }
117     }
118 
119     private UpgradeColumn _upgradePortletIdColumn;
120     private ValueMapper _groupIdMapper;
121     private ValueMapper _pollsQuestionIdMapper;
122     private ValueMapper _wikiNodeIdMapper;
123 
124 }