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.upgrade.v6_0_12_to_6_1_0;
016    
017    import com.liferay.portal.kernel.security.auth.FullNameGenerator;
018    import com.liferay.portal.kernel.security.auth.FullNameGeneratorFactory;
019    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
020    import com.liferay.portal.kernel.util.LoggingTimer;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.StringPool;
023    
024    import java.sql.PreparedStatement;
025    import java.sql.ResultSet;
026    
027    /**
028     * @author Hugo Huijser
029     */
030    public class UpgradeUserName extends UpgradeProcess {
031    
032            @Override
033            protected void doUpgrade() throws Exception {
034                    updateTable("PollsVote", true);
035            }
036    
037            protected void updateTable(String tableName, boolean setCompanyId)
038                    throws Exception {
039    
040                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
041                            StringBundler sb = new StringBundler(11);
042    
043                            sb.append("select distinct User_.companyId, User_.userId, ");
044                            sb.append("User_.firstName, User_.middleName, User_.lastName ");
045                            sb.append("from User_ inner join ");
046                            sb.append(tableName);
047                            sb.append(" on ");
048                            sb.append(tableName);
049                            sb.append(".userId = User_.userId where ");
050                            sb.append(tableName);
051                            sb.append(".userName is null or ");
052                            sb.append(tableName);
053                            sb.append(".userName = ''");
054    
055                            String sql = sb.toString();
056    
057                            try (PreparedStatement ps = connection.prepareStatement(sql);
058                                    ResultSet rs = ps.executeQuery()) {
059    
060                                    while (rs.next()) {
061                                            long companyId = rs.getLong("companyId");
062                                            long userId = rs.getLong("userId");
063                                            String firstName = rs.getString("firstName");
064                                            String middleName = rs.getString("middleName");
065                                            String lastName = rs.getString("lastName");
066    
067                                            FullNameGenerator fullNameGenerator =
068                                                    FullNameGeneratorFactory.getInstance();
069    
070                                            String fullName = fullNameGenerator.getFullName(
071                                                    firstName, middleName, lastName);
072    
073                                            fullName = fullName.replace(
074                                                    StringPool.APOSTROPHE, StringPool.DOUBLE_APOSTROPHE);
075    
076                                            if (setCompanyId) {
077                                                    sb = new StringBundler(8);
078    
079                                                    sb.append("update ");
080                                                    sb.append(tableName);
081                                                    sb.append(" set companyId = ");
082                                                    sb.append(companyId);
083                                                    sb.append(", userName = '");
084                                                    sb.append(fullName);
085                                                    sb.append("' where userId = ");
086                                                    sb.append(userId);
087                                            }
088                                            else {
089                                                    sb = new StringBundler(6);
090    
091                                                    sb.append("update ");
092                                                    sb.append(tableName);
093                                                    sb.append(" set userName = '");
094                                                    sb.append(fullName);
095                                                    sb.append("' where userId = ");
096                                                    sb.append(userId);
097                                            }
098    
099                                            runSQL(sb.toString());
100                                    }
101                            }
102                    }
103            }
104    
105    }