Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Ay Kay
Increment user id
February 4, 2013 11:50 AM
Answer

Ay Kay

Rank: Junior Member

Posts: 52

Join Date: November 17, 2011

Recent Posts

Hi there.

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

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

Appreciate any feedback!
David H Nebinger
RE: Increment user id
February 4, 2013 1:23 PM
Answer

David H Nebinger

Community Moderator

Rank: Liferay Legend

Posts: 13216

Join Date: September 1, 2006

Recent Posts

yup
Hitoshi Ozawa
RE: Increment user id
February 4, 2013 3:05 PM
Answer

Hitoshi Ozawa

Rank: Liferay Legend

Posts: 7949

Join Date: March 23, 2010

Recent Posts

Almost. It's "long" with small "l"

long newClientId = CounterLocalServiceUtil.increment(User.class.getName());
Mika Koivisto
RE: Increment user id
February 4, 2013 5:59 PM
Answer

Mika Koivisto

Rank: Liferay Legend

Posts: 1518

Join Date: August 7, 2006

Recent Posts

What ever you are doing don't do it!!! It's probably a bad idea. If you need to add a new user use

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

or

1UserLocalServiceUtil.addUserWithWorkflow(
2                creatorUserId, companyId, autoPassword, password1, password2,
3                autoScreenName, screenName, emailAddress, facebookId, openId,
4                locale, firstName, middleName, lastName, prefixId, suffixId,
5                male, birthdayMonth, birthdayDay, birthdayYear, jobTitle,
6                groupIds, organizationIds, roleIds, userGroupIds, sendEmail,
7                serviceContext);
Ay Kay
RE: Increment user id
February 5, 2013 9:46 AM
Answer

Ay Kay

Rank: Junior Member

Posts: 52

Join Date: November 17, 2011

Recent Posts

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:

 1       
 2        currentUser = PortalUtil.getUser(this.request);
 3        serviceContext = ServiceContextFactory.getInstance(this.request);
 4        serviceContext.setCompanyId(ActionUtil.getCompanyId(this.request));
 5
 6...
 7
 8    Button saveButton = new Button("Create new client", new Button.ClickListener() {
 9            @Override
10            public void buttonClick(ClickEvent event)
11            {
12                String title = (String)titleBox.getValue();
13                String firstName = (String)firstNameField.getValue();
14                String familyName = (String)familyNameField.getValue();
15                Date dateOfBirth = (Date)dateOfBirthField.getValue();
16                String gender = (String)genderBox.getValue();
17                String loginName = (String)loginNameField.getValue();
18                String emailAddress = (String)emailAddressField.getValue();
19                String clientNumber = (String)clientNumberField.getValue();
20
21                Long facebookId = 0L;
22                String openId = null;
23                Locale locale = Locale.ENGLISH;
24                String middleName = "";
25                boolean male = gender.equals(Literals.Gender.Male.getLabel());
26
27                int prefixId = 0;
28                int suffixId = 0;
29                int birthdayMonth = dateOfBirth.getMonth();
30                int birthdayDay = dateOfBirth.getDay();
31                int birthdayYear = dateOfBirth.getYear();
32
33                String jobTitle = "";
34
35                long[] organizationIds = new long[] { chamberId };
36                long[] groupIds = new long[] { 0L };
37                long[] roleIds = new long[] { 0L };
38                long[] userGroupIds = new long[] { 0L };
39
40                boolean autoPassword = false;
41                boolean autoScreenName = false;
42                boolean sendEmail = false;
43
44                String password = "test";
45
46                User newClient = null;
47
48                try
49                {
50                    Long creatorUserId = currentUser.getUserId();
51                    Long companyId = currentUser.getCompanyId();
52
53                    newClient = UserLocalServiceUtil.addUser(creatorUserId, companyId, autoPassword, password,
54                            password, autoScreenName, loginName, emailAddress, facebookId, openId, locale, firstName,
55                            middleName, familyName, prefixId, suffixId, male, birthdayMonth, birthdayDay, birthdayYear,
56                            jobTitle, groupIds, organizationIds, roleIds, userGroupIds, sendEmail, serviceContext);
57                   
58                    window.getParent().removeWindow(window);
59                }


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
RE: Increment user id
February 5, 2013 10:19 AM
Answer

David H Nebinger

Community Moderator

Rank: Liferay Legend

Posts: 13216

Join Date: September 1, 2006

Recent Posts

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.

Participate in the State of Liferay Community 2017. Help the community and even win some prizes!