All Downloads are FREE. Search and download functionalities are using the official Maven repository.

prerna.ds.export.gexf.RdbmsGexfIterator Maven / Gradle / Ivy

The newest version!
package prerna.ds.export.gexf;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;

import prerna.ds.rdbms.h2.H2Frame;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import prerna.util.Constants;


public class RdbmsGexfIterator extends AbstractGexfIterator {

	private static final Logger classLogger = LogManager.getLogger(RdbmsGexfIterator.class);

	private H2Frame dataframe;

	private ResultSet nodeRs;
	private String[] nodeRsColumns;

	private ResultSet edgeRs;
	private String[] edgeRsColumns;


	public RdbmsGexfIterator(H2Frame dataframe, String nodeMap, String edgeMap, Map aliasMap) {
		super(nodeMap, edgeMap, aliasMap);
		this.dataframe = dataframe;
	}

	@Override
	public boolean hasNextNode() {
		try {
			if(this.nodeRs == null) {
				if(this.nodeMapSplit == null || this.nodeMapSplit.length == 0) {
					return false;
				}
				
				String nodeString = this.nodeMapSplit[this.nodeIndex];
				this.nodeRsColumns = nodeString.split(",");
				this.nodeRs = this.dataframe.execQuery( createQueryString(this.nodeRsColumns) );
				this.nodeIndex++;
				// use the method itself to execute the first hasNext
				return hasNextNode();
			} else {
				if(this.nodeRs.next()) {
					return true;
				} else {
					// close the connection
					closeRs(this.nodeRs);
					
					// we finished going through for all the nodes
					if(this.nodeIndex >= this.nodeMapSplit.length) {
						return false;
					} else {
						String nodeString = this.nodeMapSplit[this.nodeIndex];
						this.nodeRsColumns = nodeString.split(",");
						this.nodeRs = this.dataframe.execQuery( createQueryString(this.nodeRsColumns) );
						this.nodeIndex++;
						// use the method itself to execute the first hasNext
						return hasNextNode();
					}
				}
			}
		} catch (SQLException e) {
			classLogger.error(Constants.STACKTRACE, e);
		}
		return false;
	}

	@Override
	public String getNextNodeString() {
		StringBuilder node = new StringBuilder("");
			// define the properties
			// default property on every node is the type
			node.append("");
			String typeName = this.nodeRsColumns[0];
			if(this.aliasMap.containsKey(typeName)) {
				typeName = this.aliasMap.get(typeName);
			}
			node.append("");
			
			// add all the other properties
			for(int i = 1; i < this.nodeRsColumns.length; i++) {
				String propName = this.nodeRsColumns[i];
				if(this.aliasMap.containsKey(propName)) {
					propName = this.aliasMap.get(propName);
				}
				
				Object prop = this.nodeRs.getObject(i+1);
				if(prop != null) {
					if(prop instanceof Double || prop instanceof Integer) {
						node.append("");
					} else {
						node.append("");
					}
				}
			}
		} catch (SQLException e) {
			classLogger.error(Constants.STACKTRACE, e);
		}

		node.append("");
		return node.toString();
	}

	@Override
	public boolean hasNextEdge() {
		try {
			if(this.edgeRs == null) {
				if(this.edgeMapSplit == null || this.edgeMapSplit.length == 0) {
					return false;
				}
				
				String edgeString = this.edgeMapSplit[this.edgeIndex];
				this.edgeRsColumns = edgeString.split(",");
				this.edgeRs = this.dataframe.execQuery( createQueryString(this.edgeRsColumns) );
				this.edgeIndex++;
				// use the method itself to execute the first hasNext
				return hasNextEdge();
			} else {
				if(this.edgeRs.next()) {
					return true;
				} else {
					// close the connection
					closeRs(this.edgeRs);
					
					// we finished going through for all the nodes
					if(this.edgeIndex >= this.edgeMapSplit.length) {
						return false;
					} else {
						String edgeString = this.edgeMapSplit[this.edgeIndex];
						this.edgeRsColumns = edgeString.split(",");
						this.edgeRs = this.dataframe.execQuery( createQueryString(this.edgeRsColumns) );
						this.edgeIndex++;
						// use the method itself to execute the first hasNext
						return hasNextEdge();
					}
				}
			}
		} catch (SQLException e) {
			classLogger.error(Constants.STACKTRACE, e);
		}
		return false;
	}

	@Override
	public String getNextEdgeString() {
		StringBuilder edge = new StringBuilder("");
			
			if(this.edgeRsColumns.length > 2) {
				edge.append("");
			}
			
			// add the edge properties
			for(int i = 2; i < this.edgeRsColumns.length; i++) {
				String propName = this.edgeRsColumns[i];
				if(this.aliasMap.containsKey(propName)) {
					propName = this.aliasMap.get(propName);
				}
				
				Object prop = this.edgeRs.getObject(i+1);
				if(prop != null) {
					if(prop instanceof Double || prop instanceof Integer) {
						edge.append("");
					} else {
						edge.append("");
					}
				}
			}
			
			if(this.edgeRsColumns.length > 2) {
				edge.append("");
			}
			
		} catch (SQLException e) {
			classLogger.error(Constants.STACKTRACE, e);
		}

		edge.append("");
		return edge.toString();
	}

	/**
	 * Create the query string to get a node and a list of properties
	 * @param nodeName
	 * @param props
	 * @return
	 */
	private String createQueryString(String[] selectors) {
		StringBuilder sql = new StringBuilder("SELECT DISTINCT ");
		sql.append(selectors[0]).append(" ");
		for(int i = 1; i < selectors.length; i++) {
			sql.append(", ").append(selectors[i]);
		}
		sql.append(" FROM ").append(dataframe.getName());
		return sql.toString();
	}
	
	/**
	 * Close the result set and grab its statement to close
	 * @param rs
	 */
	private void closeRs(ResultSet rs) {
		Statement stmt = null;
		if(rs != null) {
			try {
				stmt = rs.getStatement();
			} catch (SQLException e) {
				classLogger.error(Constants.STACKTRACE, e);
			}
			try {
				rs.close();
			} catch(SQLException e) {
				classLogger.error(Constants.STACKTRACE, e);
			}
		}
		if(stmt != null) {
			try {
				stmt.close();
			} catch(SQLException e1) {
				classLogger.error(Constants.STACKTRACE, e1);
			}
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy