Mittwoch, 30. März 2016

Reading custom attribute from hook Liferay.

Some time you want to read custom attribute from hook or from function which was called by some ajax request. You will get null value when you will try to access these attributes through expand bridge unless you set the permission first.  Some thing similar as given below

final Role adminRole = RoleLocalServiceUtil.getRole(user.getCompanyId(), "Administrator");
final List<User> adminUsers =UserLocalServiceUtil.getRoleUsers(adminRole.getRoleId());
PrincipalThreadLocal.setName(adminUsers.get(0).getUserId());
final PermissionChecker permissionChecker =PermissionCheckerFactoryUtil.create(adminUsers.get(0));

Dienstag, 29. März 2016

Liferay Reflection

MethodKey methodKey =
new MethodKey(ClassResolverUtil.
resolveByPortalClassLoader("com.liferay.portal.util.WebAppPool"),
"get", new Class[]{Long.class,String.class});

PortletCategory portletCategory =(PortletCategory)PortalClassInvoker.invoke(false, methodKey, new Object[] {new Long(user.getCompanyId()),"PORTLET_CATEGORY"});


For custom bean defined in a portlet

Object obj =PortletBeanLocatorUtil.locate("Injector-portlet", "your.portlets.ItemTool");
Class<?> claz = obj.getClass();
Object instance = claz.newInstance();
claz.getMethod("methodName").invoke(instance);