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.util;
016    
017    import com.liferay.portal.kernel.upgrade.util.TempUpgradeColumnImpl;
018    import com.liferay.portal.kernel.upgrade.util.ValueMapper;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    
021    import java.sql.Types;
022    
023    import java.util.Map;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public class ClassPKUpgradeColumnImpl extends TempUpgradeColumnImpl {
029    
030            public ClassPKUpgradeColumnImpl(
031                    ClassNameIdUpgradeColumnImpl classNameIdColumn,
032                    Map<Long, ClassPKContainer> classPKContainers) {
033    
034                    super("classPK", new Integer(Types.VARCHAR));
035    
036                    _classNameIdColumn = classNameIdColumn;
037                    _classPKContainers = classPKContainers;
038            }
039    
040            public Integer getNewColumnType(Integer defaultType) {
041                    return new Integer(Types.BIGINT);
042            }
043    
044            public Object getNewValue(Object oldValue) throws Exception {
045                    Long classNameId = (Long)_classNameIdColumn.getNewValue();
046    
047                    ClassPKContainer classPKContainer = _classPKContainers.get(classNameId);
048    
049                    if (classPKContainer != null) {
050                            ValueMapper valueMapper = classPKContainer.getValueMapper();
051    
052                            if (classPKContainer.isLong()) {
053                                    return valueMapper.getNewValue(
054                                            new Long(GetterUtil.getLong((String)oldValue)));
055                            }
056                            else {
057                                    return valueMapper.getNewValue(oldValue);
058                            }
059                    }
060                    else {
061                            if (oldValue instanceof String) {
062                                    return new Long(GetterUtil.getLong((String)oldValue));
063                            }
064                            else {
065                                    return oldValue;
066                            }
067                    }
068            }
069    
070            private ClassNameIdUpgradeColumnImpl _classNameIdColumn;
071            private Map<Long, ClassPKContainer> _classPKContainers;
072    
073    }