001    /**
002     * Copyright (c) 2000-2011 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.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.security.auth.FullNameGenerator;
021    import com.liferay.portal.security.auth.FullNameGeneratorFactory;
022    
023    import java.sql.Connection;
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                    Connection con = null;
041                    PreparedStatement ps = null;
042                    ResultSet rs = null;
043    
044                    try {
045                            con = DataAccess.getConnection();
046    
047                            StringBundler sb = new StringBundler(11);
048    
049                            sb.append("select distinct User_.companyId, User_.userId, ");
050                            sb.append("User_.firstName, User_.middleName, User_.lastName ");
051                            sb.append("from User_ inner join ");
052                            sb.append(tableName);
053                            sb.append(" on ");
054                            sb.append(tableName);
055                            sb.append(".userId = User_.userId where ");
056                            sb.append(tableName);
057                            sb.append(".userName is null or ");
058                            sb.append(tableName);
059                            sb.append(".userName = ''");
060    
061                            String sql = sb.toString();
062    
063                            ps = con.prepareStatement(sql);
064    
065                            rs = ps.executeQuery();
066    
067                            while (rs.next()) {
068                                    long companyId = rs.getLong("companyId");
069                                    long userId = rs.getLong("userId");
070                                    String firstName = rs.getString("firstName");
071                                    String middleName = rs.getString("middleName");
072                                    String lastName = rs.getString("lastName");
073    
074                                    FullNameGenerator fullNameGenerator =
075                                            FullNameGeneratorFactory.getInstance();
076    
077                                    String fullName = fullNameGenerator.getFullName(
078                                            firstName, middleName, lastName);
079    
080                                    if (setCompanyId) {
081                                            sb = new StringBundler(8);
082    
083                                            sb.append("update ");
084                                            sb.append(tableName);
085                                            sb.append(" set companyId = ");
086                                            sb.append(companyId);
087                                            sb.append(", userName = '");
088                                            sb.append(fullName);
089                                            sb.append("' where userId = ");
090                                            sb.append(userId);
091                                    }
092                                    else {
093                                            sb = new StringBundler(6);
094    
095                                            sb.append("update ");
096                                            sb.append(tableName);
097                                            sb.append(" set userName = '");
098                                            sb.append(fullName);
099                                            sb.append("' where userId = ");
100                                            sb.append(userId);
101                                    }
102    
103                                    runSQL(sb.toString());
104                            }
105                    }
106                    finally {
107                            DataAccess.cleanUp(con, ps, rs);
108                    }
109            }
110    
111    }