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