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