1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
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  }