Foren

Increment user id

thumbnail
Ay Kay, geändert vor 11 Jahren.

Increment user id

Junior Member Beiträge: 52 Beitrittsdatum: 17.11.11 Neueste Beiträge
Hi there.

Is this the correct way to get hold of a new user id?

  Long newClientId = CounterLocalServiceUtil.increment(User.class.getName());

Appreciate any feedback!
thumbnail
David H Nebinger, geändert vor 11 Jahren.

RE: Increment user id

Liferay Legend Beiträge: 14916 Beitrittsdatum: 02.09.06 Neueste Beiträge
yup
thumbnail
Hitoshi Ozawa, geändert vor 11 Jahren.

RE: Increment user id

Liferay Legend Beiträge: 7942 Beitrittsdatum: 24.03.10 Neueste Beiträge
Almost. It's "long" with small "l"

long newClientId = CounterLocalServiceUtil.increment(User.class.getName());
thumbnail
Mika Koivisto, geändert vor 11 Jahren.

RE: Increment user id

Liferay Legend Beiträge: 1519 Beitrittsdatum: 07.08.06 Neueste Beiträge
What ever you are doing don't do it!!! It's probably a bad idea. If you need to add a new user use

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

or

UserLocalServiceUtil.addUserWithWorkflow(
				creatorUserId, companyId, autoPassword, password1, password2,
				autoScreenName, screenName, emailAddress, facebookId, openId,
				locale, firstName, middleName, lastName, prefixId, suffixId,
				male, birthdayMonth, birthdayDay, birthdayYear, jobTitle,
				groupIds, organizationIds, roleIds, userGroupIds, sendEmail,
				serviceContext);
thumbnail
Ay Kay, geändert vor 11 Jahren.

RE: Increment user id

Junior Member Beiträge: 52 Beitrittsdatum: 17.11.11 Neueste Beiträge
Mika Koivisto:
What ever you are doing don't do it!!! It's probably a bad idea. If you need to add a new user use

UserLocalServiceUtil.addUser(

That was my first take. But it didn't do the job. The method just exits with no logging at all. Here's the old code:

		
		currentUser = PortalUtil.getUser(this.request);
		serviceContext = ServiceContextFactory.getInstance(this.request);
		serviceContext.setCompanyId(ActionUtil.getCompanyId(this.request));

...

    Button saveButton = new Button("Create new client", new Button.ClickListener() {
			@Override
			public void buttonClick(ClickEvent event)
			{
				String title = (String)titleBox.getValue();
				String firstName = (String)firstNameField.getValue();
				String familyName = (String)familyNameField.getValue();
				Date dateOfBirth = (Date)dateOfBirthField.getValue();
				String gender = (String)genderBox.getValue();
				String loginName = (String)loginNameField.getValue();
				String emailAddress = (String)emailAddressField.getValue();
				String clientNumber = (String)clientNumberField.getValue();

				Long facebookId = 0L;
				String openId = null;
				Locale locale = Locale.ENGLISH;
				String middleName = "";
				boolean male = gender.equals(Literals.Gender.Male.getLabel());

				int prefixId = 0;
				int suffixId = 0;
				int birthdayMonth = dateOfBirth.getMonth();
				int birthdayDay = dateOfBirth.getDay();
				int birthdayYear = dateOfBirth.getYear();

				String jobTitle = "";

				long[] organizationIds = new long[] { chamberId };
				long[] groupIds = new long[] { 0L };
				long[] roleIds = new long[] { 0L };
				long[] userGroupIds = new long[] { 0L };

				boolean autoPassword = false;
				boolean autoScreenName = false;
				boolean sendEmail = false;

				String password = "test";

				User newClient = null;

				try
				{
					Long creatorUserId = currentUser.getUserId();
					Long companyId = currentUser.getCompanyId();

					newClient = UserLocalServiceUtil.addUser(creatorUserId, companyId, autoPassword, password,
							password, autoScreenName, loginName, emailAddress, facebookId, openId, locale, firstName,
							middleName, familyName, prefixId, suffixId, male, birthdayMonth, birthdayDay, birthdayYear,
							jobTitle, groupIds, organizationIds, roleIds, userGroupIds, sendEmail, serviceContext);
					
					window.getParent().removeWindow(window);
				}


So I decided to build up a "User" myself, create a new Id and "update" it. At least that indeed creates a new user. But the created Id smelled strange, hence my question..

EDIT:

Just replaced
String openId = null;
by
String openId = "";

and that original code (with .addUser(...)) works! Many thanks (to all of you)!
thumbnail
David H Nebinger, geändert vor 11 Jahren.

RE: Increment user id

Liferay Legend Beiträge: 14916 Beitrittsdatum: 02.09.06 Neueste Beiträge
Ay Kay:
Just replaced
String openId = null;
by
String openId = "";

and that original code (with .addUser(...)) works! Many thanks (to all of you)!


That's a little known thing about the addUser() method. It should have thrown an NPE which, when combined w/ looking at the Liferay source, would have pointed out that the openId cannot be null.