001    /**
002     * Copyright (c) 2000-2013 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            @Override
052            public Object getNewValue(Object oldValue) throws Exception {
053                    Long newValue = new Long(increment());
054    
055                    if (_trackValues) {
056                            _valueMapper.mapValue(oldValue, newValue);
057                    }
058    
059                    return newValue;
060            }
061    
062            public ValueMapper getValueMapper() {
063                    return _valueMapper;
064            }
065    
066            public boolean isTrackValues() {
067                    return _trackValues;
068            }
069    
070            private Integer _newColumnType;
071            private boolean _trackValues;
072            private ValueMapper _valueMapper;
073    
074    }