/*
 * Copyright (c) 2010-2017 Evolveum
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.evolveum.midpoint.repo.sql.util;

import org.hibernate.boot.Metadata;
import org.hibernate.dialect.Oracle12cDialect;
import org.hibernate.dialect.unique.UniqueDelegate;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.Index;
import org.hibernate.mapping.Table;
import org.hibernate.mapping.UniqueKey;
import org.hibernate.tool.schema.spi.Exporter;

/**
 * Created by Viliam Repan (lazyman).
 */
public class MidPointOracleDialect extends Oracle12cDialect {

    private static final String INITRANS = " initrans 30";

    @Override
    public String getTableTypeString() {
        return super.getTableTypeString() + INITRANS;
    }

    @Override
    public Exporter<Index> getIndexExporter() {
        Exporter<Index> exporter = super.getIndexExporter();

        return new Exporter<Index>() {

            @Override
            public String[] getSqlCreateStrings(Index exportable, Metadata metadata) {
                String[] data = exporter.getSqlCreateStrings(exportable, metadata);
                String[] transformed = new String[data.length];

                for (int i = 0; i < data.length; i++) {
                    transformed[i] = data[i] + INITRANS;
                }

                return transformed;
            }

            @Override
            public String[] getSqlDropStrings(Index exportable, Metadata metadata) {
                return exporter.getSqlDropStrings(exportable, metadata);
            }
        };
    }

    @Override
    public UniqueDelegate getUniqueDelegate() {
        UniqueDelegate delegate = super.getUniqueDelegate();

        return new UniqueDelegate() {

            @Override
            public String getColumnDefinitionUniquenessFragment(Column column) {
                return delegate.getColumnDefinitionUniquenessFragment(column);
            }

            @Override
            public String getTableCreationUniqueConstraintsFragment(Table table) {
                return delegate.getTableCreationUniqueConstraintsFragment(table);
            }

            @Override
            public String getAlterTableToAddUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata) {
                return delegate.getAlterTableToAddUniqueKeyCommand(uniqueKey, metadata) + INITRANS;
            }

            @Override
            public String getAlterTableToDropUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata) {
                return delegate.getAlterTableToDropUniqueKeyCommand(uniqueKey, metadata);
            }
        };
    }

    @Override
    public String getAddForeignKeyConstraintString(
            String constraintName, String[] foreignKey, String referencedTable, String[] primaryKey,
            boolean referencesPrimaryKey) {

        return super.getAddForeignKeyConstraintString(constraintName, foreignKey, referencedTable,
                primaryKey, referencesPrimaryKey) + INITRANS;
    }

    @Override
    public String getAddForeignKeyConstraintString(String constraintName, String foreignKeyDefinition) {
        return super.getAddForeignKeyConstraintString(constraintName, foreignKeyDefinition) + INITRANS;
    }
}

@@ -750,186 +750,21 @@
 ate.
-dialect.unique.UniqueDelegate;%0Aimport org.hibernate.mapping.Column;%0Aimport org.hibernate.mapping.Index;%0Aimport org.hibernate.mapping.Table;%0Aimport org.hibernate.mapping.UniqueKey
+mapping.Index
 ;%0Aim
@@ -1885,32 +1885,32 @@
 ;%0A            %7D%0A
+
         %7D;%0A    %7D
@@ -1913,1667 +1913,7 @@
    %7D
-%0A%0A    @Override%0A    public UniqueDelegate getUniqueDelegate() %7B%0A        UniqueDelegate delegate = super.getUniqueDelegate();%0A%0A        return new UniqueDelegate() %7B%0A%0A            @Override%0A            public String getColumnDefinitionUniquenessFragment(Column column) %7B%0A                return delegate.getColumnDefinitionUniquenessFragment(column);%0A            %7D%0A%0A            @Override%0A            public String getTableCreationUniqueConstraintsFragment(Table table) %7B%0A                return delegate.getTableCreationUniqueConstraintsFragment(table);%0A            %7D%0A%0A            @Override%0A            public String getAlterTableToAddUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata) %7B%0A                return delegate.getAlterTableToAddUniqueKeyCommand(uniqueKey, metadata) + INITRANS;%0A            %7D%0A%0A            @Override%0A            public String getAlterTableToDropUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata) %7B%0A                return delegate.getAlterTableToDropUniqueKeyCommand(uniqueKey, metadata);%0A            %7D%0A        %7D;%0A    %7D%0A%0A    @Override%0A    public String getAddForeignKeyConstraintString(%0A            String constraintName, String%5B%5D foreignKey, String referencedTable, String%5B%5D primaryKey,%0A            boolean referencesPrimaryKey) %7B%0A%0A        return super.getAddForeignKeyConstraintString(constraintName, foreignKey, referencedTable,%0A                primaryKey, referencesPrimaryKey) + INITRANS;%0A    %7D%0A%0A    @Override%0A    public String getAddForeignKeyConstraintString(String constraintName, String foreignKeyDefinition) %7B%0A        return super.getAddForeignKeyConstraintString(constraintName, foreignKeyDefinition) + INITRANS;%0A    %7D
 %0A%7D%0A

remove unnecessary initrans clauses