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.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.model.Contact;
25  import com.liferay.portal.model.Organization;
26  import com.liferay.portal.model.impl.AddressImpl;
27  import com.liferay.portal.upgrade.UpgradeException;
28  import com.liferay.portal.upgrade.UpgradeProcess;
29  import com.liferay.portal.upgrade.util.DefaultUpgradeTableImpl;
30  import com.liferay.portal.upgrade.util.PKUpgradeColumnImpl;
31  import com.liferay.portal.upgrade.util.SwapUpgradeColumnImpl;
32  import com.liferay.portal.upgrade.util.UpgradeColumn;
33  import com.liferay.portal.upgrade.util.UpgradeTable;
34  import com.liferay.portal.upgrade.v4_3_0.util.AvailableMappersUtil;
35  import com.liferay.portal.upgrade.v4_3_0.util.ClassNameIdUpgradeColumnImpl;
36  import com.liferay.portal.upgrade.v4_3_0.util.ClassPKContainer;
37  import com.liferay.portal.upgrade.v4_3_0.util.ClassPKUpgradeColumnImpl;
38  import com.liferay.portal.upgrade.v4_3_0.util.ValueMapperUtil;
39  import com.liferay.portal.util.PortalUtil;
40  
41  import java.sql.Types;
42  
43  import java.util.HashMap;
44  import java.util.Map;
45  
46  /**
47   * <a href="UpgradeAddress.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Alexander Chow
50   * @author Brian Wing Shun Chan
51   *
52   */
53  public class UpgradeAddress extends UpgradeProcess {
54  
55      public void upgrade() throws UpgradeException {
56          _log.info("Upgrading");
57  
58          try {
59              doUpgrade();
60          }
61          catch (Exception e) {
62              throw new UpgradeException(e);
63          }
64      }
65  
66      protected void doUpgrade() throws Exception {
67  
68          // Address
69  
70          UpgradeColumn upgradeUserIdColumn = new SwapUpgradeColumnImpl(
71              "userId", new Integer(Types.VARCHAR),
72              AvailableMappersUtil.getUserIdMapper());
73  
74          PKUpgradeColumnImpl upgradePKColumn = new PKUpgradeColumnImpl(
75              "addressId", true);
76  
77          ClassNameIdUpgradeColumnImpl classNameIdColumn =
78              new ClassNameIdUpgradeColumnImpl();
79  
80          Map<Long, ClassPKContainer> classPKContainers =
81              new HashMap<Long, ClassPKContainer>();
82  
83          classPKContainers.put(
84              new Long(PortalUtil.getClassNameId(Contact.class.getName())),
85              new ClassPKContainer(
86                  AvailableMappersUtil.getContactIdMapper(), false));
87  
88          classPKContainers.put(
89              new Long(PortalUtil.getClassNameId(Organization.class.getName())),
90              new ClassPKContainer(
91                  AvailableMappersUtil.getOrganizationIdMapper(), true));
92  
93          UpgradeColumn upgradeClassPKColumn = new ClassPKUpgradeColumnImpl(
94              classNameIdColumn, classPKContainers);
95  
96          UpgradeTable upgradeTable = new DefaultUpgradeTableImpl(
97              AddressImpl.TABLE_NAME, AddressImpl.TABLE_COLUMNS, upgradePKColumn,
98              upgradeUserIdColumn, classNameIdColumn, upgradeClassPKColumn);
99  
100         upgradeTable.setCreateSQL(AddressImpl.TABLE_SQL_CREATE);
101 
102         upgradeTable.updateTable();
103 
104         ValueMapperUtil.persist(upgradePKColumn.getValueMapper(), "address-id");
105     }
106 
107     private static Log _log = LogFactoryUtil.getLog(UpgradeAddress.class);
108 
109 }