name.marcelomorales.siqisiqi.examples.simple.init.InitialData.java Source code

Java tutorial

Introduction

Here is the source code for name.marcelomorales.siqisiqi.examples.simple.init.InitialData.java

Source

/*
 * Copyright 2013 Marcelo Morales me@marcelomorales.name
 *
 *    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 name.marcelomorales.siqisiqi.examples.simple.init;

import com.google.common.base.Charsets;
import com.google.common.base.Splitter;
import com.google.common.io.Resources;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.ejb.TransactionAttribute;
import javax.inject.Inject;
import javax.inject.Named;
import javax.sql.DataSource;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Map;

/**
 * @author Marcelo Morales
 */
@Named
public class InitialData implements IInitialData {

    private final JdbcTemplate jdbcTemplate;

    @Inject
    public InitialData(DataSource dataSource) throws SQLException, IOException {
        jdbcTemplate = new JdbcTemplate(dataSource);
        try (Connection c = dataSource.getConnection()) {
            URL resource = Resources.getResource("ddl.sql");
            String s = Resources.toString(resource, Charsets.US_ASCII);
            Iterable<String> split = Splitter.on(';').omitEmptyStrings().trimResults().split(s);
            for (String sql : split) {
                try (Statement st = c.createStatement()) {
                    st.execute(sql);
                }
            }
            c.commit(); // this is needed because we don't use @TransactionAttribute
        }
    }

    @Override
    @TransactionAttribute
    public void populate() {
        try {
            URL resource = Resources.getResource("dml.sql");
            String s = Resources.toString(resource, Charsets.US_ASCII);

            Iterable<String> split = Splitter.on(';').omitEmptyStrings().trimResults().split(s);
            for (String sql : split) {
                jdbcTemplate.execute(sql);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    @TransactionAttribute
    public long countdept() {
        return jdbcTemplate.queryForObject("select count(*) from dept", Long.class);
    }

    @Override
    @TransactionAttribute
    public List<Map<String, Object>> emp() {
        return jdbcTemplate.queryForList("select * from emp");
    }

    @Override
    @TransactionAttribute
    public List<Map<String, Object>> dept() {
        return jdbcTemplate.queryForList("select * from dept");
    }

    @Override
    @TransactionAttribute
    public long countemp() {
        return jdbcTemplate.queryForObject("select count(*) from emp", Long.class);
    }
}