package dao;
import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.config.EmbeddedConfiguration;
import com.db4o.constraints.UniqueFieldValueConstraint;
import com.db4o.ext.DatabaseClosedException;
import com.db4o.ext.DatabaseReadOnlyException;
import com.db4o.ext.Db4oIOException;
import com.db4o.ext.InvalidIDException;
import com.db4o.query.Query;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.logging.Level;
import java.util.logging.Logger;
import business.Arvore;
import business.ContainerArvores;
import util.DB4oConnector;
/**
*
* @param <T>
*/
public class AbstractDAO<T> extends Observable {
/**
*
*/
protected ObjectContainer db;
/**
*
*/
protected EmbeddedConfiguration config;
/**
*
*/
protected String key = null;
/**
*
*/
protected int depth;
/**
*
*/
protected Class<T> type;
/**
*
*/
protected boolean connected = false;
/**
*
* @param key
* @param type
*/
public AbstractDAO(String key, Class<T> type) {
this.key = key;
this.depth = 10;
this.type = type;
}
/**
*
*/
public void connect() {
config = Db4oEmbedded.newConfiguration();
config.common().activationDepth(depth);
config.common().updateDepth(depth);
config.common().objectClass(type).objectField(key).indexed(true);
config.common().objectClass(ContainerArvores.class).objectField("id").indexed(true);
config.common().add(new UniqueFieldValueConstraint(type, key));
// config.common().add(new UniqueFieldValueConstraint(Arvore.class, key));
// config.common().add(new UniqueFieldValueConstraint(ContainerArvores.class, key));
config.common().objectClass(type).cascadeOnUpdate(true);
config.common().objectClass(type).cascadeOnActivate(true);
config.common().objectClass(type).cascadeOnDelete(true);
config.common().objectClass(type).generateUUIDs(true);
config.common().objectClass(Arvore.class).generateUUIDs(true);
db = Db4oEmbedded.openFile(config, DB4oConnector.DBFILE.getAbsolutePath());
connected = true;
}
/**
*
*/
public void close() {
db.close();
connected = false;
}
/**
*
* @return
*/
public boolean isConnected() {
return connected;
}
/**
*
* @param obj
* @param close
* @param myID
*/
public void update(T obj, boolean close, long myID) throws Exception {
try {
T o = find(myID);
long idObj = db.ext().getID(o);
db.ext().bind(obj, idObj);
db.ext().activate(obj, depth);
db.store(obj);
db.commit();
if (close) {
db.close();
}
setChanged();
notifyObservers();
} catch (InvalidIDException | DatabaseClosedException | Db4oIOException | DatabaseReadOnlyException e) {
Logger.getLogger(AbstractDAO.class.getName()).log(Level.SEVERE, null, e);
db.rollback();
db.ext().refresh(obj, Integer.MAX_VALUE);
throw new Exception(e);
}
}
/**
*
* @param obj
*/
public void save(T obj) throws Exception {
if (db.queryByExample(obj).isEmpty()) {
try {
db.store(obj);
db.commit();
setChanged();
notifyObservers();
} catch (DatabaseClosedException | DatabaseReadOnlyException | Db4oIOException e) {
Logger.getLogger(AbstractDAO.class.getName()).log(Level.SEVERE, null, e);
db.rollback();
db.ext().refresh(obj, Integer.MAX_VALUE);
throw new Exception(e);
}
}
}
/**
*
* @param obj
*/
public void delete(T obj) throws Exception {
try {
db.delete(obj);
db.commit();
setChanged();
notifyObservers();
} catch (Db4oIOException | DatabaseClosedException | DatabaseReadOnlyException e) {
Logger.getLogger(AbstractDAO.class.getName()).log(Level.SEVERE, null, e);
db.rollback();
db.ext().refresh(obj, Integer.MAX_VALUE);
throw new Exception(e);
}
}
/**
*
* @param orderByField
* @param asc
* @return
*/
public List<T> getAll(String orderByField, boolean asc) throws Exception {
ArrayList<T> result = new ArrayList<>();
try {
Query query = db.query();
query.constrain(type);
query.descend(orderByField).orderAscending();
if (!asc) {
query.descend(orderByField).orderDescending();
}
result.addAll((List) query.execute());
} catch (Exception e) {
Logger.getLogger(AbstractDAO.class.getName()).log(Level.SEVERE, null, e);
db.rollback();
throw new Exception(e);
}
return result;
}
/**
*
* @param obj
* @return
*/
public List<T> queryByExample(T obj) {
List<T> result = new ArrayList<>();
try {
result.addAll((List) db.queryByExample(obj));
db.commit();
} catch (Db4oIOException | DatabaseClosedException | DatabaseReadOnlyException e) {
Logger.getLogger(AbstractDAO.class.getName()).log(Level.SEVERE, null, e);
db.rollback();
}
return result;
}
/**
*
* @param value
* @return
*/
public T find(long value) {
Query q = db.query();
q.constrain(type);
q.descend("id").constrain(value);
List<T> result = q.execute();
if (result.size() > 0) {
return result.get(0);
} else {
return null;
}
}
}