package com.utopiaindustries.util;

import org.springframework.jdbc.support.KeyHolder;

import java.math.BigInteger;
import java.util.Objects;

public class KeyHolderFunctions {
    public static long getKey( long entityId, KeyHolder keyHolder ) {
        if (keyHolder.getKeyList().isEmpty()) {
            return entityId;
        } else if ( keyHolder.getKeyList().size() == 1 ) {
            return Objects.requireNonNull(keyHolder.getKey()).longValue();
        } else {
            BigInteger key = ( BigInteger ) keyHolder.getKeyList().get( 0 ).get( "GENERATED_KEY" );
            return key.longValue();
        }
    }
}