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