package edu.neu.ccs.task.rdf;
import java.sql.SQLException;
import javax.sql.DataSource;
import webframe.Configuration;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.sdb.StoreDesc;
import edu.neu.ccs.task.agent.Agent;
import edu.neu.ccs.task.web.Persistence;
public class DbPersistence implements Persistence {
private final DataSource dataSource;
private final StoreDesc storeDesc;
public DbPersistence(Configuration c) {
// need to make sure we have RDF support:
c.requireInstance(AgentWithRdfFactory.class, "dtask.agent-factory");
dataSource = c.getSharedObject(DataSource.class);
storeDesc = new StoreDesc(
c.getString("dtask.rdf.db-persistence.layout", "layout2"),
c.getString("dtask.rdf.db-persistence.db-type", "MySQL"));
}
public void load(Agent agent, int user) throws SQLException {
if (user < 0) return;
final Model model = ((TaskEngineWithRdf) agent.getEngine()).getRuntimeRdfModel();
SdbUtil.run(dataSource, storeDesc, "usermodel:" + user, new SdbQuery() {
public void exec(Model sdbModel) {
model.removeAll();
model.add(sdbModel);
}
});
}
public void save(Agent agent, final int user) throws SQLException {
if (user < 0) return;
final AgentWithRdf agentR = (AgentWithRdf) agent;
final Model model = agentR.getEngine().getRuntimeRdfModel();
SdbUtil.run(dataSource, storeDesc, "usermodel:" + user, new SdbQuery() {
public void exec(Model sdbModel) {
sdbModel.begin();
sdbModel.removeAll();
sdbModel.add(model);
sdbModel.commit();
}
});
}
}