
it.netgrid.commons.ormlite.TemplateBulkService Maven / Gradle / Ivy
The newest version!
package it.netgrid.commons.ormlite;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import com.j256.ormlite.dao.CloseableIterator;
import com.j256.ormlite.misc.TransactionManager;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.support.ConnectionSource;
import it.netgrid.commons.data.BulkService;
import it.netgrid.commons.data.CrudObject;
import it.netgrid.commons.data.CrudService;
import it.netgrid.commons.data.DataAdapter;
public abstract class TemplateBulkService, ID> implements BulkService {
protected final ConnectionSource connection;
protected final CrudService crudService;
protected TemplateBulkService(ConnectionSource connection) {
this.connection = connection;
this.crudService = null;
}
protected TemplateBulkService(ConnectionSource connection, CrudService crudService) {
this.connection = connection;
this.crudService = crudService;
}
@Override
public List read(List ids) throws SQLException, IllegalArgumentException {
// TODO Auto-generated method stub
return null;
}
@Override
public List create(List objects) throws SQLException, IllegalArgumentException {
Integer affected = TransactionManager.callInTransaction(connection, new Callable() {
@Override
public Integer call() throws Exception {
return createRaw(objects);
}
});
return (affected > 0) ? this.getIds(objects) : this.getIds(null);
}
@Override
public List update(List objects) throws SQLException, IllegalArgumentException {
Integer affected = TransactionManager.callInTransaction(connection, new Callable() {
@Override
public Integer call() throws Exception {
return updateRaw(objects);
}
});
return (affected > 0) ? this.getIds(objects) : this.getIds(null);
}
@Override
public List delete(List objects) throws SQLException, IllegalArgumentException {
Integer affected = TransactionManager.callInTransaction(connection, new Callable() {
@Override
public Integer call() throws Exception {
return deleteRaw(objects);
}
});
return (affected > 0) ? this.getIds(objects) : this.getIds(null);
}
@Override
public List deleteAll(List ids) throws SQLException, IllegalArgumentException {
Integer affected = TransactionManager.callInTransaction(connection, new Callable() {
@Override
public Integer call() throws Exception {
return deleteAllRaw(ids);
}
});
return (affected > 0) ? ids : this.getIds(null);
}
@Override
public int createRaw(List list) throws SQLException, IllegalArgumentException {
int affected = 0;
for(T item : list) {
affected += this.crudService.createRaw(item);
}
return affected;
}
@Override
public int updateRaw(List list) throws SQLException, IllegalArgumentException {
int affected = 0;
for(T item : list) {
affected += this.crudService.updateRaw(item);
}
return affected;
}
@Override
public int deleteRaw(List list) throws SQLException, IllegalArgumentException {
int affected = 0;
for(T item : list) {
affected += this.crudService.deleteRaw(item);
}
return affected;
}
@Override
public int deleteAllRaw(List ids) throws SQLException, IllegalArgumentException {
int affected = 0;
for(ID item : ids) {
T object = this.crudService.read(item);
affected += this.crudService.deleteRaw(object);
}
return affected;
}
@Override
public List read(Map filter, Long pageSize, Long offset) throws SQLException, IllegalArgumentException {
QueryBuilder, ID> query = this.initQueryBuilder();
if(filter != null) {
this.applyFilter(query, filter);
}
if(pageSize != null) {
this.applyPageSize(query, pageSize);
}
if(offset != null) {
this.applyOffset(query, offset);
}
List retval = new ArrayList();
CloseableIterator> iterator = query.iterator();
while(iterator.hasNext()) {
@SuppressWarnings("unchecked")
CrudObject queryItem = (CrudObject) iterator.next();
T item = this.crudService.read(queryItem.getId());
retval.add(item);
}
return retval;
}
@Override
public List read(DataAdapter dataAdapter, Map filter, Long pageSize, Long offset) throws SQLException, IllegalArgumentException {
QueryBuilder, ID> query = this.initQueryBuilder();
if(filter != null) {
this.applyFilter(query, filter);
}
if(pageSize != null) {
this.applyPageSize(query, pageSize);
}
if(offset != null) {
this.applyOffset(query, offset);
}
List retval = new ArrayList();
CloseableIterator> iterator = query.iterator();
while(iterator.hasNext()) {
@SuppressWarnings("unchecked")
CrudObject queryItem = (CrudObject) iterator.next();
T item = this.crudService.read(queryItem.getId());
D output = dataAdapter.getData(item);
retval.add(output);
}
return retval;
}
public void applyPageSize(QueryBuilder, ID> query, Long pageSize) {
query.limit(pageSize);
}
public void applyOffset(QueryBuilder, ID> query, Long offset) throws SQLException {
query.offset(offset);
}
public abstract void applyFilter(QueryBuilder, ID> query, Map filter) throws SQLException;
public abstract QueryBuilder, ID> initQueryBuilder();
private List getIds(List items) {
List retval = new ArrayList();
if(items != null) {
for(T item : items) {
retval.add(item.getId());
}
}
return retval;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy