001    /**
002     * Copyright (c) 2000-present 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.GroupConstants;
025    import com.liferay.portal.model.User;
026    import com.liferay.portal.service.CompanyLocalServiceUtil;
027    import com.liferay.portal.service.ContactLocalServiceUtil;
028    import com.liferay.portal.service.GroupLocalServiceUtil;
029    import com.liferay.portal.service.UserLocalServiceUtil;
030    
031    import java.util.List;
032    import java.util.Locale;
033    import java.util.Map;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     */
038    public class VerifyUser extends VerifyProcess {
039    
040            @Override
041            protected void doVerify() throws Exception {
042                    List<User> users = UserLocalServiceUtil.getNoContacts();
043    
044                    if (_log.isDebugEnabled()) {
045                            _log.debug(
046                                    "Processing " + users.size() + " users with no contacts");
047                    }
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.setAccountId(company.getAccountId());
065                            contact.setParentContactId(
066                                    ContactConstants.DEFAULT_PARENT_CONTACT_ID);
067                            contact.setFirstName(user.getFirstName());
068                            contact.setMiddleName(user.getMiddleName());
069                            contact.setLastName(user.getLastName());
070                            contact.setPrefixId(0);
071                            contact.setSuffixId(0);
072                            contact.setJobTitle(user.getJobTitle());
073    
074                            ContactLocalServiceUtil.updateContact(contact);
075    
076                            user.setContactId(contactId);
077    
078                            UserLocalServiceUtil.updateUser(user);
079                    }
080    
081                    if (_log.isDebugEnabled()) {
082                            _log.debug("Contacts verified for users");
083                    }
084    
085                    users = UserLocalServiceUtil.getNoGroups();
086    
087                    if (_log.isDebugEnabled()) {
088                            _log.debug("Processing " + users.size() + " users with no groups");
089                    }
090    
091                    for (User user : users) {
092                            if (_log.isDebugEnabled()) {
093                                    _log.debug("Creating group for user " + user.getUserId());
094                            }
095    
096                            GroupLocalServiceUtil.addGroup(
097                                    user.getUserId(), GroupConstants.DEFAULT_PARENT_GROUP_ID,
098                                    User.class.getName(), user.getUserId(),
099                                    GroupConstants.DEFAULT_LIVE_GROUP_ID, (Map<Locale, String>)null,
100                                    null, 0, true, GroupConstants.DEFAULT_MEMBERSHIP_RESTRICTION,
101                                    StringPool.SLASH + user.getScreenName(), false, true, null);
102                    }
103    
104                    if (_log.isDebugEnabled()) {
105                            _log.debug("Groups verified for users");
106                    }
107            }
108    
109            private static final Log _log = LogFactoryUtil.getLog(VerifyUser.class);
110    
111    }