package vg.model;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import vg.core.AModel;
import vg.core.exception.CoreException;
import vg.core.exception.EnumCriticalityException;
import vg.core.graph.ENodeType;
import vg.core.graph.Graph;
import vg.core.graph.GraphNode;
import vg.core.graph.SubGraph;
import vg.core.storableGraph.StorableAttribute;
import vg.core.storableGraph.StorableEdge;
import vg.core.storableGraph.StorableGraph;
import vg.core.storableGraph.StorableSubGraph;
import vg.core.storableGraph.StorableVertex;
/**
* This class realizes model, which uses SQL data base.
*/
public class SQLite4JavaModel extends AModel {
private HashMap<Integer,GraphNode>graphSkeletons;
private HashMap<Integer,ArrayList<GraphNode>>waitGraphNodes;
private SQLite4JavaDataBase dataBase;
//-------------------------------------------------------------------------
public SQLite4JavaModel() throws CoreException {
createModel();
}
public synchronized void addStorableGraph(StorableGraph sg, ArrayList<Integer> subgraphIDs) {
this.dataBase.addStorableGraph(sg, subgraphIDs);
GraphNode node = new GraphNode(ENodeType.DEF_ROOT_SUBGRAPH, sg.getRootSubGraphId(), null, sg.getName());
node.setInnerGraph(sg.getRootSubGraphId());
this.graphSkeletons.put(sg.getStorableId(), node);
setInnerGraph(node);
for(Integer buf : subgraphIDs) {
this.waitGraphNodes.remove(buf);
}
}
public synchronized void addStorableSubGraph(StorableSubGraph ssg) {
this.dataBase.addStorableSubGraph(ssg);
//---------------------------------------
ArrayList<GraphNode>array = new ArrayList<GraphNode>();
for(StorableVertex buf : ssg.getVertices()) {
List<StorableAttribute> lsa = buf.getAttributes();
String valueAttr = null;
for(StorableAttribute bufAttr : lsa) {
String name = bufAttr.getName();
if(name != null && name.equals("name")) {
valueAttr = bufAttr.getValue();
break;
}
}
GraphNode node = new GraphNode(ENodeType.DEF_VERTEX, buf.getStorableId(), buf.getId(), valueAttr);
node.setInnerGraph(buf.getInnerGraph());
array.add(node);
}
this.waitGraphNodes.put(ssg.getStorableId(), array);
}
public synchronized void closeGraph(int number) {
//TODO
}
public synchronized StorableGraph getStorableGraph(int graphId) {
return(this.dataBase.getStorableGraph(graphId));
}
public synchronized StorableSubGraph getStorableSubGraph(ArrayList<Integer> vertexId) {
return(this.dataBase.getStorableSubGraph(vertexId));
}
public synchronized Graph getGraph(int graphId) {
StorableGraph sg = this.dataBase.getStorableGraph(graphId);
if(sg != null) {
return(sg.getGraph());
}
return(null);
}
public synchronized StorableSubGraph getStorableSubGraph(int subGraphId) {
return(this.dataBase.getStorableSubGraph(subGraphId));
}
public synchronized SubGraph getSubGraph(int subGraphId) {
StorableSubGraph ssg = this.dataBase.getStorableSubGraph(subGraphId);
if(ssg != null) {
return(ssg.getSubGraph());
}
return(null);
}
public synchronized StorableSubGraph getRootStorableSubGraph(int graphId) {
return(this.dataBase.getStorableRootSubGraph(graphId));
}
public synchronized SubGraph getRootSubGraph(int graphId) {
StorableSubGraph ssg = this.dataBase.getStorableRootSubGraph(graphId);
if(ssg != null) {
return(ssg.getSubGraph());
}
return(null);
}
public synchronized String getGraphName(final int graphId) {
return(this.dataBase.getGraphName(graphId));
}
public synchronized List<List<Object>> executeSQLRequest(final String request) {
return(this.dataBase.executeSQLRequest(request));
}
public synchronized GraphNode getGraphSkeleton(final int graphId) {
GraphNode node = this.graphSkeletons.get(graphId);
if(node != null) {
return(node.clone());
}
return(null);
}
public synchronized void quit() {
this.dataBase.close();
}
public synchronized StorableEdge getStorableEdge(final int edgeId) {
return null;
}
public synchronized StorableVertex getStorableVertex(final int vertexId) {
return(this.dataBase.getStorableVertex(vertexId));
}
public synchronized void resetModel() throws CoreException {
quit();
createModel();
}
//-------------------------------------------------------------------------
private void setInnerGraph(GraphNode root) {
if(root == null) return;
if(root.isLeaf() && root.getInnerGraph() != null) {
ArrayList<GraphNode>nodes = this.waitGraphNodes.get(root.getInnerGraph());
for(GraphNode buf : nodes) {
root.add(buf);
setInnerGraph(buf);
}
}
}
private void createModel() throws CoreException {
try {
this.graphSkeletons = new HashMap<Integer, GraphNode>();
this.waitGraphNodes = new HashMap<Integer, ArrayList<GraphNode>>();
this.dataBase = new SQLite4JavaDataBase();
} catch (Throwable ex) {
throw(new CoreException("[" + this.getClass().getName() + ".SQLite4JavaModel] [FAIL] Exception = " + ex.getMessage(),EnumCriticalityException.FAILED));
}
}
}