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 }
130 }
131
132 protected void verifyNullFriendlyURLGroups() throws Exception {
133 List<Group> groups = GroupLocalServiceUtil.getNullFriendlyURLGroups();
134
135 for (Group group : groups) {
136 String friendlyURL = StringPool.SLASH + group.getGroupId();
137
138 User user = null;
139
140 if (group.isCompany()) {
141 friendlyURL = GroupConstants.GLOBAL_FRIENDLY_URL;
142 }
143 else if (group.isUser()) {
144 user = UserLocalServiceUtil.getUserById(group.getClassPK());
145
146 friendlyURL = StringPool.SLASH + user.getScreenName();
147 }
148 else if (group.getClassPK() > 0) {
149 friendlyURL = StringPool.SLASH + group.getClassPK();
150 }
151
152 try {
153 GroupLocalServiceUtil.updateFriendlyURL(
154 group.getGroupId(), friendlyURL);
155 }
156 catch (GroupFriendlyURLException gfurle) {
157 if (user != null) {
158 long userId = user.getUserId();
159 String screenName = user.getScreenName();
160
161 if (_log.isInfoEnabled()) {
162 _log.info(
163 "Updating user screen name " + screenName + " to " +
164 userId + " because it is generating an " +
165 "invalid friendly URL");
166 }
167
168 UserLocalServiceUtil.updateScreenName(
169 userId, String.valueOf(userId));
170 }
171 else {
172 _log.error("Invalid Friendly URL " + friendlyURL);
173
174 throw gfurle;
175 }
176 }
177 }
178 }
179
180 protected void verifyOrganizationNames() throws Exception {
181 Connection con = null;
182 PreparedStatement ps = null;
183 ResultSet rs = null;
184
185 try {
186 con = DataAccess.getUpgradeOptimizedConnection();
187
188 StringBundler sb = new StringBundler(5);
189
190 sb.append("select groupId, name from Group_ where name like '%");
191 sb.append(GroupLocalServiceImpl.ORGANIZATION_NAME_SUFFIX);
192 sb.append("%' and name not like '%");
193 sb.append(GroupLocalServiceImpl.ORGANIZATION_NAME_SUFFIX);
194 sb.append("'");
195
196 ps = con.prepareStatement(sb.toString());
197
198 rs = ps.executeQuery();
199
200 while (rs.next()) {
201 long groupId = rs.getLong("groupId");
202 String name = rs.getString("name");
203
204 int pos = name.indexOf(
205 GroupLocalServiceImpl.ORGANIZATION_NAME_SUFFIX);
206
207 pos = name.indexOf(" ", pos + 1);
208
209 String newName =
210 name.substring(pos + 1) +
211 GroupLocalServiceImpl.ORGANIZATION_NAME_SUFFIX;
212
213 updateName(groupId, newName);
214 }
215 }
216 finally {
217 DataAccess.cleanUp(con, ps, rs);
218 }
219 }
220
221 protected void verifyRobots() throws Exception {
222 List<Group> groups = GroupLocalServiceUtil.getLiveGroups();
223
224 for (Group group : groups) {
225 LayoutSet privateLayoutSet = group.getPrivateLayoutSet();
226 LayoutSet publicLayoutSet = group.getPublicLayoutSet();
227
228 String privateLayoutSetRobots = getRobots(privateLayoutSet);
229 String publicLayoutSetRobots = getRobots(publicLayoutSet);
230
231 UnicodeProperties typeSettingsProperties =
232 group.getTypeSettingsProperties();
233
234 typeSettingsProperties.setProperty(
235 "true-robots.txt", privateLayoutSetRobots);
236 typeSettingsProperties.setProperty(
237 "false-robots.txt", publicLayoutSetRobots);
238
239 GroupLocalServiceUtil.updateGroup(
240 group.getGroupId(), typeSettingsProperties.toString());
241 }
242 }
243
244 protected void verifySites() throws Exception {
245 DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
246 Group.class);
247
248 dynamicQuery.add(
249 RestrictionsFactoryUtil.eq(
250 "classNameId", PortalUtil.getClassNameId(Organization.class)));
251 dynamicQuery.add(RestrictionsFactoryUtil.eq("site", false));
252
253 List<Group> groups = GroupLocalServiceUtil.dynamicQuery(dynamicQuery);
254
255 for (Group group : groups) {
256 if ((group.getPrivateLayoutsPageCount() > 0) ||
257 (group.getPublicLayoutsPageCount() > 0)) {
258
259 group.setSite(true);
260
261 GroupLocalServiceUtil.updateGroup(group);
262 }
263 }
264 }
265
266 protected void verifyStagedGroups() throws Exception {
267 List<Group> groups = GroupLocalServiceUtil.getLiveGroups();
268
269 for (Group group : groups) {
270 if (!group.hasStagingGroup()) {
271 continue;
272 }
273
274 UnicodeProperties typeSettingsProperties =
275 group.getTypeSettingsProperties();
276
277 typeSettingsProperties.setProperty(
278 "staged", Boolean.TRUE.toString());
279 typeSettingsProperties.setProperty(
280 "stagedRemotely", Boolean.FALSE.toString());
281
282 GroupLocalServiceUtil.updateGroup(
283 group.getGroupId(), typeSettingsProperties.toString());
284
285 Group stagingGroup = group.getStagingGroup();
286
287 if (group.getClassNameId() != stagingGroup.getClassNameId()) {
288 stagingGroup.setClassNameId(group.getClassNameId());
289
290 GroupLocalServiceUtil.updateGroup(stagingGroup);
291 }
292 }
293 }
294
295 protected void verifyTree() throws Exception {
296 long[] companyIds = PortalInstances.getCompanyIdsBySQL();
297
298 for (long companyId : companyIds) {
299 GroupLocalServiceUtil.rebuildTree(companyId);
300 }
301 }
302
303 private static Log _log = LogFactoryUtil.getLog(VerifyGroup.class);
304
305 }