Fórumok

Increment user id

thumbnail
Ay Kay, módosítva 11 év-val korábban

Increment user id

Junior Member Bejegyzések: 52 Csatlakozás dátuma: 2011.11.17. Legújabb bejegyzések
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, módosítva 11 év-val korábban

RE: Increment user id

Liferay Legend Bejegyzések: 14916 Csatlakozás dátuma: 2006.09.02. Legújabb bejegyzések
yup
thumbnail
Hitoshi Ozawa, módosítva 11 év-val korábban

RE: Increment user id

Liferay Legend Bejegyzések: 7942 Csatlakozás dátuma: 2010.03.24. Legújabb bejegyzések
Almost. It's "long" with small "l"

long newClientId = CounterLocalServiceUtil.increment(User.class.getName());
thumbnail
Mika Koivisto, módosítva 11 év-val korábban

RE: Increment user id

Liferay Legend Bejegyzések: 1519 Csatlakozás dátuma: 2006.08.07. Legújabb bejegyzések
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, módosítva 11 év-val korábban

RE: Increment user id

Junior Member Bejegyzések: 52 Csatlakozás dátuma: 2011.11.17. Legújabb bejegyzések
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, módosítva 11 év-val korábban

RE: Increment user id

Liferay Legend Bejegyzések: 14916 Csatlakozás dátuma: 2006.09.02. Legújabb bejegyzések
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.