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.upgrade.UpgradeProcess;
018    import com.liferay.portal.kernel.upgrade.util.SwapUpgradeColumnImpl;
019    import com.liferay.portal.kernel.upgrade.util.UpgradeColumn;
020    import com.liferay.portal.kernel.upgrade.util.UpgradeTable;
021    import com.liferay.portal.kernel.upgrade.util.UpgradeTableFactoryUtil;
022    import com.liferay.portal.model.Contact;
023    import com.liferay.portal.model.Organization;
024    import com.liferay.portal.upgrade.util.PKUpgradeColumnImpl;
025    import com.liferay.portal.upgrade.v4_3_0.util.AvailableMappersUtil;
026    import com.liferay.portal.upgrade.v4_3_0.util.ClassNameIdUpgradeColumnImpl;
027    import com.liferay.portal.upgrade.v4_3_0.util.ClassPKContainer;
028    import com.liferay.portal.upgrade.v4_3_0.util.ClassPKUpgradeColumnImpl;
029    import com.liferay.portal.upgrade.v4_3_0.util.EmailAddressTable;
030    import com.liferay.portal.upgrade.v4_3_0.util.ValueMapperUtil;
031    import com.liferay.portal.util.PortalUtil;
032    
033    import java.sql.Types;
034    
035    import java.util.HashMap;
036    import java.util.Map;
037    
038    /**
039     * @author Alexander Chow
040     * @author Brian Wing Shun Chan
041     */
042    public class UpgradeEmailAddress extends UpgradeProcess {
043    
044            protected void doUpgrade() throws Exception {
045    
046                    // EmailAddress
047    
048                    UpgradeColumn upgradeUserIdColumn = new SwapUpgradeColumnImpl(
049                            "userId", new Integer(Types.VARCHAR),
050                            AvailableMappersUtil.getUserIdMapper());
051    
052                    PKUpgradeColumnImpl upgradePKColumn = new PKUpgradeColumnImpl(
053                            "emailAddressId", true);
054    
055                    ClassNameIdUpgradeColumnImpl classNameIdColumn =
056                            new ClassNameIdUpgradeColumnImpl();
057    
058                    Map<Long, ClassPKContainer> classPKContainers =
059                            new HashMap<Long, ClassPKContainer>();
060    
061                    classPKContainers.put(
062                            new Long(PortalUtil.getClassNameId(Contact.class.getName())),
063                            new ClassPKContainer(
064                                    AvailableMappersUtil.getContactIdMapper(), false));
065    
066                    classPKContainers.put(
067                            new Long(PortalUtil.getClassNameId(Organization.class.getName())),
068                            new ClassPKContainer(
069                                    AvailableMappersUtil.getOrganizationIdMapper(), true));
070    
071                    UpgradeColumn upgradeClassPKColumn = new ClassPKUpgradeColumnImpl(
072                            classNameIdColumn, classPKContainers);
073    
074                    UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
075                            EmailAddressTable.TABLE_NAME, EmailAddressTable.TABLE_COLUMNS,
076                            upgradePKColumn, upgradeUserIdColumn, classNameIdColumn,
077                            upgradeClassPKColumn);
078    
079                    upgradeTable.setCreateSQL(EmailAddressTable.TABLE_SQL_CREATE);
080    
081                    upgradeTable.updateTable();
082    
083                    ValueMapperUtil.persist(
084                            upgradePKColumn.getValueMapper(), "email-address-id");
085            }
086    
087    }