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.kernel.service.CounterLocalServiceUtil;
018    import com.liferay.portal.kernel.dao.db.DB;
019    import com.liferay.portal.kernel.dao.db.DBManagerUtil;
020    import com.liferay.portal.kernel.dao.db.DBType;
021    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
022    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
023    import com.liferay.portal.kernel.exception.PortalException;
024    import com.liferay.portal.kernel.log.Log;
025    import com.liferay.portal.kernel.log.LogFactoryUtil;
026    import com.liferay.portal.kernel.model.Company;
027    import com.liferay.portal.kernel.model.Contact;
028    import com.liferay.portal.kernel.model.ContactConstants;
029    import com.liferay.portal.kernel.model.GroupConstants;
030    import com.liferay.portal.kernel.model.User;
031    import com.liferay.portal.kernel.service.CompanyLocalServiceUtil;
032    import com.liferay.portal.kernel.service.ContactLocalServiceUtil;
033    import com.liferay.portal.kernel.service.GroupLocalServiceUtil;
034    import com.liferay.portal.kernel.service.UserLocalServiceUtil;
035    import com.liferay.portal.kernel.util.LoggingTimer;
036    import com.liferay.portal.kernel.util.PortalUtil;
037    import com.liferay.portal.kernel.util.StringBundler;
038    import com.liferay.portal.kernel.util.StringPool;
039    import com.liferay.portal.kernel.workflow.WorkflowConstants;
040    import com.liferay.portal.model.impl.UserImpl;
041    
042    import java.util.List;
043    import java.util.Locale;
044    import java.util.Map;
045    
046    /**
047     * @author Brian Wing Shun Chan
048     */
049    public class VerifyUser extends VerifyProcess {
050    
051            @Override
052            protected void doVerify() throws Exception {
053                    verifyInactive();
054                    verifyNoContacts();
055            }
056    
057            protected void verifyInactive() throws Exception {
058                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
059                            StringBundler sb = null;
060    
061                            DB db = DBManagerUtil.getDB();
062    
063                            if (db.getDBType() == DBType.MYSQL) {
064                                    sb = new StringBundler(7);
065    
066                                    sb.append("update Group_ inner join User_ on ");
067                                    sb.append("Group_.companyId = User_.companyId and ");
068                                    sb.append("Group_.classPK = User_.userId set active_ = ");
069                                    sb.append("[$FALSE$] where Group_.classNameId = ");
070                                    sb.append(PortalUtil.getClassNameId(User.class));
071                                    sb.append(" and User_.status = ");
072                                    sb.append(WorkflowConstants.STATUS_INACTIVE);
073                            }
074                            else {
075                                    sb = new StringBundler(9);
076    
077                                    sb.append("update Group_ set active_ = [$FALSE$] where ");
078                                    sb.append("groupId in (select Group_.groupId from Group_ ");
079                                    sb.append("inner join User_ on Group_.companyId = ");
080                                    sb.append("User_.companyId and Group_.classPK = User_.userId ");
081                                    sb.append("where Group_.classNameId = ");
082                                    sb.append(PortalUtil.getClassNameId(User.class));
083                                    sb.append(" and User_.status = ");
084                                    sb.append(WorkflowConstants.STATUS_INACTIVE);
085                                    sb.append(")");
086                            }
087    
088                            runSQL(sb.toString());
089    
090                            EntityCacheUtil.clearCache(UserImpl.class);
091                            FinderCacheUtil.clearCache(UserImpl.class.getName());
092                    }
093            }
094    
095            protected void verifyNoContacts() throws PortalException {
096                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
097                            List<User> users = UserLocalServiceUtil.getNoContacts();
098    
099                            if (_log.isDebugEnabled()) {
100                                    _log.debug(
101                                            "Processing " + users.size() + " users with no contacts");
102                            }
103    
104                            for (User user : users) {
105                                    if (_log.isDebugEnabled()) {
106                                            _log.debug("Creating contact for user " + user.getUserId());
107                                    }
108    
109                                    long contactId = CounterLocalServiceUtil.increment();
110    
111                                    Contact contact = ContactLocalServiceUtil.createContact(
112                                            contactId);
113    
114                                    Company company = CompanyLocalServiceUtil.getCompanyById(
115                                            user.getCompanyId());
116    
117                                    contact.setCompanyId(user.getCompanyId());
118                                    contact.setUserId(user.getUserId());
119                                    contact.setUserName(StringPool.BLANK);
120                                    contact.setAccountId(company.getAccountId());
121                                    contact.setParentContactId(
122                                            ContactConstants.DEFAULT_PARENT_CONTACT_ID);
123                                    contact.setFirstName(user.getFirstName());
124                                    contact.setMiddleName(user.getMiddleName());
125                                    contact.setLastName(user.getLastName());
126                                    contact.setPrefixId(0);
127                                    contact.setSuffixId(0);
128                                    contact.setJobTitle(user.getJobTitle());
129    
130                                    ContactLocalServiceUtil.updateContact(contact);
131    
132                                    user.setContactId(contactId);
133    
134                                    UserLocalServiceUtil.updateUser(user);
135                            }
136    
137                            if (_log.isDebugEnabled()) {
138                                    _log.debug("Contacts verified for users");
139                            }
140    
141                            users = UserLocalServiceUtil.getNoGroups();
142    
143                            if (_log.isDebugEnabled()) {
144                                    _log.debug(
145                                            "Processing " + users.size() + " users with no groups");
146                            }
147    
148                            for (User user : users) {
149                                    if (_log.isDebugEnabled()) {
150                                            _log.debug("Creating group for user " + user.getUserId());
151                                    }
152    
153                                    GroupLocalServiceUtil.addGroup(
154                                            user.getUserId(), GroupConstants.DEFAULT_PARENT_GROUP_ID,
155                                            User.class.getName(), user.getUserId(),
156                                            GroupConstants.DEFAULT_LIVE_GROUP_ID,
157                                            (Map<Locale, String>)null, null, 0, true,
158                                            GroupConstants.DEFAULT_MEMBERSHIP_RESTRICTION,
159                                            StringPool.SLASH + user.getScreenName(), false, true, null);
160                            }
161    
162                            if (_log.isDebugEnabled()) {
163                                    _log.debug("Groups verified for users");
164                            }
165                    }
166            }
167    
168            private static final Log _log = LogFactoryUtil.getLog(VerifyUser.class);
169    
170    }