001    /**
002     * Copyright (c) 2000-2013 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;
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.getUpgradeOptimizedConnection();
048    
049                            StringBundler sb = new StringBundler(11);
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 ");
058                            sb.append(tableName);
059                            sb.append(".userName is null or ");
060                            sb.append(tableName);
061                            sb.append(".userName = ''");
062    
063                            ps = con.prepareStatement(sb.toString());
064    
065                            rs = ps.executeQuery();
066    
067                            while (rs.next()) {
068                                    long userId = rs.getLong("userId");
069                                    String firstName = rs.getString("firstName");
070                                    String middleName = rs.getString("middleName");
071                                    String lastName = rs.getString("lastName");
072    
073                                    FullNameGenerator fullNameGenerator =
074                                            FullNameGeneratorFactory.getInstance();
075    
076                                    String fullName = fullNameGenerator.getFullName(
077                                            firstName, middleName, lastName);
078    
079                                    fullName = fullName.replace(
080                                            StringPool.APOSTROPHE, StringPool.DOUBLE_APOSTROPHE);
081    
082                                    runSQL(
083                                            "update " + tableName + " set userName = '" + fullName +
084                                                    "' where userId = " + userId);
085                            }
086                    }
087                    finally {
088                            DataAccess.cleanUp(con, ps, rs);
089                    }
090            }
091    
092    }