1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.verify;
24  
25  import com.liferay.counter.service.CounterLocalServiceUtil;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.model.Company;
30  import com.liferay.portal.model.Contact;
31  import com.liferay.portal.model.ContactConstants;
32  import com.liferay.portal.model.User;
33  import com.liferay.portal.service.CompanyLocalServiceUtil;
34  import com.liferay.portal.service.ContactLocalServiceUtil;
35  import com.liferay.portal.service.GroupLocalServiceUtil;
36  import com.liferay.portal.service.UserLocalServiceUtil;
37  
38  import java.util.Date;
39  import java.util.List;
40  
41  /**
42   * <a href="VerifyUser.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   */
46  public class VerifyUser extends VerifyProcess {
47  
48      protected void doVerify() throws Exception {
49          List<User> users = UserLocalServiceUtil.getNoContacts();
50  
51          if (_log.isDebugEnabled()) {
52              _log.debug(
53                  "Processing " + users.size() + " users with no contacts");
54          }
55  
56          Date now = new Date();
57  
58          for (User user : users) {
59              if (_log.isDebugEnabled()) {
60                  _log.debug("Creating contact for user " + user.getUserId());
61              }
62  
63              long contactId = CounterLocalServiceUtil.increment();
64  
65              Contact contact = ContactLocalServiceUtil.createContact(contactId);
66  
67              Company company = CompanyLocalServiceUtil.getCompanyById(
68                  user.getCompanyId());
69  
70              contact.setCompanyId(user.getCompanyId());
71              contact.setUserId(user.getUserId());
72              contact.setUserName(StringPool.BLANK);
73              contact.setCreateDate(now);
74              contact.setModifiedDate(now);
75              contact.setAccountId(company.getAccountId());
76              contact.setParentContactId(
77                  ContactConstants.DEFAULT_PARENT_CONTACT_ID);
78              contact.setFirstName(user.getFirstName());
79              contact.setMiddleName(user.getMiddleName());
80              contact.setLastName(user.getLastName());
81              contact.setPrefixId(0);
82              contact.setSuffixId(0);
83              contact.setMale(user.isMale());
84              contact.setBirthday(user.getBirthday());
85              contact.setJobTitle(user.getJobTitle());
86  
87              ContactLocalServiceUtil.updateContact(contact);
88  
89              user.setContactId(contactId);
90  
91              UserLocalServiceUtil.updateUser(user);
92          }
93  
94          if (_log.isDebugEnabled()) {
95              _log.debug("Contacts verified for users");
96          }
97  
98          users = UserLocalServiceUtil.getNoGroups();
99  
100         if (_log.isDebugEnabled()) {
101             _log.debug("Processing " + users.size() + " users with no groups");
102         }
103 
104         for (User user : users) {
105             if (_log.isDebugEnabled()) {
106                 _log.debug("Creating group for user " + user.getUserId());
107             }
108 
109             GroupLocalServiceUtil.addGroup(
110                 user.getUserId(), User.class.getName(), user.getUserId(), null,
111                 null, 0, StringPool.SLASH + user.getScreenName(), true);
112         }
113 
114         if (_log.isDebugEnabled()) {
115             _log.debug("Groups verified for users");
116         }
117     }
118 
119     private static Log _log = LogFactoryUtil.getLog(VerifyUser.class);
120 
121 }