BeanShell Scripting in Liferay

Hi,
Today Im going to explain you how to run a BeanShell Script in Liferay6.1.
Just for now consider “If you can code in Java, you can code in BeanShell as well”.

I had a requirement to assign some UserGroups to Roles which exists with the same name as UserGroup.
I had 1000s of Groups and 1000s of Roles in the System. I was looking to accomplish the task programatically rather than manually. Then came to my rescue is the BeanShell Scripting.
Before looking into the code we’ll just see what are the implicit Liferay objects availble in BeanShell.

  • Implicit Objects
  • portletConfig, portletContext, actionRequest, actionResponse, out
    
  • Here is the script to assign UserGroups to Roles. The UserGroup will be created if it doesn’t exist.
  • 
    		import com.liferay.portal.model.Role;
    		import com.liferay.portal.model.UserGroup;
    		import com.liferay.portal.model.Role;
    		import com.liferay.portal.util.PortalUtil;
    		import com.liferay.portal.service.RoleLocalServiceUtil;
    		import com.liferay.portal.service.UserGroupLocalServiceUtil;
    		import com.liferay.portal.service.GroupLocalServiceUtil;
    		import com.liferay.portal.service.RoleLocalServiceUtil;
    		import com.liferay.portal.kernel.exception.PortalException;
    		import com.liferay.portal.kernel.exception.SystemException;
    
    		String groups = "groupnames,as,csv";
    		String[] groupNames = groups.split(",");
    		long companyId = PortalUtil.getCompanyId(actionRequest);
    
    		for(String groupName : groupNames){
    			try{
    				Role role = RoleLocalServiceUtil.getRole(companyId, groupName);
    				UserGroup userGroup = null;
    				try {
    					userGroup = UserGroupLocalServiceUtil.getUserGroup(companyId, groupName);
    				} catch (PortalException e1) {
    					out.println(e1.getMessage());
    					out.println("Creating Group : " + groupName);
    					userGroup = UserGroupLocalServiceUtil.addUserGroup(PortalUtil.getUserId(actionRequest), companyId, groupName, groupName);
    				} catch (SystemException e1) {
    				}
    
    				long[] userGroupIds = new long [1];
    				userGroupIds [0] = GroupLocalServiceUtil.getUserGroupGroup(companyId,userGroup .getUserGroupId()).getGroupId();
    
    				GroupLocalServiceUtil.addRoleGroups(role.getRoleId(),userGroupIds);
    				out.println("Added Group [" +userGroup.getName() + "] to Role["+ role.getName()+"]");
    			}
    			catch (PortalException e) {
    				out.println(e.getMessage());
    			} catch (SystemException e) {
    				out.println(e.getMessage());
    			}
    		}
    	
    
  • Run the Script
  • Goto Contol Panel -> Server Administration -> Script
    Select Beanshell in the dropdown list and paste the code.

  • Output
  • If you want to inject any Custom Objects into beanshell then just add it to the portletObjects Map in
    com.liferay.portlet.admin.action.EditServerAction
    
  • protected void runScript(
    			PortletConfig portletConfig, ActionRequest actionRequest,
    			ActionResponse actionResponse)
    		throws Exception {
    
    		String language = ParamUtil.getString(actionRequest, "language");
    		String script = ParamUtil.getString(actionRequest, "script");
    
    		PortletContext portletContext = portletConfig.getPortletContext();
    
    		Map<String, Object> portletObjects = ScriptingUtil.getPortletObjects(
    			portletConfig, portletContext, actionRequest, actionResponse);
    
    		UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
    			new UnsyncByteArrayOutputStream();
    
    		UnsyncPrintWriter unsyncPrintWriter = UnsyncPrintWriterPool.borrow(
    			unsyncByteArrayOutputStream);
    
    		portletObjects.put("out", unsyncPrintWriter);
    
    		try {
    			SessionMessages.add(actionRequest, "language", language);
    			SessionMessages.add(actionRequest, "script", script);
    
    			ScriptingUtil.exec(null, portletObjects, language, script);
    
    			unsyncPrintWriter.flush();
    
    			SessionMessages.add(
    				actionRequest, "script_output",
    				unsyncByteArrayOutputStream.toString());
    		}
    		catch (ScriptingException se) {
    			SessionErrors.add(
    				actionRequest, ScriptingException.class.getName(), se);
    
    			_log.error(se.getMessage());
    		}
    	}
    
    
Advertisements