1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portal.upgrade.v5_2_6;
16  
17  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
18  import com.liferay.portal.kernel.upgrade.UpgradeProcess;
19  import com.liferay.portal.kernel.util.StringPool;
20  import com.liferay.portal.kernel.util.StringUtil;
21  import com.liferay.portal.model.PortletConstants;
22  import com.liferay.portal.util.PortletKeys;
23  
24  import java.sql.Connection;
25  import java.sql.PreparedStatement;
26  import java.sql.ResultSet;
27  
28  import java.util.regex.Matcher;
29  import java.util.regex.Pattern;
30  
31  /**
32   * <a href="UpgradeNestedPortlets.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Wesley Gong
35   * @author Bijan Vakili
36   * @author Douglas Wong
37   * @author Brian Wing Shun Chan
38   */
39  public class UpgradeNestedPortlets extends UpgradeProcess {
40  
41      protected void doUpgrade() throws Exception {
42          Connection con = null;
43          PreparedStatement ps = null;
44          ResultSet rs = null;
45  
46          try {
47              con = DataAccess.getConnection();
48  
49              ps = con.prepareStatement(_GET_LAYOUT);
50  
51              rs = ps.executeQuery();
52  
53              while (rs.next()) {
54                  long plid = rs.getLong("plid");
55                  String typeSettings = rs.getString("typeSettings");
56  
57                  String newTypeSettings = typeSettings;
58  
59                  Matcher matcher = _pattern.matcher(typeSettings);
60  
61                  while (matcher.find()) {
62                      String nestedColumnIds = matcher.group();
63  
64                      int underlineCount = StringUtil.count(
65                          nestedColumnIds, StringPool.UNDERLINE);
66  
67                      if (underlineCount == _UNDERLINE_COUNT) {
68                          String newNestedColumnIds = nestedColumnIds.replaceAll(
69                              _pattern.pattern(), "_$1_$2");
70  
71                          newTypeSettings = newTypeSettings.replaceAll(
72                              nestedColumnIds, newNestedColumnIds);
73                      }
74                  }
75  
76                  if (!newTypeSettings.equals(typeSettings)) {
77                      updateTypeSettings(plid, newTypeSettings);
78                  }
79              }
80          }
81          finally {
82              DataAccess.cleanUp(con, ps, rs);
83          }
84      }
85  
86      protected void updateTypeSettings(long plid, String typeSettings)
87          throws Exception {
88  
89          Connection con = null;
90          PreparedStatement ps = null;
91  
92          try {
93              con = DataAccess.getConnection();
94  
95              ps = con.prepareStatement(
96                  "update Layout set typeSettings = ? where plid = " + plid);
97  
98              ps.setString(1, typeSettings);
99  
100             ps.executeUpdate();
101         }
102         finally {
103             DataAccess.cleanUp(con, ps);
104         }
105     }
106 
107     private static final String _GET_LAYOUT =
108         "select plid, typeSettings from Layout where typeSettings like " +
109             "'%nested-column-ids=" + PortletKeys.NESTED_PORTLETS +
110                 PortletConstants.INSTANCE_SEPARATOR + "%'";
111 
112     private static final int _UNDERLINE_COUNT = StringUtil.count(
113         PortletConstants.INSTANCE_SEPARATOR, StringPool.UNDERLINE) + 1;
114 
115     private static Pattern _pattern = Pattern.compile(
116         "(" + PortletKeys.NESTED_PORTLETS +
117             PortletConstants.INSTANCE_SEPARATOR + "[^_,\\s=]+_)([^_,\\s=]+)");
118 
119 }