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