Skip to content

Instantly share code, notes, and snippets.

@bmvakili
Last active August 29, 2015 13:58
Show Gist options
  • Save bmvakili/9943998 to your computer and use it in GitHub Desktop.
Save bmvakili/9943998 to your computer and use it in GitHub Desktop.
Liferay Miscellanious util scripts. Assign template to a user's pages. Written in Beanshell for Liferay Script page.
import com.liferay.portal.model.*;
import com.liferay.portal.service.*;
import com.liferay.counter.service.*;
import com.liferay.portal.util.*;
import java.util.*;
/*********** MODIFY THESE VARIABLES **********/
templateName = "User Profile Site Template"; // name of a site template
/*********** DEBUG VARIABLES *****************/
dbg = "";
C = "\n";
count = -1;
group = null;
serviceContext = ServiceContextThreadLocal.getServiceContext();
companyId = serviceContext.getCompanyId();
date = new Date();
name = "OOBLiferay";
emailAddress = date.getTime() + name + "@mailinator.com";
screenName = name + userId;
password = "Password1";
user = null;
try {
user = UserLocalServiceUtil.addUser(
0l, //long creatorUserId,
companyId, //long companyId,
false, //boolean autoPassword,
password, //String password1,
password, //String password2,
false, //boolean autoScreenName,
screenName, //String screenName,
emailAddress, //String emailAddress,
0l, //long facebookId,
"", // String openId,
Locale.US, //Locale locale,
name, //String firstName,
"", //String middleName,
name, //String lastName,
0, //int prefixId,
0, //int suffixId,
true, //boolean male,
Calendar.JANUARY, //int birthdayMonth,
1, //int birthdayDay,
1970, //int birthdayYear,
"", //String jobTitle,
null, //long[] groupIds,
null, //long[] organizationIds,
null, //long[] roleIds,
null, //long[] userGroupIds,
false, //boolean sendEmail
serviceContext
);
} catch(Exception e) {
e.printStackTrace();
}
try {
group = user.getGroup();
} catch(Exception e) {
e.printStackTrace();
}
layoutSetPrototypes = LayoutSetPrototypeLocalServiceUtil.getLayoutSetPrototypes(-1, -1);
lsp = null;
for (layoutSetPrototype : layoutSetPrototypes) {
if (layoutSetPrototype.getNameCurrentValue().equals(templateName)) {
lsp = layoutSetPrototype;
break;
}
}
pls = lsp.getLayoutSet();
groupId = group.getGroupId();
isPrivateLayout = false;
gls = group.getPublicLayoutSet();
publicLayoutSetPrototypeLinkEnabled = true;
layoutSetPrototypeUuid = lsp.getUuid();
gls.setLayoutSetPrototypeUuid(layoutSetPrototypeUuid);
LayoutSetLocalServiceUtil.updateLayoutSet(gls);
// link the layoutset to the template one
LayoutSetLocalServiceUtil.updateLayoutSetPrototypeLinkEnabled(
groupId, false, publicLayoutSetPrototypeLinkEnabled,
layoutSetPrototypeUuid);
LayoutLocalServiceUtil.updatePriorities(groupId, false);
// adds the layouts to the Layout table
// this prevents ServicePreAction pages
LayoutLocalServiceUtil.getLayouts(groupId, false, LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
if (group != null) {
count = group.getPublicLayoutsPageCount();
}
if (group != null) {
dbg += C + group.getGroupId();
dbg += C + count;
}
if (user != null) {
dbg += C + serviceContext.getPortalURL() + "/web/" + user.getScreenName();
dbg += C + user.getScreenName();
dbg += C + user.getUserId();
}
if (ls != null) {
dbg += C + ls;
dbg += C + lsp;
dbg += C + layoutSetPrototypeUuid;
}
out.println(dbg);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment