1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.upgrade;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.util.InstancePool;
25  
26  /**
27   * <a href="SmartUpgradeSchema.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Brian Wing Shun Chan
30   *
31   */
32  public abstract class SmartUpgradeSchema extends UpgradeProcess {
33  
34      public void upgrade() throws UpgradeException {
35          String smartUpgradeSchemaImplClassName = this.getClass().getName();
36  
37          if (_alreadyUpgraded) {
38              _log.info(
39                  "Skipping " + smartUpgradeSchemaImplClassName +
40                      " because it has already been executed");
41  
42              return;
43          }
44  
45          _alreadyUpgraded = true;
46  
47          try {
48              upgradeOnce();
49  
50              boolean doUpgrade = false;
51  
52              for (int i = 0; i < _UPGRADE_PROGRESS_CLASSES.length; i++) {
53                  String curUpgradeSchemaClassName =
54                      _UPGRADE_PROGRESS_CLASSES[i].getName();
55  
56                  if (doUpgrade) {
57                      SmartUpgradeSchema upgradeSchema =
58                          (SmartUpgradeSchema)InstancePool.get(
59                              curUpgradeSchemaClassName);
60  
61                      if (!upgradeSchema.isAlreadyUpgraded()) {
62                          _log.info(
63                              "Automatically executing " +
64                                  curUpgradeSchemaClassName);
65  
66                          upgradeSchema.setAlreadyUpgraded(true);
67  
68                          upgradeSchema.upgradeOnce();
69                      }
70                  }
71  
72                  if (smartUpgradeSchemaImplClassName.equals(
73                          curUpgradeSchemaClassName)) {
74  
75                      doUpgrade = true;
76                  }
77              }
78          }
79          catch (Exception e) {
80              throw new UpgradeException(e);
81          }
82      }
83  
84      protected boolean isAlreadyUpgraded() {
85          return _alreadyUpgraded;
86      }
87  
88      protected void setAlreadyUpgraded(boolean alreadyUpgraded) {
89          _alreadyUpgraded = alreadyUpgraded;
90      }
91  
92      protected abstract void upgradeOnce() throws Exception;
93  
94      private static final Class<?>[] _UPGRADE_PROGRESS_CLASSES = new Class[] {
95          com.liferay.portal.upgrade.v4_3_0.UpgradeSchema.class,
96          com.liferay.portal.upgrade.v4_3_1.UpgradeSchema.class,
97          com.liferay.portal.upgrade.v4_3_2.UpgradeSchema.class,
98          com.liferay.portal.upgrade.v4_3_3.UpgradeSchema.class,
99          com.liferay.portal.upgrade.v4_3_4.UpgradeSchema.class,
100         com.liferay.portal.upgrade.v4_4_0.UpgradeSchema.class,
101         com.liferay.portal.upgrade.v5_0_0.UpgradeSchema.class,
102         com.liferay.portal.upgrade.v5_1_0.UpgradeSchema.class,
103         com.liferay.portal.upgrade.v5_1_2.UpgradeSchema.class,
104         com.liferay.portal.upgrade.v5_2_0.UpgradeSchema.class,
105         com.liferay.portal.upgrade.v5_2_1.UpgradeSchema.class,
106         com.liferay.portal.upgrade.v5_2_3.UpgradeSchema.class
107     };
108 
109     private static Log _log = LogFactoryUtil.getLog(SmartUpgradeSchema.class);
110 
111     private boolean _alreadyUpgraded;
112 
113 }