001    /**
002     * Copyright (c) 2000-2012 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 = new Integer(Types.BIGINT);
039                    _trackValues = trackValues;
040    
041                    if (_trackValues) {
042                            _valueMapper = ValueMapperFactoryUtil.getValueMapper();
043                    }
044            }
045    
046            @Override
047            public Integer getNewColumnType(Integer defaultType) {
048                    return _newColumnType;
049            }
050    
051            public Object getNewValue(Object oldValue) throws Exception {
052                    Long newValue = new Long(increment());
053    
054                    if (_trackValues) {
055                            _valueMapper.mapValue(oldValue, newValue);
056                    }
057    
058                    return newValue;
059            }
060    
061            public ValueMapper getValueMapper() {
062                    return _valueMapper;
063            }
064    
065            public boolean isTrackValues() {
066                    return _trackValues;
067            }
068    
069            private Integer _newColumnType;
070            private boolean _trackValues;
071            private ValueMapper _valueMapper;
072    
073    }