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.kernel.upgrade.util;
016    
017    import aQute.bnd.annotation.ProviderType;
018    
019    import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
020    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.search.IndexWriterHelperUtil;
024    import com.liferay.portal.kernel.upgrade.UpgradeException;
025    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
026    import com.liferay.portal.kernel.util.GetterUtil;
027    import com.liferay.portal.kernel.util.LocaleUtil;
028    import com.liferay.portal.kernel.util.PropsKeys;
029    import com.liferay.portal.kernel.util.PropsUtil;
030    
031    import java.sql.Connection;
032    import java.sql.PreparedStatement;
033    import java.sql.ResultSet;
034    import java.sql.SQLException;
035    
036    import java.util.ArrayList;
037    import java.util.HashMap;
038    import java.util.List;
039    import java.util.Map;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     * @author Alexander Chow
044     * @author Raymond Aug??
045     */
046    @ProviderType
047    public class UpgradeProcessUtil {
048    
049            public static String getDefaultLanguageId(long companyId)
050                    throws SQLException {
051    
052                    String languageId = _languageIds.get(companyId);
053    
054                    if (languageId != null) {
055                            return languageId;
056                    }
057    
058                    try (Connection con = DataAccess.getUpgradeOptimizedConnection();
059                            PreparedStatement ps = con.prepareStatement(
060                                    "select languageId from User_ where companyId = ? and " +
061                                            "defaultUser = ?")) {
062    
063                            ps.setLong(1, companyId);
064                            ps.setBoolean(2, true);
065    
066                            try (ResultSet rs = ps.executeQuery()) {
067                                    if (rs.next()) {
068                                            languageId = rs.getString("languageId");
069    
070                                            _languageIds.put(companyId, languageId);
071    
072                                            return languageId;
073                                    }
074                                    else {
075                                            return LocaleUtil.toLanguageId(LocaleUtil.US);
076                                    }
077                            }
078                    }
079            }
080    
081            public static List<UpgradeProcess> initUpgradeProcesses(
082                    ClassLoader classLoader, String[] upgradeProcessClassNames) {
083    
084                    List<UpgradeProcess> upgradeProcesses = new ArrayList<>();
085    
086                    for (String upgradeProcessClassName : upgradeProcessClassNames) {
087                            if (_log.isDebugEnabled()) {
088                                    _log.debug("Initializing upgrade " + upgradeProcessClassName);
089                            }
090    
091                            UpgradeProcess upgradeProcess = null;
092    
093                            try {
094                                    Class<?> clazz = classLoader.loadClass(upgradeProcessClassName);
095    
096                                    upgradeProcess = (UpgradeProcess)clazz.newInstance();
097                            }
098                            catch (Exception e) {
099                                    _log.error(
100                                            "Unable to initialize upgrade " + upgradeProcessClassName);
101    
102                                    continue;
103                            }
104    
105                            upgradeProcesses.add(upgradeProcess);
106                    }
107    
108                    return upgradeProcesses;
109            }
110    
111            public static boolean isCreateIGImageDocumentType() {
112                    return _createIGImageDocumentType;
113            }
114    
115            public static void setCreateIGImageDocumentType(
116                    boolean createIGImageDocumentType) {
117    
118                    _createIGImageDocumentType = createIGImageDocumentType;
119            }
120    
121            public static boolean upgradeProcess(
122                            int buildNumber, List<UpgradeProcess> upgradeProcesses)
123                    throws UpgradeException {
124    
125                    return upgradeProcess(buildNumber, upgradeProcesses, _INDEX_ON_UPGRADE);
126            }
127    
128            public static boolean upgradeProcess(
129                            int buildNumber, List<UpgradeProcess> upgradeProcesses,
130                            boolean indexOnUpgrade)
131                    throws UpgradeException {
132    
133                    boolean ranUpgradeProcess = false;
134    
135                    boolean tempIndexReadOnly = IndexWriterHelperUtil.isIndexReadOnly();
136    
137                    if (indexOnUpgrade) {
138                            IndexWriterHelperUtil.setIndexReadOnly(true);
139                    }
140    
141                    try {
142                            for (UpgradeProcess upgradeProcess : upgradeProcesses) {
143                                    boolean tempRanUpgradeProcess = _upgradeProcess(
144                                            buildNumber, upgradeProcess);
145    
146                                    if (tempRanUpgradeProcess) {
147                                            ranUpgradeProcess = true;
148                                    }
149                            }
150                    }
151                    finally {
152                            IndexWriterHelperUtil.setIndexReadOnly(tempIndexReadOnly);
153    
154                            if (ranUpgradeProcess) {
155                                    MultiVMPoolUtil.clear();
156                            }
157                    }
158    
159                    return ranUpgradeProcess;
160            }
161    
162            private static boolean _upgradeProcess(
163                            int buildNumber, UpgradeProcess upgradeProcess)
164                    throws UpgradeException {
165    
166                    Class<?> clazz = upgradeProcess.getClass();
167    
168                    if ((upgradeProcess.getThreshold() == 0) ||
169                            (upgradeProcess.getThreshold() > buildNumber)) {
170    
171                            if (_log.isDebugEnabled()) {
172                                    _log.debug("Running upgrade " + clazz.getName());
173                            }
174    
175                            upgradeProcess.upgrade();
176    
177                            if (_log.isDebugEnabled()) {
178                                    _log.debug("Finished upgrade " + clazz.getName());
179                            }
180    
181                            return true;
182                    }
183    
184                    if (_log.isDebugEnabled()) {
185                            _log.debug(
186                                    "Upgrade threshold " + upgradeProcess.getThreshold() +
187                                            " will not trigger upgrade");
188    
189                            _log.debug("Skipping upgrade " + clazz.getName());
190                    }
191    
192                    return false;
193            }
194    
195            private static final boolean _INDEX_ON_UPGRADE = GetterUtil.getBoolean(
196                    PropsUtil.get(PropsKeys.INDEX_ON_UPGRADE));
197    
198            private static final Log _log = LogFactoryUtil.getLog(
199                    UpgradeProcessUtil.class);
200    
201            private static boolean _createIGImageDocumentType;
202            private static final Map<Long, String> _languageIds = new HashMap<>();
203    
204    }