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