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