Skip to content

Instantly share code, notes, and snippets.

@martinlau
Last active December 10, 2015 20:49
Show Gist options
  • Save martinlau/4491154 to your computer and use it in GitHub Desktop.
Save martinlau/4491154 to your computer and use it in GitHub Desktop.
GroupLocalServiceImpl snippet
/**
* Adds a group.
*
* @param userId the primary key of the group's creator/owner
* @param parentGroupId the primary key of the parent group
* @param className the entity's class name
* @param classPK the primary key of the entity's instance
* @param liveGroupId the primary key of the live group
* @param name the entity's name
* @param description the group's description (optionally
* <code>null</code>)
* @param type the group's type. For more information see {@link
* com.liferay.portal.model.GroupConstants}
* @param friendlyURL the group's friendlyURL (optionally
* <code>null</code>)
* @param site whether the group is to be associated with a main site
* @param active whether the group is active
* @param serviceContext the service context to be applied (optionally
* <code>null</code>). 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 Group addGroup(
long userId, long parentGroupId, String className, long classPK,
long liveGroupId, String name, String description, int type,
String friendlyURL, boolean site, boolean active,
ServiceContext serviceContext)
throws PortalException, SystemException {
// Group
User user = userPersistence.findByPrimaryKey(userId);
className = GetterUtil.getString(className);
long classNameId = PortalUtil.getClassNameId(className);
String friendlyName = name;
long groupId = 0;
while (true) {
groupId = counterLocalService.increment();
User screenNameUser = userPersistence.fetchByC_SN(
user.getCompanyId(), String.valueOf(groupId));
if (screenNameUser == null) {
break;
}
}
boolean staging = isStaging(serviceContext);
long groupClassNameId = PortalUtil.getClassNameId(Group.class);
if ((classNameId <= 0) || className.equals(Group.class.getName())) {
className = Group.class.getName();
classNameId = groupClassNameId;
classPK = groupId;
}
else if (className.equals(Organization.class.getName())) {
name = getOrgGroupName(name);
}
else if (!GroupConstants.USER_PERSONAL_SITE.equals(name)) {
name = String.valueOf(classPK);
}
if (className.equals(Organization.class.getName()) && staging) {
classPK = liveGroupId;
}
if (className.equals(Layout.class.getName())) {
Layout layout = layoutLocalService.getLayout(classPK);
parentGroupId = layout.getGroupId();
}
friendlyURL = getFriendlyURL(
user.getCompanyId(), groupId, classNameId, classPK, friendlyName,
friendlyURL);
if (staging) {
name = name.concat(" (Staging)");
friendlyURL = friendlyURL.concat("-staging");
}
if (className.equals(Group.class.getName())) {
if (!site && (liveGroupId == 0) &&
!name.equals(GroupConstants.CONTROL_PANEL)) {
throw new IllegalArgumentException();
}
}
else if (!className.equals(Organization.class.getName()) &&
className.startsWith("com.liferay.portal.model.")) {
if (site) {
throw new IllegalArgumentException();
}
}
if ((classNameId <= 0) || className.equals(Group.class.getName())) {
validateName(groupId, user.getCompanyId(), name, site);
}
validateFriendlyURL(
user.getCompanyId(), groupId, classNameId, classPK, friendlyURL);
Group group = groupPersistence.create(groupId);
group.setCompanyId(user.getCompanyId());
group.setCreatorUserId(userId);
group.setClassNameId(classNameId);
group.setClassPK(classPK);
group.setParentGroupId(parentGroupId);
group.setLiveGroupId(liveGroupId);
group.setName(name);
group.setDescription(description);
group.setType(type);
group.setFriendlyURL(friendlyURL);
group.setSite(site);
group.setActive(active);
if ((serviceContext != null) && (classNameId == groupClassNameId) &&
!user.isDefaultUser()) {
group.setExpandoBridgeAttributes(serviceContext);
}
groupPersistence.update(group);
// Layout sets
layoutSetLocalService.addLayoutSet(groupId, true);
layoutSetLocalService.addLayoutSet(groupId, false);
if ((classNameId == groupClassNameId) && !user.isDefaultUser()) {
// Resources
resourceLocalService.addResources(
group.getCompanyId(), 0, 0, Group.class.getName(),
group.getGroupId(), false, false, false);
// Site roles
Role role = roleLocalService.getRole(
group.getCompanyId(), RoleConstants.SITE_OWNER);
userGroupRoleLocalService.addUserGroupRoles(
userId, groupId, new long[] {role.getRoleId()});
// User
userLocalService.addGroupUsers(
group.getGroupId(), new long[] {userId});
// Asset
if (serviceContext != null) {
updateAsset(
userId, group, serviceContext.getAssetCategoryIds(),
serviceContext.getAssetTagNames());
}
}
else if (className.equals(Organization.class.getName()) &&
!user.isDefaultUser()) {
// Resources
resourceLocalService.addResources(
group.getCompanyId(), 0, 0, Group.class.getName(),
group.getGroupId(), false, false, false);
}
return group;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment