1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.tools;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.events.StartupHelperUtil;
19  import com.liferay.portal.kernel.cache.CacheRegistry;
20  import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
21  import com.liferay.portal.kernel.dao.db.DB;
22  import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
23  import com.liferay.portal.kernel.log.Log;
24  import com.liferay.portal.kernel.log.LogFactoryUtil;
25  import com.liferay.portal.kernel.util.ReleaseInfo;
26  import com.liferay.portal.kernel.util.Time;
27  import com.liferay.portal.model.Release;
28  import com.liferay.portal.model.ReleaseConstants;
29  import com.liferay.portal.security.permission.ResourceActionsUtil;
30  import com.liferay.portal.service.ClassNameLocalServiceUtil;
31  import com.liferay.portal.service.ReleaseLocalServiceUtil;
32  import com.liferay.portal.service.ResourceActionLocalServiceUtil;
33  import com.liferay.portal.service.ResourceCodeLocalServiceUtil;
34  import com.liferay.portal.util.InitUtil;
35  
36  import org.apache.commons.lang.time.StopWatch;
37  
38  /**
39   * <a href="DBUpgrader.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Michael C. Han
42   * @author Brian Wing Shun Chan
43   */
44  public class DBUpgrader {
45  
46      public static void main(String[] args) {
47          try {
48              StopWatch stopWatch = new StopWatch();
49  
50              stopWatch.start();
51  
52              InitUtil.initWithSpring();
53  
54              upgrade();
55              verify();
56  
57              System.out.println(
58                  "\nSuccessfully completed upgrade process in " +
59                      (stopWatch.getTime() / Time.SECOND) + " seconds.");
60  
61              System.exit(0);
62          }
63          catch (Exception e) {
64              e.printStackTrace();
65  
66              System.exit(1);
67          }
68      }
69  
70      public static void upgrade() throws Exception {
71  
72          // Disable database caching before upgrade
73  
74          if (_log.isDebugEnabled()) {
75              _log.debug("Disable cache registry");
76          }
77  
78          CacheRegistry.setActive(false);
79  
80          // Upgrade
81  
82          if (_log.isDebugEnabled()) {
83              _log.debug("Run upgrade process");
84          }
85  
86          int buildNumber = ReleaseLocalServiceUtil.getBuildNumberOrCreate();
87  
88          if (buildNumber < ReleaseInfo.RELEASE_4_2_1_BUILD_NUMBER) {
89              String msg = "You must first upgrade to Liferay Portal 4.2.1";
90  
91              System.out.println(msg);
92  
93              throw new RuntimeException(msg);
94          }
95  
96          StartupHelperUtil.upgradeProcess(buildNumber);
97  
98          // Class names
99  
100         if (_log.isDebugEnabled()) {
101             _log.debug("Check class names");
102         }
103 
104         ClassNameLocalServiceUtil.checkClassNames();
105 
106         // Resource actions
107 
108         if (_log.isDebugEnabled()) {
109             _log.debug("Check resource actions");
110         }
111 
112         ResourceActionsUtil.init();
113 
114         ResourceActionLocalServiceUtil.checkResourceActions();
115 
116         // Resource codes
117 
118         if (_log.isDebugEnabled()) {
119             _log.debug("Check resource codes");
120         }
121 
122         ResourceCodeLocalServiceUtil.checkResourceCodes();
123 
124         // Delete temporary images
125 
126         if (_log.isDebugEnabled()) {
127             _log.debug("Delete temporary images");
128         }
129 
130         _deleteTempImages();
131 
132         // Clear the caches only if the upgrade process was run
133 
134         if (_log.isDebugEnabled()) {
135             _log.debug("Clear cache if upgrade process was run");
136         }
137 
138         if (StartupHelperUtil.isUpgraded()) {
139             MultiVMPoolUtil.clear();
140         }
141     }
142 
143     public static void verify() throws Exception {
144 
145         // Verify
146 
147         Release release = null;
148 
149         try {
150             release = ReleaseLocalServiceUtil.getRelease(
151                 ReleaseConstants.DEFAULT_SERVLET_CONTEXT_NAME,
152                 ReleaseInfo.getBuildNumber());
153         }
154         catch (PortalException pe) {
155             release = ReleaseLocalServiceUtil.addRelease(
156                 ReleaseConstants.DEFAULT_SERVLET_CONTEXT_NAME,
157                 ReleaseInfo.getBuildNumber());
158         }
159 
160         StartupHelperUtil.verifyProcess(release.isVerified());
161 
162         // Update indexes
163 
164         if (StartupHelperUtil.isUpgraded()) {
165             StartupHelperUtil.updateIndexes();
166         }
167 
168         // Update release
169 
170         boolean verified = StartupHelperUtil.isVerified();
171 
172         if (release.isVerified()) {
173             verified = true;
174         }
175 
176         ReleaseLocalServiceUtil.updateRelease(
177             release.getReleaseId(), ReleaseInfo.getBuildNumber(),
178             ReleaseInfo.getBuildDate(), verified);
179 
180         // Enable database caching after verify
181 
182         CacheRegistry.setActive(true);
183     }
184 
185     private static void _deleteTempImages() throws Exception {
186         DB db = DBFactoryUtil.getDB();
187 
188         db.runSQL(_DELETE_TEMP_IMAGES_1);
189         db.runSQL(_DELETE_TEMP_IMAGES_2);
190     }
191 
192     private static final String _DELETE_TEMP_IMAGES_1 =
193         "delete from Image where imageId IN (SELECT articleImageId FROM " +
194             "JournalArticleImage where tempImage = TRUE)";
195 
196     private static final String _DELETE_TEMP_IMAGES_2 =
197         "delete from JournalArticleImage where tempImage = TRUE";
198 
199     private static Log _log = LogFactoryUtil.getLog(DBUpgrader.class);
200 
201 }