001
014
015 package com.liferay.portal.verify;
016
017 import com.liferay.portal.GroupFriendlyURLException;
018 import com.liferay.portal.NoSuchShardException;
019 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
020 import com.liferay.portal.kernel.dao.orm.DynamicQuery;
021 import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
022 import com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil;
023 import com.liferay.portal.kernel.dao.shard.ShardUtil;
024 import com.liferay.portal.kernel.log.Log;
025 import com.liferay.portal.kernel.log.LogFactoryUtil;
026 import com.liferay.portal.kernel.util.GetterUtil;
027 import com.liferay.portal.kernel.util.StringBundler;
028 import com.liferay.portal.kernel.util.StringPool;
029 import com.liferay.portal.kernel.util.UnicodeProperties;
030 import com.liferay.portal.model.Company;
031 import com.liferay.portal.model.Group;
032 import com.liferay.portal.model.GroupConstants;
033 import com.liferay.portal.model.LayoutSet;
034 import com.liferay.portal.model.Organization;
035 import com.liferay.portal.model.Shard;
036 import com.liferay.portal.model.User;
037 import com.liferay.portal.service.CompanyLocalServiceUtil;
038 import com.liferay.portal.service.GroupLocalServiceUtil;
039 import com.liferay.portal.service.ShardLocalServiceUtil;
040 import com.liferay.portal.service.UserLocalServiceUtil;
041 import com.liferay.portal.service.impl.GroupLocalServiceImpl;
042 import com.liferay.portal.util.PortalUtil;
043 import com.liferay.portal.util.PropsValues;
044 import com.liferay.portal.util.RobotsUtil;
045
046 import java.sql.Connection;
047 import java.sql.PreparedStatement;
048 import java.sql.ResultSet;
049
050 import java.util.List;
051
052
055 public class VerifyGroup extends VerifyProcess {
056
057 @Override
058 protected void doVerify() throws Exception {
059 verifyCompanyGroups();
060 verifyNullFriendlyURLGroups();
061 verifyOrganizationNames();
062 verifyRobots();
063 verifySites();
064 verifyStagedGroups();
065 }
066
067 protected String getRobots(LayoutSet layoutSet) {
068 if (layoutSet == null) {
069 return RobotsUtil.getDefaultRobots(null);
070 }
071
072 String virtualHostname = StringPool.BLANK;
073
074 try {
075 virtualHostname = layoutSet.getVirtualHostname();
076 }
077 catch (Exception e) {
078 }
079
080 return GetterUtil.get(
081 layoutSet.getSettingsProperty(
082 layoutSet.isPrivateLayout() + "-robots.txt"),
083 RobotsUtil.getDefaultRobots(virtualHostname));
084 }
085
086 protected void updateName(long groupId, String name) throws Exception {
087 Connection con = null;
088 PreparedStatement ps = null;
089
090 try {
091 con = DataAccess.getUpgradeOptimizedConnection();
092
093 ps = con.prepareStatement(
094 "update Group_ set name = ? where groupId= " + groupId);
095
096 ps.setString(1, name);
097
098 ps.executeUpdate();
099 }
100 finally {
101 DataAccess.cleanUp(con, ps);
102 }
103 }
104
105 protected void verifyCompanyGroups() throws Exception {
106 List<Company> companies = CompanyLocalServiceUtil.getCompanies();
107
108 String currentShardName = ShardUtil.getCurrentShardName();
109
110 for (Company company : companies) {
111 String shardName = null;
112
113 try {
114 shardName = company.getShardName();
115 }
116 catch (NoSuchShardException nsse) {
117 Shard shard = ShardLocalServiceUtil.addShard(
118 Company.class.getName(), company.getCompanyId(),
119 PropsValues.SHARD_DEFAULT_NAME);
120
121 shardName = shard.getName();
122 }
123
124 if (!ShardUtil.isEnabled() || shardName.equals(currentShardName)) {
125 GroupLocalServiceUtil.checkCompanyGroup(company.getCompanyId());
126 }
127 }
128 }
129
130 protected void verifyNullFriendlyURLGroups() throws Exception {
131 List<Group> groups = GroupLocalServiceUtil.getNullFriendlyURLGroups();
132
133 for (Group group : groups) {
134 String friendlyURL = StringPool.SLASH + group.getGroupId();
135
136 User user = null;
137
138 if (group.isCompany()) {
139 friendlyURL = GroupConstants.GLOBAL_FRIENDLY_URL;
140 }
141 else if (group.isUser()) {
142 user = UserLocalServiceUtil.getUserById(group.getClassPK());
143
144 friendlyURL = StringPool.SLASH + user.getScreenName();
145 }
146 else if (group.getClassPK() > 0) {
147 friendlyURL = StringPool.SLASH + group.getClassPK();
148 }
149
150 try {
151 GroupLocalServiceUtil.updateFriendlyURL(
152 group.getGroupId(), friendlyURL);
153 }
154 catch (GroupFriendlyURLException gfurle) {
155 if (user != null) {
156 long userId = user.getUserId();
157 String screenName = user.getScreenName();
158
159 if (_log.isInfoEnabled()) {
160 _log.info(
161 "Updating user screen name " + screenName + " to " +
162 userId + " because it is generating an " +
163 "invalid friendly URL");
164 }
165
166 UserLocalServiceUtil.updateScreenName(
167 userId, String.valueOf(userId));
168 }
169 else {
170 _log.error("Invalid Friendly URL " + friendlyURL);
171
172 throw gfurle;
173 }
174 }
175 }
176 }
177
178 protected void verifyOrganizationNames() throws Exception {
179 Connection con = null;
180 PreparedStatement ps = null;
181 ResultSet rs = null;
182
183 try {
184 con = DataAccess.getUpgradeOptimizedConnection();
185
186 StringBundler sb = new StringBundler(5);
187
188 sb.append("select groupId, name from Group_ where name like '%");
189 sb.append(GroupLocalServiceImpl.ORGANIZATION_NAME_SUFFIX);
190 sb.append("%' and name not like '%");
191 sb.append(GroupLocalServiceImpl.ORGANIZATION_NAME_SUFFIX);
192 sb.append("'");
193
194 ps = con.prepareStatement(sb.toString());
195
196 rs = ps.executeQuery();
197
198 while (rs.next()) {
199 long groupId = rs.getLong("groupId");
200 String name = rs.getString("name");
201
202 int pos = name.indexOf(
203 GroupLocalServiceImpl.ORGANIZATION_NAME_SUFFIX);
204
205 pos = name.indexOf(" ", pos + 1);
206
207 String newName =
208 name.substring(pos + 1) +
209 GroupLocalServiceImpl.ORGANIZATION_NAME_SUFFIX;
210
211 updateName(groupId, newName);
212 }
213 }
214 finally {
215 DataAccess.cleanUp(con, ps, rs);
216 }
217 }
218
219 protected void verifyRobots() throws Exception {
220 List<Group> groups = GroupLocalServiceUtil.getLiveGroups();
221
222 for (Group group : groups) {
223 LayoutSet privateLayoutSet = group.getPrivateLayoutSet();
224 LayoutSet publicLayoutSet = group.getPublicLayoutSet();
225
226 String privateLayoutSetRobots = getRobots(privateLayoutSet);
227 String publicLayoutSetRobots = getRobots(publicLayoutSet);
228
229 UnicodeProperties typeSettingsProperties =
230 group.getTypeSettingsProperties();
231
232 typeSettingsProperties.setProperty(
233 "true-robots.txt", privateLayoutSetRobots);
234 typeSettingsProperties.setProperty(
235 "false-robots.txt", publicLayoutSetRobots);
236
237 GroupLocalServiceUtil.updateGroup(
238 group.getGroupId(), typeSettingsProperties.toString());
239 }
240 }
241
242 protected void verifySites() throws Exception {
243 DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
244 Group.class);
245
246 dynamicQuery.add(
247 RestrictionsFactoryUtil.eq(
248 "classNameId", PortalUtil.getClassNameId(Organization.class)));
249 dynamicQuery.add(RestrictionsFactoryUtil.eq("site", false));
250
251 List<Group> groups = GroupLocalServiceUtil.dynamicQuery(dynamicQuery);
252
253 for (Group group : groups) {
254 if ((group.getPrivateLayoutsPageCount() > 0) ||
255 (group.getPublicLayoutsPageCount() > 0)) {
256
257 group.setSite(true);
258
259 GroupLocalServiceUtil.updateGroup(group);
260 }
261 }
262
263 dynamicQuery = DynamicQueryFactoryUtil.forClass(Group.class);
264
265 dynamicQuery.add(
266 RestrictionsFactoryUtil.eq("name", GroupConstants.CONTROL_PANEL));
267 dynamicQuery.add(RestrictionsFactoryUtil.eq("site", true));
268
269 groups = GroupLocalServiceUtil.dynamicQuery(dynamicQuery);
270
271 for (Group group : groups) {
272 group.setSite(false);
273
274 GroupLocalServiceUtil.updateGroup(group);
275 }
276 }
277
278 protected void verifyStagedGroups() throws Exception {
279 List<Group> groups = GroupLocalServiceUtil.getLiveGroups();
280
281 for (Group group : groups) {
282 if (!group.hasStagingGroup()) {
283 continue;
284 }
285
286 UnicodeProperties typeSettingsProperties =
287 group.getTypeSettingsProperties();
288
289 typeSettingsProperties.setProperty(
290 "staged", Boolean.TRUE.toString());
291 typeSettingsProperties.setProperty(
292 "stagedRemotely", Boolean.FALSE.toString());
293
294 GroupLocalServiceUtil.updateGroup(
295 group.getGroupId(), typeSettingsProperties.toString());
296
297 Group stagingGroup = group.getStagingGroup();
298
299 if (group.getClassNameId() != stagingGroup.getClassNameId()) {
300 stagingGroup.setClassNameId(group.getClassNameId());
301
302 GroupLocalServiceUtil.updateGroup(stagingGroup);
303 }
304 }
305 }
306
307 private static Log _log = LogFactoryUtil.getLog(VerifyGroup.class);
308
309 }