1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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.setMale(user.isMale());
76              contact.setBirthday(user.getBirthday());
77              contact.setJobTitle(user.getJobTitle());
78  
79              ContactLocalServiceUtil.updateContact(contact);
80  
81              user.setContactId(contactId);
82  
83              UserLocalServiceUtil.updateUser(user);
84          }
85  
86          if (_log.isDebugEnabled()) {
87              _log.debug("Contacts verified for users");
88          }
89  
90          users = UserLocalServiceUtil.getNoGroups();
91  
92          if (_log.isDebugEnabled()) {
93              _log.debug("Processing " + users.size() + " users with no groups");
94          }
95  
96          for (User user : users) {
97              if (_log.isDebugEnabled()) {
98                  _log.debug("Creating group for user " + user.getUserId());
99              }
100 
101             GroupLocalServiceUtil.addGroup(
102                 user.getUserId(), User.class.getName(), user.getUserId(), null,
103                 null, 0, StringPool.SLASH + user.getScreenName(), true, null);
104         }
105 
106         if (_log.isDebugEnabled()) {
107             _log.debug("Groups verified for users");
108         }
109     }
110 
111     private static Log _log = LogFactoryUtil.getLog(VerifyUser.class);
112 
113 }