001    /**
002     * Copyright (c) 2000-2012 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.tools;
016    
017    import com.liferay.portal.dao.orm.common.SQLTransformer;
018    import com.liferay.portal.events.StartupHelperUtil;
019    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
020    import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
021    import com.liferay.portal.kernel.dao.db.DB;
022    import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
023    import com.liferay.portal.kernel.exception.PortalException;
024    import com.liferay.portal.kernel.log.Log;
025    import com.liferay.portal.kernel.log.LogFactoryUtil;
026    import com.liferay.portal.kernel.util.ReleaseInfo;
027    import com.liferay.portal.kernel.util.StringBundler;
028    import com.liferay.portal.kernel.util.Time;
029    import com.liferay.portal.model.Release;
030    import com.liferay.portal.model.ReleaseConstants;
031    import com.liferay.portal.service.ClassNameLocalServiceUtil;
032    import com.liferay.portal.service.ReleaseLocalServiceUtil;
033    import com.liferay.portal.service.ResourceActionLocalServiceUtil;
034    import com.liferay.portal.service.ResourceCodeLocalServiceUtil;
035    import com.liferay.portal.util.InitUtil;
036    import com.liferay.util.dao.orm.CustomSQLUtil;
037    
038    import org.apache.commons.lang.time.StopWatch;
039    
040    /**
041     * @author Michael C. Han
042     * @author Brian Wing Shun Chan
043     */
044    public class DBUpgrader {
045    
046            public static void main(String[] args) {
047                    try {
048                            StopWatch stopWatch = new StopWatch();
049    
050                            stopWatch.start();
051    
052                            InitUtil.initWithSpring();
053    
054                            upgrade();
055                            verify();
056    
057                            System.out.println(
058                                    "\nSuccessfully completed upgrade process in " +
059                                            (stopWatch.getTime() / Time.SECOND) + " seconds.");
060    
061                            System.exit(0);
062                    }
063                    catch (Exception e) {
064                            e.printStackTrace();
065    
066                            System.exit(1);
067                    }
068            }
069    
070            public static void upgrade() throws Exception {
071    
072                    // Disable database caching before upgrade
073    
074                    if (_log.isDebugEnabled()) {
075                            _log.debug("Disable cache registry");
076                    }
077    
078                    CacheRegistryUtil.setActive(false);
079    
080                    // Upgrade
081    
082                    if (_log.isDebugEnabled()) {
083                            _log.debug("Run upgrade process");
084                    }
085    
086                    int buildNumber = ReleaseLocalServiceUtil.getBuildNumberOrCreate();
087    
088                    if (buildNumber > ReleaseInfo.getBuildNumber()) {
089                            StringBundler sb = new StringBundler(6);
090    
091                            sb.append("Attempting to deploy an older Liferay Portal version. ");
092                            sb.append("Current build version is ");
093                            sb.append(buildNumber);
094                            sb.append(" and attempting to deploy version ");
095                            sb.append(ReleaseInfo.getBuildNumber());
096                            sb.append(".");
097    
098                            throw new IllegalStateException(sb.toString());
099                    }
100                    else if (buildNumber < ReleaseInfo.RELEASE_5_0_0_BUILD_NUMBER) {
101                            String msg = "You must first upgrade to Liferay Portal 5.0.0";
102    
103                            System.out.println(msg);
104    
105                            throw new RuntimeException(msg);
106                    }
107    
108                    // Reload SQL
109    
110                    CustomSQLUtil.reloadCustomSQL();
111                    SQLTransformer.reloadSQLTransformer();
112    
113                    // Upgrade build
114    
115                    if (_log.isDebugEnabled()) {
116                            _log.debug("Update build " + buildNumber);
117                    }
118    
119                    StartupHelperUtil.upgradeProcess(buildNumber);
120    
121                    // Update company key
122    
123                    if (StartupHelperUtil.isUpgraded()) {
124                            if (_log.isDebugEnabled()) {
125                                    _log.debug("Update company key");
126                            }
127    
128                            _updateCompanyKey();
129                    }
130    
131                    // Class names
132    
133                    if (_log.isDebugEnabled()) {
134                            _log.debug("Check class names");
135                    }
136    
137                    ClassNameLocalServiceUtil.checkClassNames();
138    
139                    // Resource actions
140    
141                    if (_log.isDebugEnabled()) {
142                            _log.debug("Check resource actions");
143                    }
144    
145                    ResourceActionLocalServiceUtil.checkResourceActions();
146    
147                    // Resource codes
148    
149                    if (_log.isDebugEnabled()) {
150                            _log.debug("Check resource codes");
151                    }
152    
153                    ResourceCodeLocalServiceUtil.checkResourceCodes();
154    
155                    // Delete temporary images
156    
157                    if (_log.isDebugEnabled()) {
158                            _log.debug("Delete temporary images");
159                    }
160    
161                    _deleteTempImages();
162    
163                    // Clear the caches only if the upgrade process was run
164    
165                    if (_log.isDebugEnabled()) {
166                            _log.debug("Clear cache if upgrade process was run");
167                    }
168    
169                    if (StartupHelperUtil.isUpgraded()) {
170                            MultiVMPoolUtil.clear();
171                    }
172            }
173    
174            public static void verify() throws Exception {
175    
176                    // Verify
177    
178                    Release release = null;
179    
180                    try {
181                            release = ReleaseLocalServiceUtil.getRelease(
182                                    ReleaseConstants.DEFAULT_SERVLET_CONTEXT_NAME,
183                                    ReleaseInfo.getBuildNumber());
184                    }
185                    catch (PortalException pe) {
186                            release = ReleaseLocalServiceUtil.addRelease(
187                                    ReleaseConstants.DEFAULT_SERVLET_CONTEXT_NAME,
188                                    ReleaseInfo.getBuildNumber());
189                    }
190    
191                    StartupHelperUtil.verifyProcess(release.isVerified());
192    
193                    // Update indexes
194    
195                    if (StartupHelperUtil.isUpgraded()) {
196                            StartupHelperUtil.updateIndexes();
197                    }
198    
199                    // Update release
200    
201                    boolean verified = StartupHelperUtil.isVerified();
202    
203                    if (release.isVerified()) {
204                            verified = true;
205                    }
206    
207                    ReleaseLocalServiceUtil.updateRelease(
208                            release.getReleaseId(), ReleaseInfo.getBuildNumber(),
209                            ReleaseInfo.getBuildDate(), verified);
210    
211                    // Enable database caching after verify
212    
213                    CacheRegistryUtil.setActive(true);
214            }
215    
216            private static void _deleteTempImages() throws Exception {
217                    DB db = DBFactoryUtil.getDB();
218    
219                    db.runSQL(_DELETE_TEMP_IMAGES_1);
220                    db.runSQL(_DELETE_TEMP_IMAGES_2);
221            }
222    
223            private static void _updateCompanyKey() throws Exception {
224                    DB db = DBFactoryUtil.getDB();
225    
226                    db.runSQL("update Company set key_ = null");
227            }
228    
229            private static final String _DELETE_TEMP_IMAGES_1 =
230                    "delete from Image where imageId IN (SELECT articleImageId FROM " +
231                            "JournalArticleImage where tempImage = TRUE)";
232    
233            private static final String _DELETE_TEMP_IMAGES_2 =
234                    "delete from JournalArticleImage where tempImage = TRUE";
235    
236            private static Log _log = LogFactoryUtil.getLog(DBUpgrader.class);
237    
238    }