Package br.net.woodstock.rockframework.domain.persistence.jdbc.impl

Source Code of br.net.woodstock.rockframework.domain.persistence.jdbc.impl.AbstractJDBCRepository

/*
* This file is part of rockframework.
*
* rockframework is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* rockframework is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>;.
*/
package br.net.woodstock.rockframework.domain.persistence.jdbc.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import javax.persistence.PersistenceException;

import br.net.woodstock.rockframework.core.RockFrameworkVersion;
import br.net.woodstock.rockframework.core.jdbc.Client;
import br.net.woodstock.rockframework.core.jdbc.Parameter;
import br.net.woodstock.rockframework.core.jdbc.ParameterList;
import br.net.woodstock.rockframework.core.jdbc.impl.CommonCallableClient;
import br.net.woodstock.rockframework.domain.persistence.FilterParameter;
import br.net.woodstock.rockframework.domain.persistence.jdbc.JDBCFilter;
import br.net.woodstock.rockframework.domain.persistence.jdbc.JDBCMapper;
import br.net.woodstock.rockframework.domain.persistence.jdbc.JDBCRepository;

public abstract class AbstractJDBCRepository implements JDBCRepository {

  private static final long        serialVersionUID  = RockFrameworkVersion.VERSION;

  private JDBCFilterParameterComparator  comparator;

  public AbstractJDBCRepository() {
    super();
    this.comparator = new JDBCFilterParameterComparator();
  }

  @Override
  public <T> T getSingle(final JDBCFilter filter, final JDBCMapper<T> mapper) {
    try {
      Client client = new CommonCallableClient(this.getConnection());
      ResultSet rs = client.executeQuery(filter.getFilter(), this.toParameterList(filter.getParameters()));
      if (rs.next()) {
        return mapper.map(rs);
      }
      return null;
    } catch (SQLException e) {
      throw new PersistenceException(e);
    }
  }

  @Override
  public <T> Collection<T> getCollection(final JDBCFilter filter, final JDBCMapper<T> mapper) {
    try {
      Client client = new CommonCallableClient(this.getConnection());
      ResultSet rs = client.executeQuery(filter.getFilter(), this.toParameterList(filter.getParameters()));
      List<T> list = new LinkedList<T>();
      while (rs.next()) {
        list.add(mapper.map(rs));
      }
      return list;
    } catch (SQLException e) {
      throw new PersistenceException(e);
    }
  }

  @Override
  public int executeUpdate(final JDBCFilter filter) {
    try {
      Client client = new CommonCallableClient(this.getConnection());
      int i = client.executeUpdate(filter.getFilter(), this.toParameterList(filter.getParameters()));
      return i;
    } catch (SQLException e) {
      throw new PersistenceException(e);
    }
  }

  private ParameterList toParameterList(final List<FilterParameter> parameters) {
    if (parameters == null) {
      return null;
    }

    Collections.sort(parameters, this.comparator);

    ParameterList list = new ParameterList();
    for (FilterParameter parameter : parameters) {
      Parameter p = new Parameter(parameter.getValue(), parameter.getType());
      list.add(p);
    }
    return list;
  }

  protected abstract Connection getConnection();

}
TOP

Related Classes of br.net.woodstock.rockframework.domain.persistence.jdbc.impl.AbstractJDBCRepository

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.