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