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.verify;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.settings.CompanyServiceSettingsLocator;
020    import com.liferay.portal.kernel.settings.ModifiableSettings;
021    import com.liferay.portal.kernel.settings.Settings;
022    import com.liferay.portal.kernel.settings.SettingsDescriptor;
023    import com.liferay.portal.kernel.settings.SettingsException;
024    import com.liferay.portal.kernel.settings.SettingsFactory;
025    import com.liferay.portal.model.Company;
026    import com.liferay.portal.service.CompanyLocalService;
027    
028    import java.io.IOException;
029    
030    import java.util.Dictionary;
031    import java.util.List;
032    import java.util.Set;
033    
034    import javax.portlet.ValidatorException;
035    
036    /**
037     * @author Michael C. Han
038     */
039    public abstract class BaseCompanySettingsVerifyProcess extends VerifyProcess {
040    
041            @Override
042            protected void doVerify() throws Exception {
043                    verifyProperties();
044            }
045    
046            protected abstract CompanyLocalService getCompanyLocalService();
047    
048            protected abstract Set<String> getLegacyPropertyKeys();
049    
050            protected abstract Dictionary<String, String> getPropertyValues(
051                    long companyId);
052    
053            protected abstract SettingsFactory getSettingsFactory();
054    
055            protected abstract String getSettingsId();
056    
057            protected void storeSettings(
058                            long companyId, String settingsId,
059                            Dictionary<String, String> dictionary)
060                    throws IOException, SettingsException, ValidatorException {
061    
062                    SettingsFactory settingsFactory = getSettingsFactory();
063    
064                    Settings settings = settingsFactory.getSettings(
065                            new CompanyServiceSettingsLocator(companyId, settingsId));
066    
067                    ModifiableSettings modifiableSettings =
068                            settings.getModifiableSettings();
069    
070                    SettingsDescriptor settingsDescriptor =
071                            settingsFactory.getSettingsDescriptor(settingsId);
072    
073                    for (String name : settingsDescriptor.getAllKeys()) {
074                            String value = dictionary.get(name);
075    
076                            String oldValue = settings.getValue(name, null);
077    
078                            if (!value.equals(oldValue)) {
079                                    modifiableSettings.setValue(name, value);
080                            }
081                    }
082    
083                    modifiableSettings.store();
084            }
085    
086            protected void verifyProperties() throws Exception {
087                    CompanyLocalService companyLocalService = getCompanyLocalService();
088    
089                    List<Company> companies = companyLocalService.getCompanies(false);
090    
091                    for (Company company : companies) {
092                            Dictionary<String, String> dictionary = getPropertyValues(
093                                    company.getCompanyId());
094    
095                            storeSettings(company.getCompanyId(), getSettingsId(), dictionary);
096    
097                            Set<String> keys = getLegacyPropertyKeys();
098    
099                            if (_log.isInfoEnabled()) {
100                                    _log.info(
101                                            "Removing preference keys " + keys + " for company " +
102                                                    company.getCompanyId());
103                            }
104    
105                            companyLocalService.removePreferences(
106                                    company.getCompanyId(), keys.toArray(new String[keys.size()]));
107                    }
108            }
109    
110            private static final Log _log = LogFactoryUtil.getLog(
111                    BaseCompanySettingsVerifyProcess.class);
112    
113    }