/**
* Adds a group and load layoutSets from prototypes.
*
* Some parameters of the GroupLocalServiceUtil.addGroup are preset:
* The creator of the group is set to the admin user
* The group created is of entity type Group
* The group created is active, has a main site, and is of type Private.
*
* @param name the entity's name
* @param description the group's description (optionally null)
* @param friendlyURL the group's friendlyURL (optionally null)
* @param publicLayoutSetPrototypeId ID of the LayoutSetPrototype for the public pages
* @param privateLayoutSetPrototypeId ID of the LayoutSetPrototype for the private pages
* @param serviceContext the service context to be applied (optionally null).
* Can set asset category IDs and asset tag names for the group,
* and whether the group is for staging.
* @return the group
* @throws PortalException if a creator could not be found, if the group's
* information was invalid, if a layout could not be found, or if a
* valid friendly URL could not be created for the group
* @throws SystemException if a system exception occurred
*/
public static Group createGroup(
String name, String description, String friendlyURL,
long publicLayoutSetPrototypeId, long privateLayoutSetPrototypeId,
ServiceContext serviceContext) throws PortalException, SystemException, Exception {
long userId = serviceContext.getUserId();
Group group = GroupLocalServiceUtil.addGroup(
userId, Group.class.getName(), 0,
name, description, GroupConstants.TYPE_SITE_PRIVATE,
friendlyURL, true, true,
serviceContext);
applyLayoutSetPrototypes(group, publicLayoutSetPrototypeId, privateLayoutSetPrototypeId);
return group;
}
/**
* This method applies LayoutSetPrototypes to a newly created group.
* To do so, it calls the method
* com.liferay.portlet.sites.util.SitesUtil.updateLayoutSetPrototypesLinks
* normally unaccessible (it is in portal-impl), but called using
* PortalClassInvoker.invoke.
*
* @param group : Group to which you want to add pages
* @param publicLayoutSetPrototypeId
* @param privateLayoutSetPrototypeId
* @throws Exception
*/
public static void applyLayoutSetPrototypes(
Group group, long publicLayoutSetPrototypeId, long privateLayoutSetPrototypeId)
throws Exception {
MethodKey key = methodKeyFor("updateLayoutSetPrototypesLinks", Group.class, long.class, long.class, boolean.class, boolean.class);
invokePortalClassMethod(key, group, publicLayoutSetPrototypeId, privateLayoutSetPrototypeId, true, true);
}
private static MethodKey methodKeyFor(String methodName, Class<?>... parameterTypes) {
return new MethodKey(DELEGATE_CLASS_NAME, methodName, parameterTypes);
}
private static Object invokePortalClassMethod(MethodKey key, Object... arguments) throws Exception {
try {
return PortalClassInvoker.invoke(false, key, arguments);
} catch (PortalException e) {
throw e;
} catch (Exception e) {
throw e;
}
}
private static final String DELEGATE_CLASS_NAME =
"com.liferay.portlet.sites.util.SitesUtil";
The following code provides a workaround: