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

prerna.forms.AbstractFormBuilder Maven / Gradle / Ivy

There is a newer version: 4.2.2
Show newest version
package prerna.forms;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import prerna.ds.util.RdbmsQueryBuilder;
import prerna.engine.api.IEngine;
import prerna.engine.api.IRawSelectWrapper;
import prerna.poi.main.RDBMSEngineCreationHelper;
import prerna.rdf.engine.wrappers.WrapperManager;
import prerna.util.Constants;
import prerna.util.OWLER;
import prerna.util.Utility;

public abstract class AbstractFormBuilder {

	protected static final Logger LOGGER = LogManager.getLogger(AbstractFormBuilder.class.getName());

	public static final String FORM_BUILDER_ENGINE_NAME = "form_builder_engine";
	protected static final String AUDIT_FORM_SUFFIX = "_FORM_LOG";
	protected static final DateFormat DATE_DF = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSSSSS");
	protected static final String OVERRIDE = "override";
	protected static final String ADD = "Added";
	protected static final String REMOVE = "Removed";
	protected static final String ADMIN_SIGN_OFF = "Certified";

	protected IEngine formEng;
	protected String auditLogTableName;
	protected String user;
	protected List tagCols;
	protected List tagValues;
	
	protected IEngine engine;
	
	protected AbstractFormBuilder(IEngine engine) {
		this.formEng = Utility.getEngine(FORM_BUILDER_ENGINE_NAME);
		this.engine = engine;
		this.auditLogTableName = RDBMSEngineCreationHelper.escapeForSQLStatement(RDBMSEngineCreationHelper.cleanTableName(this.engine.getEngineId())).toUpperCase() + FormBuilder.AUDIT_FORM_SUFFIX;
		generateEngineAuditLog(this.auditLogTableName);
	}
	
	public void commitFormData(Map engineHash, String user) throws IOException {
		this.user = user;
		
		if(engineHash.containsKey("tagCols") && engineHash.containsKey("tagValues")) {
			this.tagCols = (List) engineHash.get("tagCols");
			this.tagValues = (List) engineHash.get("tagValues");
		}
		
		String semossBaseURI = "http://semoss.org/ontologies";
		String baseURI = engine.getNodeBaseUri();
		if(baseURI != null && !baseURI.isEmpty()) {
			baseURI = baseURI.replace("/Concept/", "");
		} else {
			baseURI = semossBaseURI;
		}

		String relationBaseURI = semossBaseURI + "/" + Constants.DEFAULT_RELATION_CLASS;
		String conceptBaseURI = semossBaseURI + "/" + Constants.DEFAULT_NODE_CLASS;
		String propertyBaseURI = semossBaseURI + "/" + Constants.DEFAULT_PROPERTY_CLASS;

		List> nodes = new ArrayList>();
		if(engineHash.containsKey("nodes")) {
			nodes = (List>) engineHash.get("nodes"); 
		}
		List> relationships = new ArrayList>();
		if(engineHash.containsKey("relationships")) {
			relationships = (List>)engineHash.get("relationships");
		}
		List> removeNodes = new ArrayList>();
		if(engineHash.containsKey("removeNodes")) {
			removeNodes = (List>) engineHash.get("removeNodes"); 
		}
		List> removeRelationships = new ArrayList>();
		if(engineHash.containsKey("removeRelationships")) {
			removeRelationships = (List>)engineHash.get("removeRelationships");
		}

		saveFormData(baseURI, conceptBaseURI, relationBaseURI, propertyBaseURI, nodes, relationships, removeNodes, removeRelationships);

		//commit information to db
		this.formEng.commit();
		this.engine.commit();
	}
	
	protected void generateEngineAuditLog(String auditLogTableName) {
		// create audit table if doesn't exist
		String checkTableQuery = "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='" + auditLogTableName + "'";
		IRawSelectWrapper wrapper = WrapperManager.getInstance().getRawWrapper(this.formEng, checkTableQuery);
		boolean auditTableExists = false;
		if(wrapper.hasNext()) {
			auditTableExists = true;
			// call the next so we close the rs
			wrapper.next();
		}
		if(!auditTableExists) {
			OWLER owler = new OWLER(this.formEng, this.formEng.getOWL());
			owler.addConcept(auditLogTableName, "ID", "INT");
			owler.addProp(auditLogTableName, "ID", "USER", "VARCHAR(255)");
			owler.addProp(auditLogTableName, "ID", "ACTION", "VARCHAR(100)");
			owler.addProp(auditLogTableName, "ID", "START_NODE", "VARCHAR(255)");
			owler.addProp(auditLogTableName, "ID", "REL_NAME", "VARCHAR(255)");
			owler.addProp(auditLogTableName, "ID", "END_NODE", "VARCHAR(255)");
			owler.addProp(auditLogTableName, "ID", "PROP_NAME", "VARCHAR(255)");
			owler.addProp(auditLogTableName, "ID", "PROP_VALUE", "CLOB");
			owler.addProp(auditLogTableName, "ID", "TIME", "TIMESTAMP");

			LOGGER.info("CREATING NEW AUDIT LOG!!!");
			StringBuilder createAuditTable = new StringBuilder("CREATE TABLE ");
			createAuditTable.append(auditLogTableName).append("(ID IDENTITY, USER VARCHAR(255), ACTION VARCHAR(100), START_NODE VARCHAR(255), "
					+ "REL_NAME VARCHAR(255), END_NODE VARCHAR(255), PROP_NAME VARCHAR(255), PROP_VALUE CLOB, TIME TIMESTAMP");
			if(this.tagCols != null) {
				for(String tag : tagCols) {
					createAuditTable.append(tag).append(" VARCHAR(100), ");
					owler.addProp(auditLogTableName, "ID", tag.toUpperCase(), "VARCHAR(100)");
				}
			}
			createAuditTable.append(")");
			String query = createAuditTable.toString();
			LOGGER.info("SQL SCRIPT >>> " + query);
			this.formEng.insertData(query);
			owler.commit();
			try {
				owler.export();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else if(this.tagCols != null && this.tagCols.size() > 0){
			// need to execute and get the columns for the table
			// need to make sure it has the tag cols
			// since there can be multiple forms with different tags for searching on the same engine
			
			OWLER owler = new OWLER(this.formEng, this.formEng.getOWL());

			// 1) query to get the current cols
			String allColsPresent = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" + auditLogTableName.toUpperCase() + "'";
			wrapper = WrapperManager.getInstance().getRawWrapper(this.formEng, allColsPresent);
			List cols = new Vector();
			while(wrapper.hasNext()) {
				cols.add(wrapper.next().getValues()[0] + "");
			}
			// 2) find the cols that we need to add
			List colsToAdd = new Vector();
			List colsToAddTypes = new Vector();
			for(String tag : tagCols) {
				if(!cols.contains(tag)) {
					colsToAdd.add(tag);
					colsToAddTypes.add("VARCHAR(100)");
					owler.addProp(auditLogTableName, "ID", tag.toUpperCase(), "VARCHAR(100)");
				}
			}
			
			// 3) perform an update
			if(colsToAdd.size() > 0) {
				String alterQuery = RdbmsQueryBuilder.makeAlter(auditLogTableName, colsToAdd.toArray(new String[] {}), colsToAddTypes.toArray(new String[] {}));
				LOGGER.info("ALTERING TABLE: " + alterQuery);
				this.formEng.insertData(alterQuery);
				LOGGER.info("DONE ALTER TABLE");
				
				owler.commit();
				try {
					owler.export();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * Method to actually save form data
	 * @param baseURI
	 * @param conceptBaseURI
	 * @param relationBaseURI
	 * @param propertyBaseURI
	 * @param nodes
	 * @param relationships
	 * @param removeNodes
	 * @param removeRelationships
	 */
	protected abstract void saveFormData(
			String baseURI, 
			String conceptBaseURI,
			String relationBaseURI, 
			String propertyBaseURI,
			List> nodes, 
			List> relationships,
			List> removeNodes, 
			List> removeRelationships);


	/**
	 * Method to replace an instance across the entire db
	 * @param origName
	 * @param newName
	 * @param deleteInstanceBoolean
	 */
	public abstract void modifyInstanceValue(String origName, String newName, boolean deleteInstanceBoolean);
	
	public abstract void certifyInstance(String conceptType, String instanceName);
	
	/**
	 * Store the action that was performed in the audit log
	 * @param action
	 * @param startNode
	 * @param relName
	 * @param endNode
	 * @param propName
	 * @param propValue
	 * @param timeStamp
	 */
	protected void addAuditLog(String action, String startNode, String relName, String endNode, String propName, String propValue, String timeStamp) {
		String cleanUser = null;
		// TODO: FE NEEDS TO PASS IN USER!
		if(this.user != null) {
			cleanUser = RDBMSEngineCreationHelper.escapeForSQLStatement(this.user);
		} else {
			cleanUser = "User Information Not Submitted";
		}
		
		startNode = RDBMSEngineCreationHelper.escapeForSQLStatement(startNode);
		relName = RDBMSEngineCreationHelper.escapeForSQLStatement(relName);
		endNode = RDBMSEngineCreationHelper.escapeForSQLStatement(endNode);
		propName = RDBMSEngineCreationHelper.escapeForSQLStatement(propName);
		propValue = RDBMSEngineCreationHelper.escapeForSQLStatement(propValue);

		String valuesBreak = "', '";
		StringBuilder insertLogStatement = new StringBuilder("INSERT INTO ");
		insertLogStatement.append(this.auditLogTableName).append("(USER, ACTION, START_NODE, REL_NAME, END_NODE, PROP_NAME, PROP_VALUE, TIME) VALUES('")
						.append(cleanUser).append(valuesBreak).append(action).append(valuesBreak).append(startNode).append(valuesBreak)
						.append(relName).append(valuesBreak).append(endNode).append(valuesBreak).append(propName).append(valuesBreak)
						.append(propValue).append(valuesBreak).append(timeStamp).append("')");
		this.formEng.insertData(insertLogStatement.toString());
	}

	public void setUser(String user) {
		this.user = user;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy