001    /**
002     * Copyright (c) 2000-present 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.v7_0_1;
016    
017    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
018    import com.liferay.portal.kernel.util.Base64;
019    import com.liferay.portal.kernel.util.LoggingTimer;
020    import com.liferay.util.Encryptor;
021    
022    import java.security.Key;
023    
024    import java.sql.PreparedStatement;
025    import java.sql.ResultSet;
026    
027    /**
028     * @author Mika Koivisto
029     */
030    public class UpgradeCompany extends UpgradeProcess {
031    
032            @Override
033            protected void doUpgrade() throws Exception {
034                    updateCompanyKey();
035            }
036    
037            protected void updateCompanyKey() throws Exception {
038                    try (LoggingTimer loggingTimer = new LoggingTimer();
039                            PreparedStatement ps = connection.prepareStatement(
040                                    "select companyId, key_ from Company");
041                            ResultSet rs = ps.executeQuery()) {
042    
043                            while (rs.next()) {
044                                    String companyId = rs.getString("companyId");
045                                    String keyString = rs.getString("key_");
046    
047                                    Key key = (Key)Base64.stringToObject(keyString);
048    
049                                    keyString = Encryptor.serializeKey(key);
050    
051                                    runSQL(
052                                            "update Company set key_ = '" + keyString +
053                                                    "' where companyId = " + companyId);
054                            }
055                    }
056            }
057    
058    }