/**
 * JBoss, Home of Professional Open Source.
 * Copyright 2014 Red Hat, Inc., and individual contributors
 * as indicated by the @author tags.
 *
 * 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 org.jboss.pnc.datastore.repositories;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import javax.inject.Inject;
import javax.persistence.EntityManager;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.dialect.Dialect;
import org.hibernate.jdbc.ReturningWork;
import org.hibernate.jdbc.Work;
import org.hibernate.service.jdbc.dialect.internal.StandardDialectResolver;
import org.hibernate.service.jdbc.dialect.spi.DialectResolver;

public class SequenceHandlerRepository {

    public SequenceHandlerRepository() {

    }

    private EntityManager entityManager;
    private Map<String, Object> entityManagerFactoryProperties;

    @Inject
    public SequenceHandlerRepository(EntityManager entityManager) {
        this.entityManager = entityManager;
        this.entityManagerFactoryProperties = entityManager.getEntityManagerFactory().getProperties();
    }

    public String getEntityManagerFactoryProperty(String propertyName) {
        for (Map.Entry<String, Object> e : entityManagerFactoryProperties.entrySet()) {
            if (e.getKey().trim().equals(propertyName)) {
                return e.getValue().toString();
            }
        }
        return null;
    }

    public Long getNextID(final String sequenceName) {

        ReturningWork<Long> maxReturningWork = new ReturningWork<Long>() {
            @Override
            public Long execute(Connection connection) throws SQLException {
                DialectResolver dialectResolver = new StandardDialectResolver();
                Dialect dialect = dialectResolver.resolveDialect(connection.getMetaData());
                PreparedStatement preparedStatement = null;
                ResultSet resultSet = null;
                try {
                    preparedStatement = connection.prepareStatement(dialect.getSequenceNextValString(sequenceName));
                    resultSet = preparedStatement.executeQuery();
                    resultSet.next();
                    return resultSet.getLong(1);
                } catch (SQLException e) {
                    throw e;
                } finally {
                    if (preparedStatement != null) {
                        preparedStatement.close();
                    }
                    if (resultSet != null) {
                        resultSet.close();
                    }
                }

            }
        };

        Session session = (Session) entityManager.getDelegate();
        SessionFactory sessionFactory = session.getSessionFactory();

        Long maxRecord = sessionFactory.getCurrentSession().doReturningWork(maxReturningWork);
        return maxRecord;
    }

    public void createSequence(final String sequenceName) {

        Work work = new Work() {
            @Override
            public void execute(Connection connection) throws SQLException {
                DialectResolver dialectResolver = new StandardDialectResolver();
                Dialect dialect = dialectResolver.resolveDialect(connection.getMetaData());
                PreparedStatement preparedStatement = null;
                ResultSet resultSet = null;
                try {
                    preparedStatement = connection.prepareStatement(dialect.getCreateSequenceStrings(sequenceName, 1, 1)[0]);
                    preparedStatement.execute();
                } catch (SQLException e) {
                    throw e;
                } finally {
                    if (preparedStatement != null) {
                        preparedStatement.close();
                    }
                    if (resultSet != null) {
                        resultSet.close();
                    }
                }

            }
        };

        Session session = (Session) entityManager.getDelegate();
        SessionFactory sessionFactory = session.getSessionFactory();
        sessionFactory.getCurrentSession().doWork(work);
    }

    public void dropSequence(final String sequenceName) {

        Work work = new Work() {
            @Override
            public void execute(Connection connection) throws SQLException {
                DialectResolver dialectResolver = new StandardDialectResolver();
                Dialect dialect = dialectResolver.resolveDialect(connection.getMetaData());
                PreparedStatement preparedStatement = null;
                ResultSet resultSet = null;
                try {
                    preparedStatement = connection.prepareStatement(dialect.getDropSequenceStrings(sequenceName)[0]);
                    preparedStatement.execute();
                } catch (SQLException e) {
                    throw e;
                } finally {
                    if (preparedStatement != null) {
                        preparedStatement.close();
                    }
                    if (resultSet != null) {
                        resultSet.close();
                    }
                }

            }
        };

        Session session = (Session) entityManager.getDelegate();
        SessionFactory sessionFactory = session.getSessionFactory();
        sessionFactory.getCurrentSession().doWork(work);
    }

}

@@ -3520,32 +3520,106 @@
 equenceName) %7B%0A%0A
+        if (sequenceExists(sequenceName)) %7B%0A            return;%0A        %7D%0A
         Work wor
@@ -4779,32 +4779,1569 @@
 k(work);%0A    %7D%0A%0A
+    public boolean sequenceExists(final String sequenceName) %7B%0A        ReturningWork%3CBoolean%3E work = new ReturningWork%3CBoolean%3E() %7B%0A            @Override%0A            public Boolean execute(Connection connection) throws SQLException %7B%0A                DialectResolver dialectResolver = new StandardDialectResolver();%0A                Dialect dialect = dialectResolver.resolveDialect(connection.getMetaData());%0A                PreparedStatement preparedStatement = null;%0A                ResultSet resultSet = null;%0A                try %7B%0A                    preparedStatement = connection.prepareStatement(dialect.getQuerySequencesString());%0A                    resultSet = preparedStatement.executeQuery();%0A                    while(resultSet.next()) %7B%0A                        if(sequenceName.equals(resultSet.getString(1))) %7B%0A                            return true;%0A                        %7D%0A                    %7D%0A                %7D catch (SQLException e) %7B%0A                    throw e;%0A                %7D finally %7B%0A                    if (preparedStatement != null) %7B%0A                        preparedStatement.close();%0A                    %7D%0A                    if (resultSet != null) %7B%0A                        resultSet.close();%0A                    %7D%0A                %7D%0A                return false;%0A%0A            %7D%0A        %7D;%0A%0A        Session session = (Session) entityManager.getDelegate();%0A        SessionFactory sessionFactory = session.getSessionFactory();%0A        return sessionFactory.getCurrentSession().doReturningWork(work);%0A    %7D%0A%0A
     public void 

Check if the build record sequence exists before creating it