logo-preloader
. . .
blog-details-image

What is Groovy Script ?

© 2019 ByteParity Technologies. OCTOBER 31, 2017 WHAT IS GROOVY SCRIPT ? Liferay provides a powerful script engine that supports Beanshell, Javascript, Groovy, Python and Ruby. In Liferay 7, only Groovy is available by default. It has support for all the other mentioned languages but to use them you need to add appropriate modules. Scripts can be used for different purpose by different set of users like Administrators, Developers and QA for which they don’t need to bother about code execution or databases access. Just to explain you the power of scripting engine here we use Groovy. Groovy Script Console can be accessed from the following path,

“Control Panel -> Server Administration -> Script”

Usage of Script engine

  1. If you are a developer and you don’t have databases access and suppose you need to check list of users with some specifications then Groovy Script will work for you.
  2. If you are part of QA team and you need to create some Users with roles then using Script you can perform action faster than manual action.
  3. If you are Administrator and you need to add some tag in web-contents in a bulk, then Groovy script will do it for you very quickly.

How to use Groovy Script

Groovy Script execution is very simple. Only you need to write your script in the console and click on execute button.

That’s it ! 🙂

For better understanding please refer below examples.

Example 1:-

In this example we are creating new User with specific Role. Fields are added in the top of the script so one can customize userName, emailAddress, password, roleName etc as per your need. If you want to create user without any role then left roleName as blank.

import java.util.Calendar
import java.util.Locale
import com.liferay.portal.kernel.util.LocaleUtil
import com.liferay.portal.kernel.util.StringPool
import com.liferay.portal.kernel.util.ArrayUtil
import com.liferay.portal.kernel.util.Validator
import com.liferay.portal.kernel.model.Role
import com.liferay.portal.kernel.model.RoleConstants
import com.liferay.portal.kernel.service.UserServiceUtil
import com.liferay.portal.kernel.service.ServiceContext
import com.liferay.portal.kernel.service.RoleLocalServiceUtil
import com.liferay.portal.kernel.exception.NoSuchRoleException
boolean autoPassword = false;
boolean autoScreenName = true;
String screenName = StringPool.BLANK;
long facebookId = 0;
String openId = StringPool.BLANK;
Locale locale = LocaleUtil.getDefault();
String middleName = StringPool.BLANK;
long prefixId = 0;
long suffixId = 0;
boolean male = true;
int birthdayMonth = Calendar.JANUARY;
int birthdayDay = 1;
int birthdayYear = 1970;
String jobTitle = StringPool.BLANK;
long[] groupIds = null;
long[] organizationIds = null;
long[] roleIds = null;
Role role = null;
long[] userGroupIds = null;
boolean sendMail = false;
 
String firstName = "Albert";
String lastName = "Coetzee";
String emailAddress = "albert.coetzee@liferay.com";
String password = "test";
String roleName = "Admin";
try{
 
themeDisplay = actionRequest.getAttribute("LIFERAY_SHARED_THEME_DISPLAY");
groupId = themeDisplay.getScopeGroupId();
companyId = themeDisplay.getCompanyId();
userId = themeDisplay.getUserId();
// Create Role if not exist
if(Validator.isNotNull(roleName)){
 
try{
role = RoleLocalServiceUtil.getRole(companyId, roleName)
}catch(NoSuchRoleException re){
role = RoleLocalServiceUtil.addRole(userId, Role.class.getName(), 0, roleName, null, null, RoleConstants.TYPE_REGULAR, null, null);
println("Role <b>"+roleName+"</b> created successfully !");
}
}
if(Validator.isNotNull(emailAddress)){
 
long[] roleIdArray = new long[0]
if(Validator.isNotNull(role)){
roleIdArray = ArrayUtil.append(roleIdArray, Long.valueOf(role.getRoleId()))
}
 
roleIds = roleIdArray;
ServiceContext serviceContext = new ServiceContext();
serviceContext.setAddGroupPermissions(true);
serviceContext.setAddGuestPermissions(true);
serviceContext.setCompanyId(companyId);
serviceContext.setScopeGroupId(groupId);
 
UserServiceUtil.addUser(
companyId, autoPassword, password,
password, autoScreenName, screenName, emailAddress, facebookId,
openId, locale, firstName, middleName, lastName, prefixId,
suffixId, male, birthdayMonth, birthdayDay, birthdayYear,
jobTitle, groupIds, organizationIds, roleIds, userGroupIds,
sendMail, serviceContext);
 
println("User <b>"+ emailAddress +"</b> created successfully");
}
}catch(e){
println("""<div class="portlet-msg-error">${e}</div>""")
}

Output :-

output1

Example 2 :-

Suppose you want add one more tag if any specific tag is exists in the web-content. For example, you want to add tag “Hardware” for all the web-contents with tag “DELL” exists then following script is for you. Here also, structureName, and tagNames are provided in the top so one can modify it easily.

importjava.util.Calendar
importjava.util.Locale
importcom.liferay.portal.kernel.util.LocaleUtil
importcom.liferay.portal.kernel.util.StringPool
importcom.liferay.portal.kernel.util.ArrayUtil
importcom.liferay.portal.kernel.util.Validator
importcom.liferay.portal.kernel.model.Role
importcom.liferay.portal.kernel.model.RoleConstants
importcom.liferay.portal.kernel.service.UserServiceUtil
importcom.liferay.portal.kernel.service.ServiceContext
importcom.liferay.portal.kernel.service.RoleLocalServiceUtil
importcom.liferay.portal.kernel.exception.NoSuchRoleException
boolean autoPassword =false;
boolean autoScreenName =true;
String screenName =StringPool.BLANK;
long facebookId =0;
String openId =StringPool.BLANK;
Locale locale =LocaleUtil.getDefault();
String middleName =StringPool.BLANK;
long prefixId =0;
long suffixId =0;
boolean male =true;
int birthdayMonth =Calendar.JANUARY;
int birthdayDay =1;
int birthdayYear =1970;
String jobTitle =StringPool.BLANK;
long[] groupIds =null;
long[] organizationIds =null;
long[] roleIds =null;
Role role =null;
long[] userGroupIds =null;
boolean sendMail =false;
String firstName ="Albert";
String lastName ="Coetzee";
String emailAddress ="albert.coetzee@liferay.com";
String password ="test";
String roleName ="Admin";
try{
themeDisplay = actionRequest.getAttribute("LIFERAY_SHARED_THEME_DISPLAY");
groupId = themeDisplay.getScopeGroupId();
companyId = themeDisplay.getCompanyId();
userId = themeDisplay.getUserId();
// Create Role if not exist
if(Validator.isNotNull(roleName)){
try{
role =RoleLocalServiceUtil.getRole(companyId, roleName)
}catch(NoSuchRoleException re){
role =RoleLocalServiceUtil.addRole(userId, Role.class.getName(), 0, roleName, null, null, RoleConstants.TYPE_REGULAR, null, null);
println("Role <b>"+roleName+"</b> created successfully !");
}
}
if(Validator.isNotNull(emailAddress)){
long[] roleIdArray =newlong[0]
if(Validator.isNotNull(role)){
roleIdArray =ArrayUtil.append(roleIdArray, Long.valueOf(role.getRoleId()))
}
roleIds = roleIdArray;
ServiceContext serviceContext =newServiceContext();
serviceContext.setAddGroupPermissions(true);
serviceContext.setAddGuestPermissions(true);
serviceContext.setCompanyId(companyId);
serviceContext.setScopeGroupId(groupId);
UserServiceUtil.addUser(
companyId, autoPassword, password,
password, autoScreenName, screenName, emailAddress, facebookId,
openId, locale, firstName, middleName, lastName, prefixId,
suffixId, male, birthdayMonth, birthdayDay, birthdayYear,
jobTitle, groupIds, organizationIds, roleIds, userGroupIds,
sendMail, serviceContext);
println("User <b>"+ emailAddress +"</b> created successfully");
}
}catch(e){
println("""<div class="portlet-msg-error">${e}</div>""")
}

Output :-

output2

Isn’t it easy and simple !!! Please feel free to ask us question in comment box if any.

Thank you !

 

No Comments

Have a creative idea in mind?

Let’s discuss & work together!