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_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("BookmarksEntry", false);
036                    updateTable("BookmarksFolder", false);
037                    updateTable("IGFolder", false);
038                    updateTable("IGImage", false);
039                    updateTable("PollsVote", true);
040            }
041    
042            protected void updateTable(String tableName, boolean setCompanyId)
043                    throws Exception {
044    
045                    Connection con = null;
046                    PreparedStatement ps = null;
047                    ResultSet rs = null;
048    
049                    try {
050                            con = DataAccess.getUpgradeOptimizedConnection();
051    
052                            StringBundler sb = new StringBundler(11);
053    
054                            sb.append("select distinct User_.companyId, User_.userId, ");
055                            sb.append("User_.firstName, User_.middleName, User_.lastName ");
056                            sb.append("from User_ inner join ");
057                            sb.append(tableName);
058                            sb.append(" on ");
059                            sb.append(tableName);
060                            sb.append(".userId = User_.userId where ");
061                            sb.append(tableName);
062                            sb.append(".userName is null or ");
063                            sb.append(tableName);
064                            sb.append(".userName = ''");
065    
066                            String sql = sb.toString();
067    
068                            ps = con.prepareStatement(sql);
069    
070                            rs = ps.executeQuery();
071    
072                            while (rs.next()) {
073                                    long companyId = rs.getLong("companyId");
074                                    long userId = rs.getLong("userId");
075                                    String firstName = rs.getString("firstName");
076                                    String middleName = rs.getString("middleName");
077                                    String lastName = rs.getString("lastName");
078    
079                                    FullNameGenerator fullNameGenerator =
080                                            FullNameGeneratorFactory.getInstance();
081    
082                                    String fullName = fullNameGenerator.getFullName(
083                                            firstName, middleName, lastName);
084    
085                                    fullName = fullName.replace(
086                                            StringPool.APOSTROPHE, StringPool.DOUBLE_APOSTROPHE);
087    
088                                    if (setCompanyId) {
089                                            sb = new StringBundler(8);
090    
091                                            sb.append("update ");
092                                            sb.append(tableName);
093                                            sb.append(" set companyId = ");
094                                            sb.append(companyId);
095                                            sb.append(", userName = '");
096                                            sb.append(fullName);
097                                            sb.append("' where userId = ");
098                                            sb.append(userId);
099                                    }
100                                    else {
101                                            sb = new StringBundler(6);
102    
103                                            sb.append("update ");
104                                            sb.append(tableName);
105                                            sb.append(" set userName = '");
106                                            sb.append(fullName);
107                                            sb.append("' where userId = ");
108                                            sb.append(userId);
109                                    }
110    
111                                    runSQL(sb.toString());
112                            }
113                    }
114                    finally {
115                            DataAccess.cleanUp(con, ps, rs);
116                    }
117            }
118    
119    }