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.exception.SystemException;
018    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
019    import com.liferay.portal.kernel.util.Base64;
020    import com.liferay.portal.model.Company;
021    import com.liferay.portal.service.CompanyLocalServiceUtil;
022    import com.liferay.util.Encryptor;
023    import com.liferay.util.EncryptorException;
024    
025    import java.security.Key;
026    
027    import java.util.List;
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                    List<Company> companies = CompanyLocalServiceUtil.getCompanies();
043    
044                    for (Company company : companies) {
045                            upgradeKey(company);
046                    }
047            }
048    
049            protected void upgradeKey(Company company)
050                    throws EncryptorException, SystemException {
051    
052                    Key key = company.getKeyObj();
053    
054                    if (key != null) {
055                            String algorithm = key.getAlgorithm();
056    
057                            if (!algorithm.equals("DES")) {
058                                    return;
059                            }
060                    }
061    
062                    Key newKey = Encryptor.generateKey();
063    
064                    company.setKey(Base64.objectToString(newKey));
065    
066                    CompanyLocalServiceUtil.updateCompany(company);
067            }
068    
069    }