留言板

Tricky question: how to create group and user programmatically?

IGC SA,修改在12 年前。

Tricky question: how to create group and user programmatically?

Junior Member 帖子: 80 加入日期: 11-6-8 最近的帖子
I've been trying to create a group (a community) programmatically and I'm having a lot more trouble than expected.

What I first used was the following code:

ServiceContext serviceContext = ServiceContextFactory.getInstance(
				className, [b]actionRequest[/b]);
		Group comunidad = GroupLocalServiceUtil.addGroup(JSFUtil.getUserId(), className, [b]classPK[/b],
				nombreComunidad, descripcionComunidad, tipoComunidad, friendlyURL, active, serviceContext);


but I didn't know how to get classPK, so I decided to use instead the simpler webservice call:


Group group = GroupServiceUtil.addGroup(
				nombreComunidad, descripcionComunidad, tipoComunidad, friendlyURL, active, serviceContext);


But I still have several problems and doubts:
-I don't understand why so many parameters are necessary (I thought that having name, description and active would be more than enough to create a group).
-I don't know which of those parameters can be set to null. For example, I think that friendlyURL is not necessary to create a community.
-I don't know how to get either the actionRequest or seviceContext on JSF.

I would also like to know if there's any way to know which parameters are mandatory for any of methods on Liferay's services (looking at Liferay's source code doesn't help much).

On the other hand, I'm having problems with user creation. I found the following snippet on the forums (http://www.liferay.com/es/community/forums/-/message_boards/message/6354119):

UserServiceUtil.addUser(
themeDisplay.getCompanyId(), autoPassword, password1, password2,
autoScreenName, screenName, emailAddress, facebookId, openId,
LocaleUtil.getDefault(), firstName, middleName, lastName, prefixId,
suffixId, male, birthdayMonth, birthdayDay, birthdayYear, jobTitle,
groupIds, organizationIds, roleIds, userGroupIds, sendEmail,
addresses, emailAddresses, phones, websites,
announcementsDeliveries, serviceContext);


and on Liferay's source code I found this different way of doing it (EditUserAction.java) :

...
ServiceContext serviceContext = ServiceContextFactory.getInstance(
			User.class.getName(), actionRequest);
...
if (cmd.equals(Constants.ADD)) {

			// Add user

			user = UserServiceUtil.addUser(
				themeDisplay.getCompanyId(), autoPassword, password1, password2,
				autoScreenName, screenName, emailAddress, openId,
				themeDisplay.getLocale(), firstName, middleName, lastName,
				prefixId, suffixId, male, birthdayMonth, birthdayDay,
				birthdayYear, jobTitle, groupIds, organizationIds,
				roleIds, userGroupIds, sendEmail, addresses, emailAddresses,
				phones, websites, announcementsDeliveries, serviceContext);

			if (!userGroupRoles.isEmpty()) {
				for (UserGroupRole userGroupRole : userGroupRoles) {
					userGroupRole.setUserId(user.getUserId());
				}

				user = UserServiceUtil.updateUser(
					user.getUserId(), StringPool.BLANK, StringPool.BLANK,
					StringPool.BLANK, false, reminderQueryQuestion,
					reminderQueryAnswer, screenName, emailAddress, openId,
					languageId, timeZoneId, greeting, comments, firstName,
					middleName, lastName, prefixId, suffixId, male,
					birthdayMonth, birthdayDay, birthdayYear, smsSn, aimSn,
					facebookSn, icqSn, jabberSn, msnSn, mySpaceSn, skypeSn,
					twitterSn, ymSn, jobTitle, groupIds, organizationIds,
					roleIds, userGroupRoles, userGroupIds, addresses,
					emailAddresses, phones, websites, announcementsDeliveries,
					serviceContext);
			}
		}


-Which of both codes will work correctly? Is it necessary to update the user -updateUser- after adding the user -addUser- ???
-Are all those parameters necessary?
-Still don't know how to get the serviceContext on JSF (can't get actionRequest)

I'll be very grateful if at least some of those questions are answered, I thought that doing simple actions such as creating users and groups would be a lot less difficult!
BTW, if anybody knows about the existance of a simpler "LiferayServicesUtil" or similar class that makes use of those services but simplifies and minimizes the use of parameters it would be great to have a look at it.

Cheers.
H. William Connors II,修改在12 年前。

RE: Tricky question: how to create group and user programmatically?

New Member 帖子: 24 加入日期: 11-2-3 最近的帖子
Here is the code I use to create groups and it seems to work fine for me.


ServiceContext serviceContext = new ServiceContext();
serviceContext.setAddCommunityPermissions(true);
serviceContext.setAddGuestPermissions(true);

Group group = GroupServiceUtil.addGroup(groupName, "", GroupConstants.TYPE_COMMUNITY_RESTRICTED, "", true, serviceContext);



I haven't created Users programatically yet but I would suggested you grab the source code for the 7Cogs hook and look at that. They create several objects programatically (including Users) and it serves as some great example code.
IGC SA,修改在12 年前。

RE: Tricky question: how to create group and user programmatically?

Junior Member 帖子: 80 加入日期: 11-6-8 最近的帖子
Thank you very much for your answer. I guess that GroupLocalServiceUtil.createGroup(...) is not necessary before the call to GroupServiceUtil.addGroup(...)
I'll take a look at the 7Cogs code for User creation (hadn't ever thought of that actually).

Thanks again for pointing me in the right direction..

PS. Just found the 7cogs code for user creation at sevencogs-hook\WEB-INF\src\com\liferay\sevencogs\hook\events\StartupAction.java, I hope it helps other people:

protected User addUser(
			long companyId, String screenName, String firstName,
			String lastName, boolean male, String jobTitle, long[] roleIds)
		throws Exception {
long creatorUserId = 0;
		boolean autoPassword = false;
		String password1 = screenName;
		String password2 = password1;
		boolean autoScreenName = false;
		String emailAddress = screenName + "@7cogs.com";
		String openId = StringPool.BLANK;
		Locale locale = Locale.US;
		String middleName = StringPool.BLANK;
		int prefixId = 0;
		int suffixId = 0;
		int birthdayMonth = Calendar.JANUARY;
		int birthdayDay = 1;
		int birthdayYear = 1970;

		Group guestGroup = GroupLocalServiceUtil.getGroup(
			companyId, GroupConstants.GUEST);

		long[] groupIds = new long[] {guestGroup.getGroupId()};

		Organization sevenCogsOrganization =
			OrganizationLocalServiceUtil.getOrganization(
				companyId, "7Cogs, Inc.");

		long[] organizationIds = new long[] {
			sevenCogsOrganization.getOrganizationId()
		};

		long[] userGroupIds = null;
		boolean sendEmail = false;
		ServiceContext serviceContext = null;

		User user = UserLocalServiceUtil.addUser(
			creatorUserId, companyId, autoPassword, password1, password2,
			autoScreenName, screenName, emailAddress, openId, locale, firstName,
			middleName, lastName, prefixId, suffixId, male, birthdayMonth,
			birthdayDay, birthdayYear, jobTitle, groupIds, organizationIds,
			roleIds, userGroupIds, sendEmail, serviceContext);

....
H. William Connors II,修改在12 年前。

RE: Tricky question: how to create group and user programmatically?

New Member 帖子: 24 加入日期: 11-2-3 最近的帖子
So after doing a bit more experimenting I suspect you want to use GroupLocalServiceUtil instead of GroupServiceUtil that I used in my first example.


GroupLocalServiceUtil.addGroup(userId, null, 0, "Group Name", "", GroupConstants.TYPE_COMMUNITY_RESTRICTED, "", true, serviceContext);