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.upgrade.v4_3_0;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.upgrade.UpgradeProcess;
20  import com.liferay.portal.kernel.upgrade.util.UpgradeTable;
21  import com.liferay.portal.kernel.upgrade.util.UpgradeTableFactoryUtil;
22  import com.liferay.portal.kernel.upgrade.util.ValueMapper;
23  import com.liferay.portal.kernel.upgrade.util.ValueMapperFactoryUtil;
24  import com.liferay.portal.model.Account;
25  import com.liferay.portal.upgrade.v4_3_0.util.AvailableMappersUtil;
26  import com.liferay.portal.upgrade.v4_3_0.util.CompanyTable;
27  import com.liferay.portal.upgrade.v4_3_0.util.WebIdUtil;
28  import com.liferay.portal.util.PortletKeys;
29  
30  /**
31   * <a href="UpgradeCompany.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Alexander Chow
34   * @author Brian Wing Shun Chan
35   */
36  public class UpgradeCompany extends UpgradeProcess {
37  
38      protected void doUpgrade() throws Exception {
39          ValueMapper companyIdMapper = ValueMapperFactoryUtil.getValueMapper();
40  
41          AvailableMappersUtil.setCompanyIdMapper(companyIdMapper);
42  
43          String[] webIds = WebIdUtil.getWebIds();
44  
45          long[] companyIds = new long[webIds.length];
46  
47          for (int i = 0; i < webIds.length; i++) {
48              String webId = webIds[i];
49  
50              long companyId = upgradeWebId(webId);
51  
52              companyIds[i] = companyId;
53  
54              companyIdMapper.mapValue(webId, new Long(companyId));
55          }
56  
57          UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
58              CompanyTable.TABLE_NAME, CompanyTable.TABLE_COLUMNS);
59  
60          upgradeTable.setCreateSQL(CompanyTable.TABLE_SQL_CREATE);
61  
62          upgradeTable.updateTable();
63  
64          runSQL(
65              "update PortletPreferences set ownerId = '0', ownerType = " +
66                  PortletKeys.PREFS_OWNER_TYPE_COMPANY +
67                      " where ownerId = 'COMPANY.LIFERAY_PORTAL'");
68      }
69  
70      protected String getUpdateSQL(
71          String tableName, long companyId, String webId) {
72  
73          String updateSQL =
74              "update " + tableName + " set companyId = '" + companyId +
75                  "' where companyId = '" + webId + "'";
76  
77          if (_log.isDebugEnabled()) {
78              _log.debug(updateSQL);
79          }
80  
81          return updateSQL;
82      }
83  
84      protected long upgradeWebId(String webId) throws Exception {
85          long companyId = increment();
86  
87          for (int j = 0; j < _TABLES.length; j++) {
88              runSQL(getUpdateSQL(_TABLES[j], companyId, webId));
89          }
90  
91          long accountId = increment();
92  
93          runSQL(
94              "update Account_ set accountId = '" + accountId +
95                  "', companyId = '" + companyId + "' where accountId = '" +
96                      webId + "'");
97  
98          runSQL(
99              "update Address set classPK = '" + accountId +
100                 "' where classNameId = '" + Account.class.getName() +
101                     "' and classPK = '" + webId + "'");
102 
103         ValueMapper imageIdMapper = AvailableMappersUtil.getImageIdMapper();
104 
105         Long logoId = (Long)imageIdMapper.getNewValue(webId);
106 
107         runSQL(
108             "update Company set accountId = " + accountId + ", logoId = " +
109                 logoId.longValue() + " where webId = '" + webId + "'");
110 
111         runSQL(
112             "update Contact_ set companyId = '" + companyId +
113                 "', accountId = '" + accountId + "' where contactId = '" +
114                     webId + ".default'");
115 
116         runSQL(
117             "update Contact_ set accountId = '" + accountId +
118                 "' where accountId = '" + webId + "'");
119 
120         runSQL(
121             "update EmailAddress set classPK = '" + accountId +
122                 "' where classNameId = '" + Account.class.getName() +
123                     "' and classPK = '" + webId + "'");
124 
125         runSQL(
126             "update Phone set classPK = '" + accountId +
127                 "' where classNameId = '" + Account.class.getName() +
128                     "' and classPK = '" + webId + "'");
129 
130         runSQL(
131             "update Resource_ set primKey = '" + companyId +
132                 "' where scope = 'company' and primKey = '" + webId + "'");
133 
134         runSQL(
135             "update User_ set companyId = '" + companyId +
136                 "', defaultUser = TRUE where userId = '" + webId + ".default'");
137 
138         runSQL(
139             "update Website set classPK = '" + accountId +
140                 "' where classNameId = '" + Account.class.getName() +
141                     "' and classPK = '" + webId + "'");
142 
143         return companyId;
144     }
145 
146     private static final String[] _TABLES = new String[] {
147         "Account_", "Address", "BlogsEntry", "BookmarksEntry",
148         "BookmarksFolder", "CalEvent", "Company", "Contact_", "DLFileRank",
149         "DLFileShortcut", "DLFileVersion", "DLFolder", "EmailAddress", "Group_",
150         "IGFolder", "Layout", "LayoutSet", "MBCategory", "Organization_",
151         "Permission_", "Phone", "PollsQuestion", "Portlet", "RatingsEntry",
152         "Resource_", "Role_", "ShoppingCart", "ShoppingCategory",
153         "ShoppingCoupon", "ShoppingItem", "ShoppingOrder", "Subscription",
154         "UserGroup", "User_", "Website", "WikiNode", "WikiPage"
155     };
156 
157     private static Log _log = LogFactoryUtil.getLog(UpgradeCompany.class);
158 
159 }