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