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                            if (value == null) {
077                                    continue;
078                            }
079    
080                            String oldValue = settings.getValue(name, null);
081    
082                            if (!value.equals(oldValue)) {
083                                    modifiableSettings.setValue(name, value);
084                            }
085                    }
086    
087                    modifiableSettings.store();
088            }
089    
090            protected void verifyProperties() throws Exception {
091                    CompanyLocalService companyLocalService = getCompanyLocalService();
092    
093                    List<Company> companies = companyLocalService.getCompanies(false);
094    
095                    for (Company company : companies) {
096                            Dictionary<String, String> dictionary = getPropertyValues(
097                                    company.getCompanyId());
098    
099                            if (!dictionary.isEmpty()) {
100                                    storeSettings(
101                                            company.getCompanyId(), getSettingsId(), dictionary);
102                            }
103    
104                            Set<String> keys = getLegacyPropertyKeys();
105    
106                            if (_log.isInfoEnabled()) {
107                                    _log.info(
108                                            "Removing preference keys " + keys + " for company " +
109                                                    company.getCompanyId());
110                            }
111    
112                            companyLocalService.removePreferences(
113                                    company.getCompanyId(), keys.toArray(new String[keys.size()]));
114                    }
115            }
116    
117            private static final Log _log = LogFactoryUtil.getLog(
118                    BaseCompanySettingsVerifyProcess.class);
119    
120    }