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