001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.verify;
016    
017    import com.liferay.counter.service.CounterLocalServiceUtil;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.model.Company;
022    import com.liferay.portal.model.Contact;
023    import com.liferay.portal.model.ContactConstants;
024    import com.liferay.portal.model.User;
025    import com.liferay.portal.service.CompanyLocalServiceUtil;
026    import com.liferay.portal.service.ContactLocalServiceUtil;
027    import com.liferay.portal.service.GroupLocalServiceUtil;
028    import com.liferay.portal.service.UserLocalServiceUtil;
029    
030    import java.util.Date;
031    import java.util.List;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public class VerifyUser extends VerifyProcess {
037    
038            @Override
039            protected void doVerify() throws Exception {
040                    List<User> users = UserLocalServiceUtil.getNoContacts();
041    
042                    if (_log.isDebugEnabled()) {
043                            _log.debug(
044                                    "Processing " + users.size() + " users with no contacts");
045                    }
046    
047                    Date now = new Date();
048    
049                    for (User user : users) {
050                            if (_log.isDebugEnabled()) {
051                                    _log.debug("Creating contact for user " + user.getUserId());
052                            }
053    
054                            long contactId = CounterLocalServiceUtil.increment();
055    
056                            Contact contact = ContactLocalServiceUtil.createContact(contactId);
057    
058                            Company company = CompanyLocalServiceUtil.getCompanyById(
059                                    user.getCompanyId());
060    
061                            contact.setCompanyId(user.getCompanyId());
062                            contact.setUserId(user.getUserId());
063                            contact.setUserName(StringPool.BLANK);
064                            contact.setCreateDate(now);
065                            contact.setModifiedDate(now);
066                            contact.setAccountId(company.getAccountId());
067                            contact.setParentContactId(
068                                    ContactConstants.DEFAULT_PARENT_CONTACT_ID);
069                            contact.setFirstName(user.getFirstName());
070                            contact.setMiddleName(user.getMiddleName());
071                            contact.setLastName(user.getLastName());
072                            contact.setPrefixId(0);
073                            contact.setSuffixId(0);
074                            contact.setJobTitle(user.getJobTitle());
075    
076                            ContactLocalServiceUtil.updateContact(contact);
077    
078                            user.setContactId(contactId);
079    
080                            UserLocalServiceUtil.updateUser(user);
081                    }
082    
083                    if (_log.isDebugEnabled()) {
084                            _log.debug("Contacts verified for users");
085                    }
086    
087                    users = UserLocalServiceUtil.getNoGroups();
088    
089                    if (_log.isDebugEnabled()) {
090                            _log.debug("Processing " + users.size() + " users with no groups");
091                    }
092    
093                    for (User user : users) {
094                            if (_log.isDebugEnabled()) {
095                                    _log.debug("Creating group for user " + user.getUserId());
096                            }
097    
098                            GroupLocalServiceUtil.addGroup(
099                                    user.getUserId(), User.class.getName(), user.getUserId(), null,
100                                    null, 0, StringPool.SLASH + user.getScreenName(), false, true,
101                                    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    }