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_2_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.Base64;
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.Connection;
026    import java.sql.PreparedStatement;
027    import java.sql.ResultSet;
028    
029    /**
030     * @author Tomas Polesovsky
031     */
032    public class UpgradeCompany extends UpgradeProcess {
033    
034            @Override
035            protected void doUpgrade() throws Exception {
036                    String keyAlgorithm = Encryptor.KEY_ALGORITHM;
037    
038                    if (keyAlgorithm.equals("DES")) {
039                            return;
040                    }
041    
042                    Connection con = null;
043                    PreparedStatement ps = null;
044                    ResultSet rs = null;
045    
046                    try {
047                            con = DataAccess.getUpgradeOptimizedConnection();
048    
049                            ps = con.prepareStatement("select companyId, key_ from Company");
050    
051                            rs = ps.executeQuery();
052    
053                            while (rs.next()) {
054                                    long companyId = rs.getLong("companyId");
055                                    String key = rs.getString("key_");
056    
057                                    upgradeKey(companyId, key);
058                            }
059                    }
060                    finally {
061                            DataAccess.cleanUp(con, ps, rs);
062                    }
063            }
064    
065            protected void upgradeKey(long companyId, String key) throws Exception {
066                    Key keyObj = null;
067    
068                    if (Validator.isNotNull(key)) {
069                            keyObj = (Key)Base64.stringToObjectSilent(key);
070                    }
071    
072                    if (keyObj != null) {
073                            String algorithm = keyObj.getAlgorithm();
074    
075                            if (!algorithm.equals("DES")) {
076                                    return;
077                            }
078                    }
079    
080                    Connection con = null;
081                    PreparedStatement ps = null;
082                    ResultSet rs = null;
083    
084                    try {
085                            con = DataAccess.getUpgradeOptimizedConnection();
086    
087                            ps = con.prepareStatement(
088                                    "update Company set key_ = ? where companyId = ?");
089    
090                            ps.setString(1, Base64.objectToString(Encryptor.generateKey()));
091                            ps.setLong(2, companyId);
092    
093                            ps.executeUpdate();
094                    }
095                    finally {
096                            DataAccess.cleanUp(con, ps, rs);
097                    }
098            }
099    
100    }