001
014
015 package com.liferay.portal.upgrade.v6_1_0;
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.StringBundler;
020 import com.liferay.portal.model.Group;
021 import com.liferay.portal.model.Organization;
022 import com.liferay.portal.upgrade.v6_1_0.util.GroupTable;
023
024 import java.sql.Connection;
025 import java.sql.PreparedStatement;
026 import java.sql.ResultSet;
027 import java.sql.SQLException;
028
029
033 public class UpgradeGroup extends UpgradeProcess {
034
035 @Override
036 protected void doUpgrade() throws Exception {
037 try {
038 runSQL("alter_column_type Group_ name VARCHAR(150) null");
039 }
040 catch (SQLException sqle) {
041 upgradeTable(
042 GroupTable.TABLE_NAME, GroupTable.TABLE_COLUMNS,
043 GroupTable.TABLE_SQL_CREATE, GroupTable.TABLE_SQL_ADD_INDEXES);
044 }
045
046 updateName();
047 updateSite();
048 }
049
050 protected long getClassNameId(String className) throws Exception {
051 Connection con = null;
052 PreparedStatement ps = null;
053 ResultSet rs = null;
054
055 try {
056 con = DataAccess.getUpgradeOptimizedConnection();
057
058 ps = con.prepareStatement(
059 "select classNameId from ClassName_ where value = ?");
060
061 ps.setString(1, className);
062
063 rs = ps.executeQuery();
064
065 if (rs.next()) {
066 return rs.getLong("classNameId");
067 }
068
069 return 0;
070 }
071 finally {
072 DataAccess.cleanUp(con, ps, rs);
073 }
074 }
075
076 protected void updateName() throws Exception {
077 long organizationClassNameId = getClassNameId(
078 Organization.class.getName());
079
080 Connection con = null;
081 PreparedStatement ps = null;
082 ResultSet rs = null;
083
084 try {
085 con = DataAccess.getUpgradeOptimizedConnection();
086
087 StringBundler sb = new StringBundler(4);
088
089 sb.append("select Group_.groupId, Group_.classPK, ");
090 sb.append("Organization_.name from Group_ inner join ");
091 sb.append("Organization_ on Organization_.organizationId = ");
092 sb.append("Group_.classPK where classNameId = ?");
093
094 String sql = sb.toString();
095
096 ps = con.prepareStatement(sql);
097
098 ps.setLong(1, organizationClassNameId);
099
100 rs = ps.executeQuery();
101
102 while (rs.next()) {
103 long groupId = rs.getLong("groupId");
104 long classPK = rs.getLong("classPK");
105 String name = rs.getString("name");
106
107 updateName(groupId, classPK, name);
108 }
109 }
110 finally {
111 DataAccess.cleanUp(con, ps, rs);
112 }
113 }
114
115 protected void updateName(long groupId, long classPK, String name)
116 throws Exception {
117
118 Connection con = null;
119 PreparedStatement ps = null;
120
121 try {
122 con = DataAccess.getUpgradeOptimizedConnection();
123
124 ps = con.prepareStatement(
125 "update Group_ set name = ? where groupId = ?");
126
127 StringBundler sb = new StringBundler(3);
128
129 sb.append(classPK);
130 sb.append(_ORGANIZATION_NAME_DELIMETER);
131 sb.append(name);
132
133 ps.setString(1, sb.toString());
134 ps.setLong(2, groupId);
135
136 ps.executeUpdate();
137 }
138 finally {
139 DataAccess.cleanUp(con, ps);
140 }
141 }
142
143 protected void updateSite() throws Exception {
144 long groupClassNameId = getClassNameId(Group.class.getName());
145
146 runSQL(
147 "update Group_ set site = TRUE where classNameId = " +
148 groupClassNameId);
149
150 long organizationClassNameId = getClassNameId(
151 Organization.class.getName());
152
153 Connection con = null;
154 PreparedStatement ps = null;
155 ResultSet rs = null;
156
157 try {
158 con = DataAccess.getUpgradeOptimizedConnection();
159
160 String sql =
161 "select distinct Group_.groupId from Group_ inner join " +
162 "Layout on Layout.groupId = Group_.groupId where " +
163 "classNameId = ?";
164
165 ps = con.prepareStatement(sql);
166
167 ps.setLong(1, organizationClassNameId);
168
169 rs = ps.executeQuery();
170
171 while (rs.next()) {
172 long groupId = rs.getLong("groupId");
173
174 runSQL(
175 "update Group_ set site = TRUE where groupId = " + groupId);
176 }
177 }
178 finally {
179 DataAccess.cleanUp(con, ps, rs);
180 }
181 }
182
183 private static final String _ORGANIZATION_NAME_DELIMETER =
184 " LFR_ORGANIZATION ";
185
186 }