001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }