1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class PrefsXMLUpgradeColumnImpl extends BaseUpgradeColumnImpl {
41  
42      public PrefsXMLUpgradeColumnImpl(
43          UpgradeColumn upgradePortletIdColumn, ValueMapper groupIdMapper,
44          ValueMapper pollsQuestionIdMapper, ValueMapper wikiNodeIdMapper) {
45  
46          super("preferences");
47  
48          _upgradePortletIdColumn = upgradePortletIdColumn;
49          _groupIdMapper = groupIdMapper;
50          _pollsQuestionIdMapper = pollsQuestionIdMapper;
51          _wikiNodeIdMapper = wikiNodeIdMapper;
52      }
53  
54      public Object getNewValue(Object oldValue) throws Exception {
55          String xml = (String)oldValue;
56  
57          String portletId = (String)_upgradePortletIdColumn.getOldValue();
58  
59          PortletPreferences prefs =
60              PortletPreferencesSerializer.fromDefaultXML(xml);
61  
62          processPrefs(portletId, prefs);
63  
64          return PortletPreferencesSerializer.toXML(
65              (PortletPreferencesImpl)prefs);
66      }
67  
68      protected void processPrefs(String portletId, PortletPreferences prefs)
69          throws Exception {
70  
71          // Portlet Setup
72  
73          String portletCSS = prefs.getValue("portlet-setup-css", null);
74  
75          if (Validator.isNotNull(portletCSS)) {
76              prefs.reset("portlet-setup-css");
77          }
78  
79          // Journal Articles and Journal Content
80  
81          if (portletId.startsWith("62_INSTANCE_") ||
82              portletId.startsWith("56_INSTANCE_")) {
83  
84              String groupId = prefs.getValue("group-id", null);
85  
86              if (Validator.isNotNull(groupId)) {
87                  groupId = String.valueOf(_groupIdMapper.getNewValue(
88                      new Long(GetterUtil.getLong(groupId))));
89  
90                  prefs.setValue("group-id", groupId);
91              }
92          }
93  
94          // Polls Display
95  
96          else if (portletId.startsWith("59_INSTANCE_")) {
97              String questionId = prefs.getValue("question-id", null);
98  
99              if (Validator.isNotNull(questionId)) {
100                 questionId = String.valueOf(_pollsQuestionIdMapper.getNewValue(
101                     new Long(GetterUtil.getLong(questionId))));
102 
103                 prefs.setValue("question-id", questionId);
104             }
105         }
106 
107         // Wiki Display
108 
109         else if (portletId.startsWith("54_INSTANCE_")) {
110             String nodeId = prefs.getValue("node-id", null);
111 
112             if (Validator.isNotNull(nodeId)) {
113                 nodeId = String.valueOf(_wikiNodeIdMapper.getNewValue(
114                     new Long(GetterUtil.getLong(nodeId))));
115 
116                 prefs.setValue("node-id", nodeId);
117             }
118         }
119     }
120 
121     private UpgradeColumn _upgradePortletIdColumn;
122     private ValueMapper _groupIdMapper;
123     private ValueMapper _pollsQuestionIdMapper;
124     private ValueMapper _wikiNodeIdMapper;
125 
126 }