001    /**
002     * Copyright (c) 2000-present 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.util;
016    
017    import com.liferay.portal.kernel.upgrade.util.BaseUpgradeColumnImpl;
018    import com.liferay.portal.kernel.upgrade.util.ValueMapper;
019    import com.liferay.portal.kernel.upgrade.util.ValueMapperFactoryUtil;
020    
021    import java.sql.Types;
022    
023    /**
024     * @author Brian Wing Shun Chan
025     * @author Alexander Chow
026     */
027    public class PKUpgradeColumnImpl extends BaseUpgradeColumnImpl {
028    
029            public PKUpgradeColumnImpl(String name, boolean trackValues) {
030                    this(name, null, trackValues);
031            }
032    
033            public PKUpgradeColumnImpl(
034                    String name, Integer oldColumnType, boolean trackValues) {
035    
036                    super(name, oldColumnType);
037    
038                    _newColumnType = Integer.valueOf(Types.BIGINT);
039                    _trackValues = trackValues;
040    
041                    if (_trackValues) {
042                            _valueMapper = ValueMapperFactoryUtil.getValueMapper();
043                    }
044                    else {
045                            _valueMapper = null;
046                    }
047            }
048    
049            @Override
050            public Integer getNewColumnType(Integer defaultType) {
051                    return _newColumnType;
052            }
053    
054            @Override
055            public Object getNewValue(Object oldValue) throws Exception {
056                    Long newValue = Long.valueOf(increment());
057    
058                    if (_trackValues) {
059                            _valueMapper.mapValue(oldValue, newValue);
060                    }
061    
062                    return newValue;
063            }
064    
065            public ValueMapper getValueMapper() {
066                    return _valueMapper;
067            }
068    
069            public boolean isTrackValues() {
070                    return _trackValues;
071            }
072    
073            private final Integer _newColumnType;
074            private final boolean _trackValues;
075            private final ValueMapper _valueMapper;
076    
077    }