001
014
015 package com.liferay.portal.upgrade.v6_1_1;
016
017 import com.liferay.portal.kernel.upgrade.UpgradeProcess;
018 import com.liferay.portal.kernel.util.LoggingTimer;
019 import com.liferay.portal.kernel.util.StringBundler;
020
021 import java.sql.PreparedStatement;
022 import java.sql.ResultSet;
023
024
027 public class UpgradeLayout extends UpgradeProcess {
028
029 @Override
030 protected void doUpgrade() throws Exception {
031 updateSourcePrototypeLayoutUuid();
032 }
033
034 protected long getLayoutPrototypeGroupId(String layoutPrototypeUuid)
035 throws Exception {
036
037 try (PreparedStatement ps = connection.prepareStatement(
038 "select groupId from Group_ where classPK = (select " +
039 "layoutPrototypeId from LayoutPrototype where uuid_ = " +
040 "?)")) {
041
042 ps.setString(1, layoutPrototypeUuid);
043
044 try (ResultSet rs = ps.executeQuery()) {
045 while (rs.next()) {
046 long groupId = rs.getLong("groupId");
047
048 return groupId;
049 }
050 }
051 }
052
053 return 0;
054 }
055
056 protected boolean isGroupPrivateLayout(
057 long groupId, String sourcePrototypeLayoutUuid)
058 throws Exception {
059
060 try (PreparedStatement ps = connection.prepareStatement(
061 "select count(*) from Layout where uuid_ = ? and groupId = ? " +
062 "and privateLayout = ?")) {
063
064 ps.setString(1, sourcePrototypeLayoutUuid);
065 ps.setLong(2, groupId);
066 ps.setBoolean(3, true);
067
068 try (ResultSet rs = ps.executeQuery()) {
069 while (rs.next()) {
070 int count = rs.getInt(1);
071
072 if (count > 0) {
073 return true;
074 }
075 else {
076 return false;
077 }
078 }
079 }
080 }
081
082 return false;
083 }
084
085 protected void updateSourcePrototypeLayoutUuid() throws Exception {
086 try (LoggingTimer loggingTimer = new LoggingTimer()) {
087 StringBundler sb = new StringBundler(4);
088
089 sb.append("select plid, layoutPrototypeUuid, ");
090 sb.append("sourcePrototypeLayoutUuid from Layout where ");
091 sb.append("layoutPrototypeUuid != '' and ");
092 sb.append("sourcePrototypeLayoutUuid != ''");
093
094 try (PreparedStatement ps = connection.prepareStatement(
095 sb.toString());
096 ResultSet rs = ps.executeQuery()) {
097
098
099
100
101
102
103 while (rs.next()) {
104 long plid = rs.getLong("plid");
105 String layoutPrototypeUuid = rs.getString(
106 "layoutPrototypeUuid");
107 String sourcePrototypeLayoutUuid = rs.getString(
108 "sourcePrototypeLayoutUuid");
109
110 long groupId = getLayoutPrototypeGroupId(
111 layoutPrototypeUuid);
112
113 if (groupId == 0) {
114 continue;
115 }
116
117 if (isGroupPrivateLayout(
118 groupId, sourcePrototypeLayoutUuid)) {
119
120 runSQL(
121 "update Layout set sourcePrototypeLayoutUuid = " +
122 "null where plid = " + plid);
123 }
124 }
125 }
126 }
127 }
128
129 }