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