001    /**
002     * Copyright (c) 2000-2011 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.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.StringBundler;
027    import com.liferay.portal.kernel.util.Time;
028    import com.liferay.portal.model.Release;
029    import com.liferay.portal.model.ReleaseConstants;
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.getBuildNumber()) {
087                            StringBundler sb = new StringBundler(6);
088    
089                            sb.append("Attempting to deploy an older Liferay Portal version. ");
090                            sb.append("Current build version is ");
091                            sb.append(buildNumber);
092                            sb.append(" and attempting to deploy version ");
093                            sb.append(ReleaseInfo.getBuildNumber());
094                            sb.append(".");
095    
096                            throw new IllegalStateException(sb.toString());
097                    }
098                    else if (buildNumber < ReleaseInfo.RELEASE_5_0_0_BUILD_NUMBER) {
099                            String msg = "You must first upgrade to Liferay Portal 5.0.0";
100    
101                            System.out.println(msg);
102    
103                            throw new RuntimeException(msg);
104                    }
105    
106                    // Upgrade build
107    
108                    if (_log.isDebugEnabled()) {
109                            _log.debug("Update build " + buildNumber);
110                    }
111    
112                    StartupHelperUtil.upgradeProcess(buildNumber);
113    
114                    // Update company key
115    
116                    if (StartupHelperUtil.isUpgraded()) {
117                            if (_log.isDebugEnabled()) {
118                                    _log.debug("Update company key");
119                            }
120    
121                            _updateCompanyKey();
122                    }
123    
124                    // Class names
125    
126                    if (_log.isDebugEnabled()) {
127                            _log.debug("Check class names");
128                    }
129    
130                    ClassNameLocalServiceUtil.checkClassNames();
131    
132                    // Resource actions
133    
134                    if (_log.isDebugEnabled()) {
135                            _log.debug("Check resource actions");
136                    }
137    
138                    ResourceActionLocalServiceUtil.checkResourceActions();
139    
140                    // Resource codes
141    
142                    if (_log.isDebugEnabled()) {
143                            _log.debug("Check resource codes");
144                    }
145    
146                    ResourceCodeLocalServiceUtil.checkResourceCodes();
147    
148                    // Delete temporary images
149    
150                    if (_log.isDebugEnabled()) {
151                            _log.debug("Delete temporary images");
152                    }
153    
154                    _deleteTempImages();
155    
156                    // Clear the caches only if the upgrade process was run
157    
158                    if (_log.isDebugEnabled()) {
159                            _log.debug("Clear cache if upgrade process was run");
160                    }
161    
162                    if (StartupHelperUtil.isUpgraded()) {
163                            MultiVMPoolUtil.clear();
164                    }
165            }
166    
167            public static void verify() throws Exception {
168    
169                    // Verify
170    
171                    Release release = null;
172    
173                    try {
174                            release = ReleaseLocalServiceUtil.getRelease(
175                                    ReleaseConstants.DEFAULT_SERVLET_CONTEXT_NAME,
176                                    ReleaseInfo.getBuildNumber());
177                    }
178                    catch (PortalException pe) {
179                            release = ReleaseLocalServiceUtil.addRelease(
180                                    ReleaseConstants.DEFAULT_SERVLET_CONTEXT_NAME,
181                                    ReleaseInfo.getBuildNumber());
182                    }
183    
184                    StartupHelperUtil.verifyProcess(release.isVerified());
185    
186                    // Update indexes
187    
188                    if (StartupHelperUtil.isUpgraded()) {
189                            StartupHelperUtil.updateIndexes();
190                    }
191    
192                    // Update release
193    
194                    boolean verified = StartupHelperUtil.isVerified();
195    
196                    if (release.isVerified()) {
197                            verified = true;
198                    }
199    
200                    ReleaseLocalServiceUtil.updateRelease(
201                            release.getReleaseId(), ReleaseInfo.getBuildNumber(),
202                            ReleaseInfo.getBuildDate(), verified);
203    
204                    // Enable database caching after verify
205    
206                    CacheRegistryUtil.setActive(true);
207            }
208    
209            private static void _deleteTempImages() throws Exception {
210                    DB db = DBFactoryUtil.getDB();
211    
212                    db.runSQL(_DELETE_TEMP_IMAGES_1);
213                    db.runSQL(_DELETE_TEMP_IMAGES_2);
214            }
215    
216            private static void _updateCompanyKey() throws Exception {
217                    DB db = DBFactoryUtil.getDB();
218    
219                    db.runSQL("update Company set key_ = null");
220            }
221    
222            private static final String _DELETE_TEMP_IMAGES_1 =
223                    "delete from Image where imageId IN (SELECT articleImageId FROM " +
224                            "JournalArticleImage where tempImage = TRUE)";
225    
226            private static final String _DELETE_TEMP_IMAGES_2 =
227                    "delete from JournalArticleImage where tempImage = TRUE";
228    
229            private static Log _log = LogFactoryUtil.getLog(DBUpgrader.class);
230    
231    }