1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.upgrade.v4_3_0.util;
16  
17  import com.liferay.portal.kernel.upgrade.util.TempUpgradeColumnImpl;
18  import com.liferay.portal.kernel.upgrade.util.ValueMapper;
19  import com.liferay.portal.kernel.util.GetterUtil;
20  
21  import java.sql.Types;
22  
23  import java.util.Map;
24  
25  /**
26   * <a href="ClassPKUpgradeColumnImpl.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Brian Wing Shun Chan
29   */
30  public class ClassPKUpgradeColumnImpl extends TempUpgradeColumnImpl {
31  
32      public ClassPKUpgradeColumnImpl(
33          ClassNameIdUpgradeColumnImpl classNameIdColumn,
34          Map<Long, ClassPKContainer> classPKContainers) {
35  
36          super("classPK", new Integer(Types.VARCHAR));
37  
38          _classNameIdColumn = classNameIdColumn;
39          _classPKContainers = classPKContainers;
40      }
41  
42      public Integer getNewColumnType(Integer defaultType) {
43          return new Integer(Types.BIGINT);
44      }
45  
46      public Object getNewValue(Object oldValue) throws Exception {
47          Long classNameId = (Long)_classNameIdColumn.getNewValue();
48  
49          ClassPKContainer classPKContainer = _classPKContainers.get(classNameId);
50  
51          if (classPKContainer != null) {
52              ValueMapper valueMapper = classPKContainer.getValueMapper();
53  
54              if (classPKContainer.isLong()) {
55                  return valueMapper.getNewValue(
56                      new Long(GetterUtil.getLong((String)oldValue)));
57              }
58              else {
59                  return valueMapper.getNewValue(oldValue);
60              }
61          }
62          else {
63              if (oldValue instanceof String) {
64                  return new Long(GetterUtil.getLong((String)oldValue));
65              }
66              else {
67                  return oldValue;
68              }
69          }
70      }
71  
72      private ClassNameIdUpgradeColumnImpl _classNameIdColumn;
73      private Map<Long, ClassPKContainer> _classPKContainers;
74  
75  }