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.v6_2_0;
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.portal.kernel.util.Validator;
021    import com.liferay.util.Encryptor;
022    
023    import java.security.Key;
024    
025    import java.sql.PreparedStatement;
026    import java.sql.ResultSet;
027    
028    /**
029     * @author Tomas Polesovsky
030     */
031    public class UpgradeCompany extends UpgradeProcess {
032    
033            @Override
034            protected void doUpgrade() throws Exception {
035                    String keyAlgorithm = Encryptor.KEY_ALGORITHM;
036    
037                    if (keyAlgorithm.equals("DES")) {
038                            return;
039                    }
040    
041                    upgradeKey();
042            }
043    
044            protected void upgradeKey() throws Exception {
045                    try (LoggingTimer loggingTimer = new LoggingTimer();
046                            PreparedStatement ps = connection.prepareStatement(
047                                    "select companyId, key_ from Company");
048                            ResultSet rs = ps.executeQuery()) {
049    
050                            while (rs.next()) {
051                                    long companyId = rs.getLong("companyId");
052                                    String key = rs.getString("key_");
053    
054                                    upgradeKey(companyId, key);
055                            }
056                    }
057            }
058    
059            protected void upgradeKey(long companyId, String key) throws Exception {
060                    Key keyObj = null;
061    
062                    if (Validator.isNotNull(key)) {
063                            keyObj = (Key)Base64.stringToObjectSilent(key);
064                    }
065    
066                    if (keyObj != null) {
067                            String algorithm = keyObj.getAlgorithm();
068    
069                            if (!algorithm.equals("DES")) {
070                                    return;
071                            }
072                    }
073    
074                    try (PreparedStatement ps = connection.prepareStatement(
075                                    "update Company set key_ = ? where companyId = ?")) {
076    
077                            ps.setString(1, Base64.objectToString(Encryptor.generateKey()));
078                            ps.setLong(2, companyId);
079    
080                            ps.executeUpdate();
081                    }
082            }
083    
084    }