001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.upgrade.v4_3_0;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
020    import com.liferay.portal.kernel.upgrade.util.UpgradeTable;
021    import com.liferay.portal.kernel.upgrade.util.UpgradeTableFactoryUtil;
022    import com.liferay.portal.kernel.upgrade.util.ValueMapper;
023    import com.liferay.portal.kernel.upgrade.util.ValueMapperFactoryUtil;
024    import com.liferay.portal.model.Account;
025    import com.liferay.portal.upgrade.v4_3_0.util.AvailableMappersUtil;
026    import com.liferay.portal.upgrade.v4_3_0.util.CompanyTable;
027    import com.liferay.portal.upgrade.v4_3_0.util.WebIdUtil;
028    import com.liferay.portal.util.PortletKeys;
029    
030    /**
031     * @author Alexander Chow
032     * @author Brian Wing Shun Chan
033     */
034    public class UpgradeCompany extends UpgradeProcess {
035    
036            protected void doUpgrade() throws Exception {
037                    ValueMapper companyIdMapper = ValueMapperFactoryUtil.getValueMapper();
038    
039                    AvailableMappersUtil.setCompanyIdMapper(companyIdMapper);
040    
041                    String[] webIds = WebIdUtil.getWebIds();
042    
043                    long[] companyIds = new long[webIds.length];
044    
045                    for (int i = 0; i < webIds.length; i++) {
046                            String webId = webIds[i];
047    
048                            long companyId = upgradeWebId(webId);
049    
050                            companyIds[i] = companyId;
051    
052                            companyIdMapper.mapValue(webId, new Long(companyId));
053                    }
054    
055                    UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
056                            CompanyTable.TABLE_NAME, CompanyTable.TABLE_COLUMNS);
057    
058                    upgradeTable.setCreateSQL(CompanyTable.TABLE_SQL_CREATE);
059    
060                    upgradeTable.updateTable();
061    
062                    runSQL(
063                            "update PortletPreferences set ownerId = '0', ownerType = " +
064                                    PortletKeys.PREFS_OWNER_TYPE_COMPANY +
065                                            " where ownerId = 'COMPANY.LIFERAY_PORTAL'");
066            }
067    
068            protected String getUpdateSQL(
069                    String tableName, long companyId, String webId) {
070    
071                    String updateSQL =
072                            "update " + tableName + " set companyId = '" + companyId +
073                                    "' where companyId = '" + webId + "'";
074    
075                    if (_log.isDebugEnabled()) {
076                            _log.debug(updateSQL);
077                    }
078    
079                    return updateSQL;
080            }
081    
082            protected long upgradeWebId(String webId) throws Exception {
083                    long companyId = increment();
084    
085                    for (int j = 0; j < _TABLES.length; j++) {
086                            runSQL(getUpdateSQL(_TABLES[j], companyId, webId));
087                    }
088    
089                    long accountId = increment();
090    
091                    runSQL(
092                            "update Account_ set accountId = '" + accountId +
093                                    "', companyId = '" + companyId + "' where accountId = '" +
094                                            webId + "'");
095    
096                    runSQL(
097                            "update Address set classPK = '" + accountId +
098                                    "' where classNameId = '" + Account.class.getName() +
099                                            "' and classPK = '" + webId + "'");
100    
101                    ValueMapper imageIdMapper = AvailableMappersUtil.getImageIdMapper();
102    
103                    Long logoId = (Long)imageIdMapper.getNewValue(webId);
104    
105                    runSQL(
106                            "update Company set accountId = " + accountId + ", logoId = " +
107                                    logoId.longValue() + " where webId = '" + webId + "'");
108    
109                    runSQL(
110                            "update Contact_ set companyId = '" + companyId +
111                                    "', accountId = '" + accountId + "' where contactId = '" +
112                                            webId + ".default'");
113    
114                    runSQL(
115                            "update Contact_ set accountId = '" + accountId +
116                                    "' where accountId = '" + webId + "'");
117    
118                    runSQL(
119                            "update EmailAddress set classPK = '" + accountId +
120                                    "' where classNameId = '" + Account.class.getName() +
121                                            "' and classPK = '" + webId + "'");
122    
123                    runSQL(
124                            "update Phone set classPK = '" + accountId +
125                                    "' where classNameId = '" + Account.class.getName() +
126                                            "' and classPK = '" + webId + "'");
127    
128                    runSQL(
129                            "update Resource_ set primKey = '" + companyId +
130                                    "' where scope = 'company' and primKey = '" + webId + "'");
131    
132                    runSQL(
133                            "update User_ set companyId = '" + companyId +
134                                    "', defaultUser = TRUE where userId = '" + webId + ".default'");
135    
136                    runSQL(
137                            "update Website set classPK = '" + accountId +
138                                    "' where classNameId = '" + Account.class.getName() +
139                                            "' and classPK = '" + webId + "'");
140    
141                    return companyId;
142            }
143    
144            private static final String[] _TABLES = new String[] {
145                    "Account_", "Address", "BlogsEntry", "BookmarksEntry",
146                    "BookmarksFolder", "CalEvent", "Company", "Contact_", "DLFileRank",
147                    "DLFileShortcut", "DLFileVersion", "DLFolder", "EmailAddress", "Group_",
148                    "IGFolder", "Layout", "LayoutSet", "MBCategory", "Organization_",
149                    "Permission_", "Phone", "PollsQuestion", "Portlet", "RatingsEntry",
150                    "Resource_", "Role_", "ShoppingCart", "ShoppingCategory",
151                    "ShoppingCoupon", "ShoppingItem", "ShoppingOrder", "Subscription",
152                    "UserGroup", "User_", "Website", "WikiNode", "WikiPage"
153            };
154    
155            private static Log _log = LogFactoryUtil.getLog(UpgradeCompany.class);
156    
157    }