1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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 preferences =
52              PortletPreferencesSerializer.fromDefaultXML(xml);
53  
54          processPreferences(portletId, preferences);
55  
56          return PortletPreferencesSerializer.toXML(
57              (PortletPreferencesImpl)preferences);
58      }
59  
60      protected void processPreferences(
61              String portletId, PortletPreferences preferences)
62          throws Exception {
63  
64          // Portlet Setup
65  
66          String portletCSS = preferences.getValue("portlet-setup-css", null);
67  
68          if (Validator.isNotNull(portletCSS)) {
69              preferences.reset("portlet-setup-css");
70          }
71  
72          // Journal Articles and Journal Content
73  
74          if (portletId.startsWith("62_INSTANCE_") ||
75              portletId.startsWith("56_INSTANCE_")) {
76  
77              String groupId = preferences.getValue("group-id", null);
78  
79              if (Validator.isNotNull(groupId)) {
80                  groupId = String.valueOf(_groupIdMapper.getNewValue(
81                      new Long(GetterUtil.getLong(groupId))));
82  
83                  preferences.setValue("group-id", groupId);
84              }
85          }
86  
87          // Polls Display
88  
89          else if (portletId.startsWith("59_INSTANCE_")) {
90              String questionId = preferences.getValue("question-id", null);
91  
92              if (Validator.isNotNull(questionId)) {
93                  questionId = String.valueOf(_pollsQuestionIdMapper.getNewValue(
94                      new Long(GetterUtil.getLong(questionId))));
95  
96                  preferences.setValue("question-id", questionId);
97              }
98          }
99  
100         // Wiki Display
101 
102         else if (portletId.startsWith("54_INSTANCE_")) {
103             String nodeId = preferences.getValue("node-id", null);
104 
105             if (Validator.isNotNull(nodeId)) {
106                 nodeId = String.valueOf(_wikiNodeIdMapper.getNewValue(
107                     new Long(GetterUtil.getLong(nodeId))));
108 
109                 preferences.setValue("node-id", nodeId);
110             }
111         }
112     }
113 
114     private UpgradeColumn _upgradePortletIdColumn;
115     private ValueMapper _groupIdMapper;
116     private ValueMapper _pollsQuestionIdMapper;
117     private ValueMapper _wikiNodeIdMapper;
118 
119 }