Skip to content

Instantly share code, notes, and snippets.

@otw1248
Last active December 12, 2016 02:13
Show Gist options
  • Save otw1248/979e19107912895de1e715fb6fe296d0 to your computer and use it in GitHub Desktop.
Save otw1248/979e19107912895de1e715fb6fe296d0 to your computer and use it in GitHub Desktop.
liferay

organization group
OrganizationLocalServiceImpl
    public Organization addOrganization(long userId, long parentOrganizationId, String name, String type, boolean recursable, long regionId, long countryId, int statusId, String comments, ServiceContext serviceContext) throws PortalException, SystemException {
        User user = this.userPersistence.findByPrimaryKey(userId);
        parentOrganizationId = this.getParentOrganizationId(user.getCompanyId(), parentOrganizationId);
        recursable = true;
        this.validate(user.getCompanyId(), parentOrganizationId, name, type, countryId, statusId);
        long organizationId = this.counterLocalService.increment();
        Organization organization = this.organizationPersistence.create(organizationId);
        organization.setCompanyId(user.getCompanyId());
        organization.setParentOrganizationId(parentOrganizationId);
        organization.setName(name);
        organization.setType(type);
        organization.setRecursable(recursable);
        organization.setRegionId(regionId);
        organization.setCountryId(countryId);
        organization.setStatusId(statusId);
        organization.setComments(comments);
        this.organizationPersistence.update(organization, false);
        Group group = this.groupLocalService.addGroup(userId, Organization.class.getName(), organizationId, (String)null, (String)null, 0, (String)null, true);
        if(PropsValues.ORGANIZATIONS_ASSIGNMENT_AUTO) {
            Role expandoBridge = this.roleLocalService.getRole(organization.getCompanyId(), "Organization Owner");
            this.userGroupRoleLocalService.addUserGroupRoles(userId, group.getGroupId(), new long[]{expandoBridge.getRoleId()});
            this.userPersistence.addOrganization(userId, organizationId);
        }

        this.addOrganizationResources(userId, organization);
        ExpandoBridge expandoBridge1 = organization.getExpandoBridge();
        expandoBridge1.setAttributes(serviceContext);
        return organization;
    }
public interface Organization extends OrganizationModel {
    List<Organization> getAncestors() throws PortalException, SystemException;

    Organization getParentOrganization() throws PortalException, SystemException;

    Group getGroup();
GroupLocalServiceImpl
	public Group addGroup(
			long userId, String className, long classPK, long liveGroupId,
			String name, String description, int type, String friendlyURL,
			boolean active)
		throws PortalException, SystemException {

		// Group

		User user = userPersistence.findByPrimaryKey(userId);
		className = GetterUtil.getString(className);
		long classNameId = PortalUtil.getClassNameId(className);

		String friendlyName = name;

		if (className.equals(Organization.class.getName())) {
			Organization organization =
				organizationPersistence.findByPrimaryKey(classPK);

			friendlyName = organization.getName();
		}

		long groupId = counterLocalService.increment();

		friendlyURL = getFriendlyURL(
			user.getCompanyId(), groupId, classNameId, classPK, friendlyName,
			friendlyURL);

		if ((classNameId <= 0) || (classPK <= 0)) {
			validateName(groupId, user.getCompanyId(), name);
		}

		try
		{
		    validateFriendlyURL(
		        groupId, user.getCompanyId(), classNameId, classPK, friendlyURL);
		}
		catch(GroupFriendlyURLException gfue)
		{
		    friendlyURL = "/" + String.valueOf(groupId);
		}
		
		Group group = groupPersistence.create(groupId);

		group.setCompanyId(user.getCompanyId());
		group.setCreatorUserId(userId);
		group.setClassNameId(classNameId);
		group.setClassPK(classPK);
		group.setParentGroupId(GroupConstants.DEFAULT_PARENT_GROUP_ID);
		group.setLiveGroupId(liveGroupId);
		group.setName(friendlyName);
		group.setDescription(description);
		group.setType(type);
		group.setFriendlyURL(friendlyURL);
		group.setActive(active);

		groupPersistence.update(group, false);

		// Layout sets

		layoutSetLocalService.addLayoutSet(groupId, true);

		layoutSetLocalService.addLayoutSet(groupId, false);

		if ((classNameId <= 0) && (classPK <= 0) && !user.isDefaultUser()) {

			// Resources

			resourceLocalService.addResources(
				group.getCompanyId(), 0, 0, Group.class.getName(),
				group.getGroupId(), false, false, false);

			// Community roles

			Role role = roleLocalService.getRole(
				group.getCompanyId(), RoleConstants.COMMUNITY_OWNER);

			userGroupRoleLocalService.addUserGroupRoles(
				userId, groupId, new long[] {role.getRoleId()});

			// User

			userLocalService.addGroupUsers(
				group.getGroupId(), new long[] {userId});
		}
		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;
	}
from:https://github.com/liferay/liferay-portal/blob/master/portal-impl/src/com/liferay/portal/events/ServicePreAction.java
~~~
protected void servicePre(
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ThemeDisplay themeDisplay = initThemeDisplay(request, response);
if (themeDisplay == null) {
return;
}
request.setAttribute(WebKeys.THEME_DISPLAY, themeDisplay);
// Service context
ServiceContext serviceContext = ServiceContextFactory.getInstance(
request);
ServiceContextThreadLocal.pushServiceContext(serviceContext);
// Ajaxable render
if (PropsValues.LAYOUT_AJAX_RENDER_ENABLE) {
boolean portletAjaxRender = ParamUtil.getBoolean(
request, "p_p_ajax", true);
request.setAttribute(
WebKeys.PORTLET_AJAX_RENDER, portletAjaxRender);
}
// Parallel render
if (PropsValues.LAYOUT_PARALLEL_RENDER_ENABLE &&
ServerDetector.isTomcat()) {
boolean portletParallelRender = ParamUtil.getBoolean(
request, "p_p_parallel", true);
request.setAttribute(
WebKeys.PORTLET_PARALLEL_RENDER, portletParallelRender);
}
}
protected void updateUserLayouts(User user) throws Exception {
Boolean hasPowerUserRole = null;
// Private layouts
boolean addDefaultUserPrivateLayouts = false;
if (PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_ENABLED &&
PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_AUTO_CREATE) {
addDefaultUserPrivateLayouts = true;
if (PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_POWER_USER_REQUIRED) {
if (hasPowerUserRole == null) {
hasPowerUserRole = hasPowerUserRole(user);
}
if (!hasPowerUserRole.booleanValue()) {
addDefaultUserPrivateLayouts = false;
}
}
}
Boolean hasPrivateLayouts = null;
if (addDefaultUserPrivateLayouts) {
hasPrivateLayouts = LayoutLocalServiceUtil.hasLayouts(
user, true, false);
if (!hasPrivateLayouts) {
addDefaultUserPrivateLayouts(user);
}
}
boolean deleteDefaultUserPrivateLayouts = false;
if (!PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_ENABLED) {
deleteDefaultUserPrivateLayouts = true;
}
else if (PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_POWER_USER_REQUIRED) {
if (hasPowerUserRole == null) {
hasPowerUserRole = hasPowerUserRole(user);
}
if (!hasPowerUserRole.booleanValue()) {
deleteDefaultUserPrivateLayouts = true;
}
}
if (deleteDefaultUserPrivateLayouts) {
if (hasPrivateLayouts == null) {
hasPrivateLayouts = LayoutLocalServiceUtil.hasLayouts(
user, true, false);
}
if (hasPrivateLayouts) {
deleteDefaultUserPrivateLayouts(user);
}
}
// Public pages
boolean addDefaultUserPublicLayouts = false;
if (PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_ENABLED &&
PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_AUTO_CREATE) {
addDefaultUserPublicLayouts = true;
if (PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_POWER_USER_REQUIRED) {
if (hasPowerUserRole == null) {
hasPowerUserRole = hasPowerUserRole(user);
}
if (!hasPowerUserRole.booleanValue()) {
addDefaultUserPublicLayouts = false;
}
}
}
Boolean hasPublicLayouts = null;
if (addDefaultUserPublicLayouts) {
hasPublicLayouts = LayoutLocalServiceUtil.hasLayouts(
user, false, false);
if (!hasPublicLayouts) {
addDefaultUserPublicLayouts(user);
}
}
boolean deleteDefaultUserPublicLayouts = false;
if (!PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_ENABLED) {
deleteDefaultUserPublicLayouts = true;
}
else if (PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_POWER_USER_REQUIRED) {
if (hasPowerUserRole == null) {
hasPowerUserRole = hasPowerUserRole(user);
}
if (!hasPowerUserRole.booleanValue()) {
deleteDefaultUserPublicLayouts = true;
}
}
if (deleteDefaultUserPublicLayouts) {
if (hasPublicLayouts == null) {
hasPublicLayouts = LayoutLocalServiceUtil.hasLayouts(
user, false, false);
}
if (hasPublicLayouts) {
deleteDefaultUserPublicLayouts(user);
}
}
}
~~~
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment