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