1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.tools;
16  
17  import com.liferay.portal.events.StartupHelperUtil;
18  import com.liferay.portal.kernel.cache.CacheRegistry;
19  import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
20  import com.liferay.portal.kernel.dao.db.DB;
21  import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
22  import com.liferay.portal.kernel.exception.PortalException;
23  import com.liferay.portal.kernel.util.ReleaseInfo;
24  import com.liferay.portal.kernel.util.Time;
25  import com.liferay.portal.model.Release;
26  import com.liferay.portal.model.ReleaseConstants;
27  import com.liferay.portal.security.permission.ResourceActionsUtil;
28  import com.liferay.portal.service.ClassNameLocalServiceUtil;
29  import com.liferay.portal.service.ReleaseLocalServiceUtil;
30  import com.liferay.portal.service.ResourceActionLocalServiceUtil;
31  import com.liferay.portal.service.ResourceCodeLocalServiceUtil;
32  import com.liferay.portal.util.InitUtil;
33  
34  import org.apache.commons.lang.time.StopWatch;
35  
36  /**
37   * <a href="DBUpgrader.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Michael C. Han
40   * @author Brian Wing Shun Chan
41   */
42  public class DBUpgrader {
43  
44      public static void main(String[] args) {
45          try {
46              StopWatch stopWatch = new StopWatch();
47  
48              stopWatch.start();
49  
50              InitUtil.initWithSpring();
51  
52              upgrade();
53              verify();
54  
55              System.out.println(
56                  "\nSuccessfully completed upgrade process in " +
57                      (stopWatch.getTime() / Time.SECOND) + " seconds.");
58  
59              System.exit(0);
60          }
61          catch (Exception e) {
62              e.printStackTrace();
63  
64              System.exit(1);
65          }
66      }
67  
68      public static void upgrade() throws Exception {
69  
70          // Disable database caching before upgrade
71  
72          CacheRegistry.setActive(false);
73  
74          // Upgrade
75  
76          int buildNumber = ReleaseLocalServiceUtil.getBuildNumberOrCreate();
77  
78          if (buildNumber < ReleaseInfo.RELEASE_4_2_1_BUILD_NUMBER) {
79              String msg = "You must first upgrade to Liferay Portal 4.2.1";
80  
81              System.out.println(msg);
82  
83              throw new RuntimeException(msg);
84          }
85  
86          StartupHelperUtil.upgradeProcess(buildNumber);
87  
88          // Class names
89  
90          ClassNameLocalServiceUtil.checkClassNames();
91  
92          // Resource actions
93  
94          ResourceActionsUtil.init();
95  
96          ResourceActionLocalServiceUtil.checkResourceActions();
97  
98          // Resource codes
99  
100         ResourceCodeLocalServiceUtil.checkResourceCodes();
101 
102         // Delete temporary images
103 
104         _deleteTempImages();
105 
106         // Clear the caches only if the upgrade process was run
107 
108         if (StartupHelperUtil.isUpgraded()) {
109             MultiVMPoolUtil.clear();
110         }
111     }
112 
113     public static void verify() throws Exception {
114 
115         // Verify
116 
117         Release release = null;
118 
119         try {
120             release = ReleaseLocalServiceUtil.getRelease(
121                 ReleaseConstants.DEFAULT_SERVLET_CONTEXT_NAME,
122                 ReleaseInfo.getBuildNumber());
123         }
124         catch (PortalException pe) {
125             release = ReleaseLocalServiceUtil.addRelease(
126                 ReleaseConstants.DEFAULT_SERVLET_CONTEXT_NAME,
127                 ReleaseInfo.getBuildNumber());
128         }
129 
130         StartupHelperUtil.verifyProcess(release.isVerified());
131 
132         // Update indexes
133 
134         if (StartupHelperUtil.isUpgraded()) {
135             StartupHelperUtil.updateIndexes();
136         }
137 
138         // Update release
139 
140         boolean verified = StartupHelperUtil.isVerified();
141 
142         if (release.isVerified()) {
143             verified = true;
144         }
145 
146         ReleaseLocalServiceUtil.updateRelease(
147             release.getReleaseId(), ReleaseInfo.getBuildNumber(),
148             ReleaseInfo.getBuildDate(), verified);
149 
150         // Enable database caching after verify
151 
152         CacheRegistry.setActive(true);
153     }
154 
155     private static void _deleteTempImages() throws Exception {
156         DB db = DBFactoryUtil.getDB();
157 
158         db.runSQL(_DELETE_TEMP_IMAGES_1);
159         db.runSQL(_DELETE_TEMP_IMAGES_2);
160     }
161 
162     private static final String _DELETE_TEMP_IMAGES_1 =
163         "delete from Image where imageId IN (SELECT articleImageId FROM " +
164             "JournalArticleImage where tempImage = TRUE)";
165 
166     private static final String _DELETE_TEMP_IMAGES_2 =
167         "delete from JournalArticleImage where tempImage = TRUE";
168 
169 }