Foros de discusión
Increment user id
Ay Kay, modificado hace 11 años.
Increment user id
Junior Member Mensajes: 52 Fecha de incorporación: 17/11/11 Mensajes recientes
Hi there.
Is this the correct way to get hold of a new user id?
Appreciate any feedback!
Is this the correct way to get hold of a new user id?
Long newClientId = CounterLocalServiceUtil.increment(User.class.getName());
Appreciate any feedback!
David H Nebinger, modificado hace 11 años.
RE: Increment user id
Liferay Legend Mensajes: 14919 Fecha de incorporación: 2/09/06 Mensajes recientes
yup
Hitoshi Ozawa, modificado hace 11 años.
RE: Increment user id
Liferay Legend Mensajes: 7942 Fecha de incorporación: 24/03/10 Mensajes recientes
Almost. It's "long" with small "l"
long newClientId = CounterLocalServiceUtil.increment(User.class.getName());
long newClientId = CounterLocalServiceUtil.increment(User.class.getName());
Mika Koivisto, modificado hace 11 años.
RE: Increment user id
Liferay Legend Mensajes: 1519 Fecha de incorporación: 7/08/06 Mensajes recientes
What ever you are doing don't do it!!! It's probably a bad idea. If you need to add a new user use
or
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);
Ay Kay, modificado hace 11 años.
RE: Increment user id
Junior Member Mensajes: 52 Fecha de incorporación: 17/11/11 Mensajes recientesMika 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)!
David H Nebinger, modificado hace 11 años.
RE: Increment user id
Liferay Legend Mensajes: 14919 Fecha de incorporación: 2/09/06 Mensajes recientesAy 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.