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