1
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
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
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 }