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