1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.tools;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.events.StartupHelperUtil;
27  import com.liferay.portal.kernel.cache.CacheRegistry;
28  import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
29  import com.liferay.portal.kernel.dao.db.DB;
30  import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
31  import com.liferay.portal.kernel.util.ReleaseInfo;
32  import com.liferay.portal.kernel.util.Time;
33  import com.liferay.portal.model.Release;
34  import com.liferay.portal.model.ReleaseConstants;
35  import com.liferay.portal.security.permission.ResourceActionsUtil;
36  import com.liferay.portal.service.ClassNameLocalServiceUtil;
37  import com.liferay.portal.service.ReleaseLocalServiceUtil;
38  import com.liferay.portal.service.ResourceActionLocalServiceUtil;
39  import com.liferay.portal.service.ResourceCodeLocalServiceUtil;
40  import com.liferay.portal.util.InitUtil;
41  
42  import org.apache.commons.lang.time.StopWatch;
43  
44  /**
45   * <a href="DBUpgrader.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Michael C. Han
48   * @author Brian Wing Shun Chan
49   */
50  public class DBUpgrader {
51  
52      public static void main(String[] args) {
53          try {
54              StopWatch stopWatch = new StopWatch();
55  
56              stopWatch.start();
57  
58              InitUtil.initWithSpring();
59  
60              upgrade();
61              verify();
62  
63              System.out.println(
64                  "\nSuccessfully completed upgrade process in " +
65                      (stopWatch.getTime() / Time.SECOND) + " seconds.");
66  
67              System.exit(0);
68          }
69          catch (Exception e) {
70              e.printStackTrace();
71  
72              System.exit(1);
73          }
74      }
75  
76      public static void upgrade() throws Exception {
77  
78          // Disable database caching before upgrade
79  
80          CacheRegistry.setActive(false);
81  
82          // Upgrade
83  
84          int buildNumber = ReleaseLocalServiceUtil.getBuildNumberOrCreate();
85  
86          if (buildNumber < ReleaseInfo.RELEASE_4_2_1_BUILD_NUMBER) {
87              String msg = "You must first upgrade to Liferay Portal 4.2.1";
88  
89              System.out.println(msg);
90  
91              throw new RuntimeException(msg);
92          }
93  
94          StartupHelperUtil.upgradeProcess(buildNumber);
95  
96          // Class names
97  
98          ClassNameLocalServiceUtil.checkClassNames();
99  
100         // Resource actions
101 
102         ResourceActionsUtil.init();
103 
104         ResourceActionLocalServiceUtil.checkResourceActions();
105 
106         // Resource codes
107 
108         ResourceCodeLocalServiceUtil.checkResourceCodes();
109 
110         // Delete temporary images
111 
112         _deleteTempImages();
113 
114         // Clear the caches only if the upgrade process was run
115 
116         if (StartupHelperUtil.isUpgraded()) {
117             MultiVMPoolUtil.clear();
118         }
119     }
120 
121     public static void verify() throws Exception {
122 
123         // Verify
124 
125         Release release = null;
126 
127         try {
128             release = ReleaseLocalServiceUtil.getRelease(
129                 ReleaseConstants.DEFAULT_SERVLET_CONTEXT_NAME,
130                 ReleaseInfo.getBuildNumber());
131         }
132         catch (PortalException pe) {
133             release = ReleaseLocalServiceUtil.addRelease(
134                 ReleaseConstants.DEFAULT_SERVLET_CONTEXT_NAME,
135                 ReleaseInfo.getBuildNumber());
136         }
137 
138         StartupHelperUtil.verifyProcess(release.isVerified());
139 
140         // Update indexes
141 
142         if (StartupHelperUtil.isUpgraded()) {
143             StartupHelperUtil.updateIndexes();
144         }
145 
146         // Update release
147 
148         boolean verified = StartupHelperUtil.isVerified();
149 
150         if (release.isVerified()) {
151             verified = true;
152         }
153 
154         ReleaseLocalServiceUtil.updateRelease(
155             release.getReleaseId(), ReleaseInfo.getBuildNumber(),
156             ReleaseInfo.getBuildDate(), verified);
157 
158         // Enable database caching after verify
159 
160         CacheRegistry.setActive(true);
161     }
162 
163     private static void _deleteTempImages() throws Exception {
164         DB db = DBFactoryUtil.getDB();
165 
166         db.runSQL(_DELETE_TEMP_IMAGES_1);
167         db.runSQL(_DELETE_TEMP_IMAGES_2);
168     }
169 
170     private static final String _DELETE_TEMP_IMAGES_1 =
171         "delete from Image where imageId IN (SELECT articleImageId FROM " +
172             "JournalArticleImage where tempImage = TRUE)";
173 
174     private static final String _DELETE_TEMP_IMAGES_2 =
175         "delete from JournalArticleImage where tempImage = TRUE";
176 
177 }