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_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("BookmarksEntry", false);
035                    updateTable("BookmarksFolder", false);
036                    updateTable("IGFolder", false);
037                    updateTable("IGImage", false);
038                    updateTable("PollsVote", true);
039            }
040    
041            protected void updateTable(String tableName, boolean setCompanyId)
042                    throws Exception {
043    
044                    Connection con = null;
045                    PreparedStatement ps = null;
046                    ResultSet rs = null;
047    
048                    try {
049                            con = DataAccess.getConnection();
050    
051                            StringBundler sb = new StringBundler(11);
052    
053                            sb.append("select distinct User_.companyId, User_.userId, ");
054                            sb.append("User_.firstName, User_.middleName, User_.lastName ");
055                            sb.append("from User_ inner join ");
056                            sb.append(tableName);
057                            sb.append(" on ");
058                            sb.append(tableName);
059                            sb.append(".userId = User_.userId where ");
060                            sb.append(tableName);
061                            sb.append(".userName is null or ");
062                            sb.append(tableName);
063                            sb.append(".userName = ''");
064    
065                            String sql = sb.toString();
066    
067                            ps = con.prepareStatement(sql);
068    
069                            rs = ps.executeQuery();
070    
071                            while (rs.next()) {
072                                    long companyId = rs.getLong("companyId");
073                                    long userId = rs.getLong("userId");
074                                    String firstName = rs.getString("firstName");
075                                    String middleName = rs.getString("middleName");
076                                    String lastName = rs.getString("lastName");
077    
078                                    FullNameGenerator fullNameGenerator =
079                                            FullNameGeneratorFactory.getInstance();
080    
081                                    String fullName = fullNameGenerator.getFullName(
082                                            firstName, middleName, lastName);
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    }