001    /**
002     * Copyright (c) 2000-2013 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.Date;
032    import java.util.List;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     */
037    public class VerifyUser extends VerifyProcess {
038    
039            @Override
040            protected void doVerify() throws Exception {
041                    List<User> users = UserLocalServiceUtil.getNoContacts();
042    
043                    if (_log.isDebugEnabled()) {
044                            _log.debug(
045                                    "Processing " + users.size() + " users with no contacts");
046                    }
047    
048                    Date now = new Date();
049    
050                    for (User user : users) {
051                            if (_log.isDebugEnabled()) {
052                                    _log.debug("Creating contact for user " + user.getUserId());
053                            }
054    
055                            long contactId = CounterLocalServiceUtil.increment();
056    
057                            Contact contact = ContactLocalServiceUtil.createContact(contactId);
058    
059                            Company company = CompanyLocalServiceUtil.getCompanyById(
060                                    user.getCompanyId());
061    
062                            contact.setCompanyId(user.getCompanyId());
063                            contact.setUserId(user.getUserId());
064                            contact.setUserName(StringPool.BLANK);
065                            contact.setCreateDate(now);
066                            contact.setModifiedDate(now);
067                            contact.setAccountId(company.getAccountId());
068                            contact.setParentContactId(
069                                    ContactConstants.DEFAULT_PARENT_CONTACT_ID);
070                            contact.setFirstName(user.getFirstName());
071                            contact.setMiddleName(user.getMiddleName());
072                            contact.setLastName(user.getLastName());
073                            contact.setPrefixId(0);
074                            contact.setSuffixId(0);
075                            contact.setJobTitle(user.getJobTitle());
076    
077                            ContactLocalServiceUtil.updateContact(contact);
078    
079                            user.setContactId(contactId);
080    
081                            UserLocalServiceUtil.updateUser(user);
082                    }
083    
084                    if (_log.isDebugEnabled()) {
085                            _log.debug("Contacts verified for users");
086                    }
087    
088                    users = UserLocalServiceUtil.getNoGroups();
089    
090                    if (_log.isDebugEnabled()) {
091                            _log.debug("Processing " + users.size() + " users with no groups");
092                    }
093    
094                    for (User user : users) {
095                            if (_log.isDebugEnabled()) {
096                                    _log.debug("Creating group for user " + user.getUserId());
097                            }
098    
099                            GroupLocalServiceUtil.addGroup(
100                                    user.getUserId(), GroupConstants.DEFAULT_PARENT_GROUP_ID,
101                                    User.class.getName(), user.getUserId(),
102                                    GroupConstants.DEFAULT_LIVE_GROUP_ID, null, null, 0, true,
103                                    GroupConstants.DEFAULT_MEMBERSHIP_RESTRICTION,
104                                    StringPool.SLASH + user.getScreenName(), false, true, null);
105                    }
106    
107                    if (_log.isDebugEnabled()) {
108                            _log.debug("Groups verified for users");
109                    }
110            }
111    
112            private static Log _log = LogFactoryUtil.getLog(VerifyUser.class);
113    
114    }