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                    Connection con = null;
059                    PreparedStatement ps = null;
060                    ResultSet rs = null;
061    
062                    try {
063                            con = DataAccess.getUpgradeOptimizedConnection();
064    
065                            ps = con.prepareStatement(
066                                    "select languageId from User_ where companyId = ? and " +
067                                            "defaultUser = ?");
068    
069                            ps.setLong(1, companyId);
070                            ps.setBoolean(2, true);
071    
072                            rs = ps.executeQuery();
073    
074                            if (rs.next()) {
075                                    languageId = rs.getString("languageId");
076    
077                                    _languageIds.put(companyId, languageId);
078    
079                                    return languageId;
080                            }
081                            else {
082                                    return LocaleUtil.toLanguageId(LocaleUtil.US);
083                            }
084                    }
085                    finally {
086                            DataAccess.cleanUp(con, ps, rs);
087                    }
088            }
089    
090            public static List<UpgradeProcess> initUpgradeProcesses(
091                    ClassLoader classLoader, String[] upgradeProcessClassNames) {
092    
093                    List<UpgradeProcess> upgradeProcesses = new ArrayList<>();
094    
095                    for (String upgradeProcessClassName : upgradeProcessClassNames) {
096                            if (_log.isDebugEnabled()) {
097                                    _log.debug("Initializing upgrade " + upgradeProcessClassName);
098                            }
099    
100                            UpgradeProcess upgradeProcess = null;
101    
102                            try {
103                                    Class<?> clazz = classLoader.loadClass(upgradeProcessClassName);
104    
105                                    upgradeProcess = (UpgradeProcess)clazz.newInstance();
106                            }
107                            catch (Exception e) {
108                                    _log.error(
109                                            "Unable to initialize upgrade " + upgradeProcessClassName);
110    
111                                    continue;
112                            }
113    
114                            upgradeProcesses.add(upgradeProcess);
115                    }
116    
117                    return upgradeProcesses;
118            }
119    
120            public static boolean isCreateIGImageDocumentType() {
121                    return _createIGImageDocumentType;
122            }
123    
124            public static void setCreateIGImageDocumentType(
125                    boolean createIGImageDocumentType) {
126    
127                    _createIGImageDocumentType = createIGImageDocumentType;
128            }
129    
130            public static boolean upgradeProcess(
131                            int buildNumber, List<UpgradeProcess> upgradeProcesses)
132                    throws UpgradeException {
133    
134                    return upgradeProcess(buildNumber, upgradeProcesses, _INDEX_ON_UPGRADE);
135            }
136    
137            public static boolean upgradeProcess(
138                            int buildNumber, List<UpgradeProcess> upgradeProcesses,
139                            boolean indexOnUpgrade)
140                    throws UpgradeException {
141    
142                    boolean ranUpgradeProcess = false;
143    
144                    boolean tempIndexReadOnly = IndexWriterHelperUtil.isIndexReadOnly();
145    
146                    if (indexOnUpgrade) {
147                            IndexWriterHelperUtil.setIndexReadOnly(true);
148                    }
149    
150                    try {
151                            for (UpgradeProcess upgradeProcess : upgradeProcesses) {
152                                    boolean tempRanUpgradeProcess = _upgradeProcess(
153                                            buildNumber, upgradeProcess);
154    
155                                    if (tempRanUpgradeProcess) {
156                                            ranUpgradeProcess = true;
157                                    }
158                            }
159                    }
160                    finally {
161                            IndexWriterHelperUtil.setIndexReadOnly(tempIndexReadOnly);
162    
163                            if (ranUpgradeProcess) {
164                                    MultiVMPoolUtil.clear();
165                            }
166                    }
167    
168                    return ranUpgradeProcess;
169            }
170    
171            private static boolean _upgradeProcess(
172                            int buildNumber, UpgradeProcess upgradeProcess)
173                    throws UpgradeException {
174    
175                    Class<?> clazz = upgradeProcess.getClass();
176    
177                    if ((upgradeProcess.getThreshold() == 0) ||
178                            (upgradeProcess.getThreshold() > buildNumber)) {
179    
180                            if (_log.isDebugEnabled()) {
181                                    _log.debug("Running upgrade " + clazz.getName());
182                            }
183    
184                            upgradeProcess.upgrade();
185    
186                            if (_log.isDebugEnabled()) {
187                                    _log.debug("Finished upgrade " + clazz.getName());
188                            }
189    
190                            return true;
191                    }
192    
193                    if (_log.isDebugEnabled()) {
194                            _log.debug(
195                                    "Upgrade threshold " + upgradeProcess.getThreshold() +
196                                            " will not trigger upgrade");
197    
198                            _log.debug("Skipping upgrade " + clazz.getName());
199                    }
200    
201                    return false;
202            }
203    
204            private static final boolean _INDEX_ON_UPGRADE = GetterUtil.getBoolean(
205                    PropsUtil.get(PropsKeys.INDEX_ON_UPGRADE));
206    
207            private static final Log _log = LogFactoryUtil.getLog(
208                    UpgradeProcessUtil.class);
209    
210            private static boolean _createIGImageDocumentType = false;
211            private static final Map<Long, String> _languageIds = new HashMap<>();
212    
213    }