001
014
015 package com.liferay.portal.upgrade.v6_0_0;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.model.GroupConstants;
020 import com.liferay.portal.kernel.upgrade.UpgradeProcess;
021 import com.liferay.portal.kernel.util.LoggingTimer;
022 import com.liferay.portal.kernel.util.PortalUtil;
023
024 import java.sql.PreparedStatement;
025 import java.sql.ResultSet;
026 import java.sql.Statement;
027
028
031 public class UpgradeGroup extends UpgradeProcess {
032
033 @Override
034 protected void doUpgrade() throws Exception {
035 updateGlobalFriendlyURLs();
036 updateParentGroupId();
037 }
038
039 protected long getGroupId(long companyId, String friendlyURL)
040 throws Exception {
041
042 try (PreparedStatement ps = connection.prepareStatement(
043 "select groupId from Group_ where companyId = ? and " +
044 "friendlyURL = ?")) {
045
046 ps.setLong(1, companyId);
047 ps.setString(2, friendlyURL);
048
049 try (ResultSet rs = ps.executeQuery()) {
050 if (rs.next()) {
051 return rs.getLong(1);
052 }
053 }
054 }
055
056 return 0;
057 }
058
059 protected Object[] getLayout(long plid) throws Exception {
060 try (PreparedStatement ps = connection.prepareStatement(_GET_LAYOUT)) {
061 ps.setLong(1, plid);
062
063 try (ResultSet rs = ps.executeQuery()) {
064 if (rs.next()) {
065 long groupId = rs.getLong("groupId");
066
067 return new Object[] {groupId};
068 }
069
070 return null;
071 }
072 }
073 }
074
075 protected String getNewFriendlyURL(long companyId, long groupId)
076 throws Exception {
077
078 String friendlyURL = null;
079
080 int i = 1;
081
082 while (groupId > 0) {
083 friendlyURL = GroupConstants.GLOBAL_FRIENDLY_URL + i;
084
085 groupId = getGroupId(companyId, friendlyURL);
086
087 i++;
088 }
089
090 return friendlyURL;
091 }
092
093 protected void updateGlobalFriendlyURL(long companyId) throws Exception {
094 long groupId = getGroupId(
095 companyId, GroupConstants.GLOBAL_FRIENDLY_URL);
096
097 if (groupId == 0) {
098 return;
099 }
100
101 String friendlyURL = getNewFriendlyURL(companyId, groupId);
102
103 if (_log.isInfoEnabled()) {
104 _log.info(
105 "Updating friendly URL " + GroupConstants.GLOBAL_FRIENDLY_URL +
106 " of global group " + groupId + " to " + friendlyURL);
107 }
108
109 try (PreparedStatement ps = connection.prepareStatement(
110 "update Group_ set friendlyURL = ? where groupId = ?")) {
111
112 ps.setString(1, friendlyURL);
113 ps.setLong(2, groupId);
114
115 ps.execute();
116 }
117 }
118
119 protected void updateGlobalFriendlyURLs() throws Exception {
120 try (Statement s = connection.createStatement()) {
121 String query = "select companyId from Company";
122
123 try (ResultSet rs = s.executeQuery(query)) {
124 while (rs.next()) {
125 updateGlobalFriendlyURL(rs.getLong(1));
126 }
127 }
128 }
129 }
130
131 protected void updateParentGroupId() throws Exception {
132 try (LoggingTimer loggingTimer = new LoggingTimer()) {
133 long classNameId = PortalUtil.getClassNameId(
134 "com.liferay.portal.model.Layout");
135
136 try (PreparedStatement ps = connection.prepareStatement(
137 "select groupId, classPK from Group_ where classNameId = " +
138 classNameId);
139 ResultSet rs = ps.executeQuery()) {
140
141 while (rs.next()) {
142 long groupId = rs.getLong("groupId");
143 long classPK = rs.getLong("classPK");
144
145 Object[] layout = getLayout(classPK);
146
147 if (layout != null) {
148 long layoutGroupId = (Long)layout[0];
149
150 runSQL(
151 "update Group_ set parentGroupId = " +
152 layoutGroupId + " where groupId = " + groupId);
153 }
154 }
155 }
156 }
157 }
158
159 private static final String _GET_LAYOUT =
160 "select * from Layout where plid = ?";
161
162 private static final Log _log = LogFactoryUtil.getLog(UpgradeGroup.class);
163
164 }