1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.verify;
16  
17  import com.liferay.counter.service.CounterLocalServiceUtil;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.model.Company;
22  import com.liferay.portal.model.Contact;
23  import com.liferay.portal.model.ContactConstants;
24  import com.liferay.portal.model.User;
25  import com.liferay.portal.service.CompanyLocalServiceUtil;
26  import com.liferay.portal.service.ContactLocalServiceUtil;
27  import com.liferay.portal.service.GroupLocalServiceUtil;
28  import com.liferay.portal.service.UserLocalServiceUtil;
29  
30  import java.util.Date;
31  import java.util.List;
32  
33  /**
34   * <a href="VerifyUser.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   */
38  public class VerifyUser extends VerifyProcess {
39  
40      protected void doVerify() throws Exception {
41          List<User> users = UserLocalServiceUtil.getNoContacts();
42  
43          if (_log.isDebugEnabled()) {
44              _log.debug(
45                  "Processing " + users.size() + " users with no contacts");
46          }
47  
48          Date now = new Date();
49  
50          for (User user : users) {
51              if (_log.isDebugEnabled()) {
52                  _log.debug("Creating contact for user " + user.getUserId());
53              }
54  
55              long contactId = CounterLocalServiceUtil.increment();
56  
57              Contact contact = ContactLocalServiceUtil.createContact(contactId);
58  
59              Company company = CompanyLocalServiceUtil.getCompanyById(
60                  user.getCompanyId());
61  
62              contact.setCompanyId(user.getCompanyId());
63              contact.setUserId(user.getUserId());
64              contact.setUserName(StringPool.BLANK);
65              contact.setCreateDate(now);
66              contact.setModifiedDate(now);
67              contact.setAccountId(company.getAccountId());
68              contact.setParentContactId(
69                  ContactConstants.DEFAULT_PARENT_CONTACT_ID);
70              contact.setFirstName(user.getFirstName());
71              contact.setMiddleName(user.getMiddleName());
72              contact.setLastName(user.getLastName());
73              contact.setPrefixId(0);
74              contact.setSuffixId(0);
75              contact.setJobTitle(user.getJobTitle());
76  
77              ContactLocalServiceUtil.updateContact(contact);
78  
79              user.setContactId(contactId);
80  
81              UserLocalServiceUtil.updateUser(user);
82          }
83  
84          if (_log.isDebugEnabled()) {
85              _log.debug("Contacts verified for users");
86          }
87  
88          users = UserLocalServiceUtil.getNoGroups();
89  
90          if (_log.isDebugEnabled()) {
91              _log.debug("Processing " + users.size() + " users with no groups");
92          }
93  
94          for (User user : users) {
95              if (_log.isDebugEnabled()) {
96                  _log.debug("Creating group for user " + user.getUserId());
97              }
98  
99              GroupLocalServiceUtil.addGroup(
100                 user.getUserId(), User.class.getName(), user.getUserId(), null,
101                 null, 0, StringPool.SLASH + user.getScreenName(), true, null);
102         }
103 
104         if (_log.isDebugEnabled()) {
105             _log.debug("Groups verified for users");
106         }
107     }
108 
109     private static Log _log = LogFactoryUtil.getLog(VerifyUser.class);
110 
111 }