001
014
015 package com.liferay.portal.upgrade.v6_0_2;
016
017 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018 import com.liferay.portal.kernel.upgrade.UpgradeProcess;
019 import com.liferay.portal.kernel.util.StringPool;
020 import com.liferay.portal.kernel.util.StringUtil;
021 import com.liferay.portal.util.PortletKeys;
022
023 import java.sql.Connection;
024 import java.sql.PreparedStatement;
025 import java.sql.ResultSet;
026
027 import java.util.regex.Matcher;
028 import java.util.regex.Pattern;
029
030
036 public class UpgradeNestedPortlets extends UpgradeProcess {
037
038 @Override
039 protected void doUpgrade() throws Exception {
040 Connection con = null;
041 PreparedStatement ps = null;
042 ResultSet rs = null;
043
044 try {
045 con = DataAccess.getUpgradeOptimizedConnection();
046
047 ps = con.prepareStatement(_GET_LAYOUT);
048
049 rs = ps.executeQuery();
050
051 while (rs.next()) {
052 long plid = rs.getLong("plid");
053 String typeSettings = rs.getString("typeSettings");
054
055 String newTypeSettings = typeSettings;
056
057 Matcher matcher = _pattern.matcher(typeSettings);
058
059 while (matcher.find()) {
060 String nestedColumnIds = matcher.group();
061
062 int underlineCount = StringUtil.count(
063 nestedColumnIds, StringPool.UNDERLINE);
064
065 if (underlineCount == _UNDERLINE_COUNT) {
066 String newNestedColumnIds =
067 "_" + matcher.group(1) + "_" + matcher.group(2);
068
069 newTypeSettings = StringUtil.replace(
070 newTypeSettings, nestedColumnIds,
071 newNestedColumnIds);
072 }
073 }
074
075 if (!newTypeSettings.equals(typeSettings)) {
076 updateLayout(plid, newTypeSettings);
077 }
078 }
079 }
080 finally {
081 DataAccess.cleanUp(con, ps, rs);
082 }
083 }
084
085 protected void updateLayout(long plid, String typeSettings)
086 throws Exception {
087
088 Connection con = null;
089 PreparedStatement ps = null;
090
091 try {
092 con = DataAccess.getUpgradeOptimizedConnection();
093
094 ps = con.prepareStatement(
095 "update Layout set typeSettings = ? where plid = " + plid);
096
097 ps.setString(1, typeSettings);
098
099 ps.executeUpdate();
100 }
101 finally {
102 DataAccess.cleanUp(con, ps);
103 }
104 }
105
106 private static final String _GET_LAYOUT =
107 "select plid, typeSettings from Layout where typeSettings like " +
108 "'%nested-column-ids=" + PortletKeys.NESTED_PORTLETS +
109 UpgradeNestedPortlets._INSTANCE_SEPARATOR + "%'";
110
111 private static final String _INSTANCE_SEPARATOR = "_INSTANCE_";
112
113 private static final int _UNDERLINE_COUNT = StringUtil.count(
114 _INSTANCE_SEPARATOR, StringPool.UNDERLINE) + 1;
115
116 private static final Pattern _pattern = Pattern.compile(
117 "(" + PortletKeys.NESTED_PORTLETS +
118 _INSTANCE_SEPARATOR + "[^_,\\s=]+_)([^_,\\s=]+)");
119
120 }