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