フォーラム

ホーム » Liferay Portal » English » 3. Development

構造的に表示 平面上に表示 ツリー上に表示
スレッド [ 前へ | 次へ ]
toggle
Ay Kay
Increment user id
2013/02/04 11:50
答え

Ay Kay

ランク: Junior Member

投稿: 52

参加年月日: 2011/11/17

最近の投稿

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
2013/02/04 13:23
答え

David H Nebinger

Community Moderator

ランク: Liferay Legend

投稿: 8335

参加年月日: 2006/09/01

最近の投稿

yup
Hitoshi Ozawa
RE: Increment user id
2013/02/04 15:05
答え

Hitoshi Ozawa

ランク: Liferay Legend

投稿: 7952

参加年月日: 2010/03/23

最近の投稿

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

long newClientId = CounterLocalServiceUtil.increment(User.class.getName());
Mika Koivisto
RE: Increment user id
2013/02/04 17:59
答え

Mika Koivisto

LIFERAY STAFF

ランク: Liferay Legend

投稿: 1505

参加年月日: 2006/08/07

最近の投稿

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
2013/02/05 9:46
答え

Ay Kay

ランク: Junior Member

投稿: 52

参加年月日: 2011/11/17

最近の投稿

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
2013/02/05 10:19
答え

David H Nebinger

Community Moderator

ランク: Liferay Legend

投稿: 8335

参加年月日: 2006/09/01

最近の投稿

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.