package org.activiti.engine.impl.db;

import liquibase.database.Database;
import liquibase.database.core.OracleDatabase;
import liquibase.database.structure.Column;
import liquibase.database.structure.type.BigIntType;
import liquibase.database.structure.type.CustomType;
import liquibase.database.structure.type.DataType;
import liquibase.database.structure.type.DateTimeType;
import liquibase.database.structure.type.DoubleType;
import liquibase.database.structure.type.FloatType;
import liquibase.database.structure.type.IntType;
import liquibase.database.structure.type.SmallIntType;
import liquibase.database.typeconversion.core.OracleTypeConverter;

public class ActivitiOracleTypeConverter extends OracleTypeConverter {

  @Override
  public int getPriority() {
    return super.getPriority() + 1;
  }

  @Override
  public boolean supports(final Database database) {
    return database instanceof OracleDatabase;
  }

  @Override
  protected DataType getDataType(final String columnTypeString,
      final Boolean autoIncrement, final String dataTypeName,
      final String precision, final String additionalInformation) {

    DataType dataType = super.getDataType(columnTypeString, autoIncrement,
        dataTypeName, precision, additionalInformation);
    if (dataType instanceof CustomType
        && columnTypeString.toUpperCase().equals("REAL")) {
      dataType = getFloatType();
    }
    return dataType;
  }

  // See http://liquibase.org/forum/index.php?topic=715.0
  @Override
  public String convertToDatabaseTypeString(final Column referenceColumn, final Database database) {
    String translatedTypeName = referenceColumn.getTypeName();
    if ("NVARCHAR2".equals(translatedTypeName)) {
      int columnSize = referenceColumn.getColumnSize();
      if (columnSize > 2000) {
        columnSize = 2000;
      }
      translatedTypeName = translatedTypeName + "(" + columnSize + ")";
    } else if ("BINARY_FLOAT".equals(translatedTypeName)
        || "BINARY_DOUBLE".equals(translatedTypeName)) {
      // nothing to do
    } else {
      translatedTypeName = super.convertToDatabaseTypeString(referenceColumn,
          database);
    }
    return translatedTypeName;
  }

  @Override
  public SmallIntType getSmallIntType() {
    return new SmallIntType("NUMBER(5)");
  }

  @Override
  public DateTimeType getDateTimeType() {
    return new DateTimeType("TIMESTAMP(9)");
  }

  @Override
  public IntType getIntType() {
    return new IntType("NUMBER(10)");
  }

  @Override
  public BigIntType getBigIntType() {
    return new BigIntType("NUMBER(19)");
  }

  @Override
  public DoubleType getDoubleType() {
    return new DoubleType("BINARY_DOUBLE");
  }

  @Override
  public FloatType getFloatType() {
    return new FloatType("BINARY_FLOAT");
  }
}

@@ -101,22 +101,18 @@
 se.core.
-Oracle
+H2
 Database
@@ -118,53 +118,8 @@
 e;%0D%0A
-import liquibase.database.structure.Column;%0D%0A
 impo
@@ -480,32 +480,88 @@
 .type.IntType;%0D%0A
+import liquibase.database.structure.type.NVarcharType;%0D%0A
 import liquibase
@@ -738,24 +738,26 @@
 onverter %7B%0D%0A
+  
 %0D%0A  @Overrid
@@ -931,22 +931,18 @@
 tanceof 
-Oracle
+H2
 Database
@@ -1329,554 +1329,174 @@
 eof 
-CustomType%0D%0A        && columnTypeString.toUpperCase().equals(%22REAL%22)) %7B%0D%0A      dataType = getFloatType();
+NVarcharType) %7B%0D%0A      try %7B
 %0D%0A    
-%7D%0D%0A
     
-return dataType;%0D%0A  %7D%0D%0A%0D%0A  // See http://liquibase.org/forum/index.php?topic=715.0%0D%0A  @Override%0D%0A  public String convertToDatabaseTypeString(final Column referenceColumn, final Database database) %7B%0D%0A    String translatedTypeName = referenceColumn.getTypeName();%0D%0A    if (%22NVARCHAR2%22.equals(translatedTypeName)) %7B%0D%0A      int columnSize = referenceColumn.getColumnSize();%0D%0A      if (columnSize %3E 2000) %7B%0D%0A        columnSize = 
+int intPrecision = Integer.valueOf(precision);%0D%0A        if (intPrecision %3E 2000) %7B%0D%0A          dataType.setFirstParameter(%22
 2000
+%22)
 ;%0D%0A 
@@ -1496,24 +1496,26 @@
 0%22);%0D%0A      
+  
 %7D%0D%0A      tra
@@ -1515,326 +1515,162 @@
     
-translatedTypeName = translatedTypeName + %22(%22 + columnSize + %22)%22;%0D%0A    %7D else if (%22BINARY_FLOAT%22.equals(translatedTypeName)%0D%0A        %7C%7C %22BINARY_DOUBLE%22.equals(translatedTypeName)) %7B%0D%0A      // nothing to do%0D%0A    %7D else %7B%0D%0A      translatedTypeName = super.convertToDatabaseTypeString(referenceColumn,%0D%0A          database
+%7D catch(Exception e) %7B%7D%0D%0A    %7D else if (dataType instanceof CustomType && columnTypeString.toUpperCase().equals(%22REAL%22)) %7B%0D%0A      dataType = getFloatType(
 );%0D%0A
@@ -1691,25 +1691,15 @@
 urn 
-translatedTypeNam
+dataTyp
 e;%0D%0A

Fix for Oracle NVARCHAR2 fields bigger than 2000