Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LPS-124657 Change the order to make sure we're not running into the race condition #658

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@
import com.liferay.portal.kernel.model.LayoutSet;
import com.liferay.portal.kernel.model.LayoutTemplate;
import com.liferay.portal.kernel.model.LayoutTypePortlet;
import com.liferay.portal.kernel.model.Role;
import com.liferay.portal.kernel.model.User;
import com.liferay.portal.kernel.model.role.RoleConstants;
import com.liferay.portal.kernel.module.framework.ModuleServiceLifecycle;
import com.liferay.portal.kernel.security.auth.PrincipalThreadLocal;
import com.liferay.portal.kernel.service.GroupLocalService;
import com.liferay.portal.kernel.service.LayoutLocalService;
import com.liferay.portal.kernel.service.LayoutSetLocalService;
import com.liferay.portal.kernel.service.RoleLocalService;
import com.liferay.portal.kernel.service.ServiceContext;
import com.liferay.portal.kernel.service.ServiceContextThreadLocal;
import com.liferay.portal.kernel.service.UserLocalService;
Expand All @@ -46,6 +50,8 @@
import com.liferay.portal.util.PropsUtil;
import com.liferay.portal.util.PropsValues;

import java.util.List;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

Expand Down Expand Up @@ -73,15 +79,15 @@ public void portalInstanceRegistered(Company company) throws Exception {
protected void addDefaultGuestPublicLayoutByProperties(Group group)
throws PortalException {

long defaultUserId = _userLocalService.getDefaultUserId(
group.getCompanyId());
User user = _getUser(group.getCompanyId());

String friendlyURL = FriendlyURLNormalizerUtil.normalizeWithEncoding(
PropsValues.DEFAULT_GUEST_PUBLIC_LAYOUT_FRIENDLY_URL);

ServiceContext serviceContext = new ServiceContext();

Layout layout = _layoutLocalService.addLayout(
defaultUserId, group.getGroupId(), false,
user.getUserId(), group.getGroupId(), false,
LayoutConstants.DEFAULT_PARENT_LAYOUT_ID,
PropsValues.DEFAULT_GUEST_PUBLIC_LAYOUT_NAME, StringPool.BLANK,
StringPool.BLANK, LayoutConstants.TYPE_CONTENT, false, friendlyURL,
Expand All @@ -102,6 +108,28 @@ protected void addDefaultGuestPublicLayoutByProperties(Group group)

layout = _layoutCopyHelper.copyLayout(draftLayout, layout);

LayoutTypePortlet layoutTypePortlet =
(LayoutTypePortlet)layout.getLayoutType();

layoutTypePortlet.setLayoutTemplateId(
0, PropsValues.DEFAULT_GUEST_PUBLIC_LAYOUT_TEMPLATE_ID, false);

LayoutTemplate layoutTemplate =
layoutTypePortlet.getLayoutTemplate();

for (String columnId : layoutTemplate.getColumns()) {
String keyPrefix = PropsKeys.DEFAULT_GUEST_PUBLIC_LAYOUT_PREFIX;

String portletIds = PropsUtil.get(keyPrefix.concat(columnId));

layoutTypePortlet.addPortletIds(
0, StringUtil.split(portletIds), columnId, false);
}

_layoutLocalService.updateLayout(
layout.getGroupId(), layout.isPrivateLayout(),
layout.getLayoutId(), layout.getTypeSettings());

_layoutLocalService.updatePriority(
layout.getPlid(), LayoutConstants.FIRST_PRIORITY);

Expand All @@ -112,62 +140,41 @@ protected void addDefaultGuestPublicLayoutByProperties(Group group)
_layoutLocalService.updateStatus(
layout.getUserId(), draftLayout.getPlid(),
WorkflowConstants.STATUS_APPROVED, serviceContext);
}
catch (Exception exception) {
throw new PortalException(exception);
}
finally {
PrincipalThreadLocal.setName(currentName);
ServiceContextThreadLocal.pushServiceContext(currentServiceContext);
}

LayoutTypePortlet layoutTypePortlet =
(LayoutTypePortlet)layout.getLayoutType();

layoutTypePortlet.setLayoutTemplateId(
0, PropsValues.DEFAULT_GUEST_PUBLIC_LAYOUT_TEMPLATE_ID, false);

LayoutTemplate layoutTemplate = layoutTypePortlet.getLayoutTemplate();

for (String columnId : layoutTemplate.getColumns()) {
String keyPrefix = PropsKeys.DEFAULT_GUEST_PUBLIC_LAYOUT_PREFIX;
boolean updateLayoutSet = false;

String portletIds = PropsUtil.get(keyPrefix.concat(columnId));
LayoutSet layoutSet = layout.getLayoutSet();

layoutTypePortlet.addPortletIds(
0, StringUtil.split(portletIds), columnId, false);
}
if (Validator.isNotNull(
PropsValues.DEFAULT_GUEST_PUBLIC_LAYOUT_REGULAR_THEME_ID)) {

_layoutLocalService.updateLayout(
layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
layout.getTypeSettings());
layoutSet.setThemeId(
PropsValues.DEFAULT_GUEST_PUBLIC_LAYOUT_REGULAR_THEME_ID);

boolean updateLayoutSet = false;
updateLayoutSet = true;
}

LayoutSet layoutSet = layout.getLayoutSet();
if (Validator.isNotNull(
PropsValues.
DEFAULT_GUEST_PUBLIC_LAYOUT_REGULAR_COLOR_SCHEME_ID)) {

if (Validator.isNotNull(
PropsValues.DEFAULT_GUEST_PUBLIC_LAYOUT_REGULAR_THEME_ID)) {
layoutSet.setColorSchemeId(
PropsValues.
DEFAULT_GUEST_PUBLIC_LAYOUT_REGULAR_COLOR_SCHEME_ID);

layoutSet.setThemeId(
PropsValues.DEFAULT_GUEST_PUBLIC_LAYOUT_REGULAR_THEME_ID);
updateLayoutSet = true;
}

updateLayoutSet = true;
if (updateLayoutSet) {
_layoutSetLocalService.updateLayoutSet(layoutSet);
}
}

if (Validator.isNotNull(
PropsValues.
DEFAULT_GUEST_PUBLIC_LAYOUT_REGULAR_COLOR_SCHEME_ID)) {

layoutSet.setColorSchemeId(
PropsValues.
DEFAULT_GUEST_PUBLIC_LAYOUT_REGULAR_COLOR_SCHEME_ID);

updateLayoutSet = true;
catch (Exception exception) {
throw new PortalException(exception);
}

if (updateLayoutSet) {
_layoutSetLocalService.updateLayoutSet(layoutSet);
finally {
PrincipalThreadLocal.setName(currentName);
ServiceContextThreadLocal.pushServiceContext(currentServiceContext);
}
}

Expand All @@ -184,6 +191,24 @@ protected void setModuleServiceLifecycle(
ModuleServiceLifecycle moduleServiceLifecycle) {
}

private User _getUser(long companyId) throws PortalException {
Role role = _roleLocalService.fetchRole(
companyId, RoleConstants.ADMINISTRATOR);

if (role == null) {
return _userLocalService.getDefaultUser(companyId);
}

List<User> adminUsers = _userLocalService.getRoleUsers(
role.getRoleId(), 0, 1);

if (adminUsers.isEmpty()) {
return _userLocalService.getDefaultUser(companyId);
}

return adminUsers.get(0);
}

@Reference
private DefaultLayoutDefinitionImporter _defaultLayoutDefinitionImporter;

Expand All @@ -202,6 +227,9 @@ protected void setModuleServiceLifecycle(
@Reference
private Portal _portal;

@Reference
private RoleLocalService _roleLocalService;

@Reference
private UserLocalService _userLocalService;

Expand Down