001    /**
002     * Copyright (c) 2000-2012 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;
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("BookmarksEntry");
036                    updateTable("BookmarksFolder");
037                    updateTable("IGFolder");
038                    updateTable("IGImage");
039            }
040    
041            protected void updateTable(String tableName) throws Exception {
042                    Connection con = null;
043                    PreparedStatement ps = null;
044                    ResultSet rs = null;
045    
046                    try {
047                            con = DataAccess.getConnection();
048    
049                            StringBundler sb = new StringBundler(8);
050    
051                            sb.append("select distinct User_.userId, User_.firstName, ");
052                            sb.append("User_.middleName, User_.lastName from User_ ");
053                            sb.append("inner join ");
054                            sb.append(tableName);
055                            sb.append(" on ");
056                            sb.append(tableName);
057                            sb.append(".userId = User_.userId where " + tableName);
058                            sb.append(".userName is null or " + tableName + ".userName = ''");
059    
060                            ps = con.prepareStatement(sb.toString());
061    
062                            rs = ps.executeQuery();
063    
064                            while (rs.next()) {
065                                    long userId = rs.getLong("userId");
066                                    String firstName = rs.getString("firstName");
067                                    String middleName = rs.getString("middleName");
068                                    String lastName = rs.getString("lastName");
069    
070                                    FullNameGenerator fullNameGenerator =
071                                            FullNameGeneratorFactory.getInstance();
072    
073                                    String fullName = fullNameGenerator.getFullName(
074                                            firstName, middleName, lastName);
075    
076                                    fullName = fullName.replace(
077                                            StringPool.APOSTROPHE, StringPool.DOUBLE_APOSTROPHE);
078    
079                                    runSQL(
080                                            "update " + tableName + " set userName = '" + fullName +
081                                                    "' where userId = " + userId);
082                            }
083                    }
084                    finally {
085                            DataAccess.cleanUp(con, ps, rs);
086                    }
087            }
088    
089    }