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