001
014
015 package com.liferay.portal.verify;
016
017 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.util.StringPool;
021
022 import java.sql.Connection;
023 import java.sql.PreparedStatement;
024 import java.sql.ResultSet;
025
026 import java.util.ArrayList;
027 import java.util.List;
028
029
032 public class VerifyGroupId extends VerifyProcess {
033
034 @Override
035 protected void doVerify() throws Exception {
036 List<String> pendingModels = new ArrayList<String>();
037
038 for (String[] model : _MODELS) {
039 pendingModels.add(model[0]);
040 }
041
042 while (!pendingModels.isEmpty()) {
043 int count = pendingModels.size();
044
045 for (String[] model : _MODELS) {
046 if (pendingModels.contains(model[2]) ||
047 !pendingModels.contains(model[0])) {
048
049 continue;
050 }
051
052 verifyModel(model[0], model[1], model[2], model[3]);
053
054 pendingModels.remove(model[0]);
055 }
056
057 if (pendingModels.size() == count) {
058 throw new VerifyException(
059 "Circular dependency detected " + pendingModels);
060 }
061 }
062 }
063
064 protected long getGroupId(
065 String modelName, String pkColumnName, long primKey)
066 throws Exception {
067
068 Connection con = null;
069 PreparedStatement ps = null;
070 ResultSet rs = null;
071
072 try {
073 con = DataAccess.getUpgradeOptimizedConnection();
074
075 ps = con.prepareStatement(
076 "select groupId from " + modelName + " where " + pkColumnName +
077 " = ?");
078
079 ps.setLong(1, primKey);
080
081 rs = ps.executeQuery();
082
083 if (rs.next()) {
084 return rs.getLong("groupId");
085 }
086
087 if (_log.isDebugEnabled()) {
088 _log.debug(
089 "Unable to find " + modelName + StringPool.SPACE + primKey);
090 }
091
092 return 0;
093 }
094 finally {
095 DataAccess.cleanUp(con, ps, rs);
096 }
097 }
098
099 protected void verifyModel(
100 String modelName, String pkColumnName, String relatedModelName,
101 String relatedPKColumnName)
102 throws Exception {
103
104 Connection con = null;
105 PreparedStatement ps = null;
106 ResultSet rs = null;
107
108 try {
109 con = DataAccess.getUpgradeOptimizedConnection();
110
111 ps = con.prepareStatement(
112 "select " + pkColumnName + StringPool.COMMA_AND_SPACE +
113 relatedPKColumnName + " from " + modelName + " where " +
114 "groupId is null");
115
116 rs = ps.executeQuery();
117
118 while (rs.next()) {
119 long primKey = rs.getLong(pkColumnName);
120 long relatedPrimKey = rs.getLong(relatedPKColumnName);
121
122 long groupId = getGroupId(
123 relatedModelName, relatedPKColumnName, relatedPrimKey);
124
125 if (groupId <= 0) {
126 continue;
127 }
128
129 runSQL(
130 "update " + modelName + " set groupId = " + groupId +
131 " where " + pkColumnName + " = " + primKey);
132 }
133 }
134 finally {
135 DataAccess.cleanUp(con, ps, rs);
136 }
137 }
138
139 private static final String[][] _MODELS = new String[][] {
140 new String[] {
141 "MBDiscussion", "discussionId", "MBThread", "threadId"
142 },
143 new String[] {
144 "MBThreadFlag", "threadFlagId", "MBThread", "threadId"
145 },
146 new String[] {
147 "PollsChoice", "choiceId", "PollsQuestion", "questionId"
148 },
149 new String[] {
150 "PollsVote", "voteId", "PollsQuestion", "questionId"
151 }
152 };
153
154 private static Log _log = LogFactoryUtil.getLog(VerifyGroupId.class);
155
156 }