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