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

prerna.reactor.utils.DatabaseMetadataToPdfReactor Maven / Gradle / Ivy

The newest version!
package prerna.reactor.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.Vector;

import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.util.Strings;
import org.xhtmlrenderer.pdf.ITextRenderer;

import prerna.auth.User;
import prerna.auth.utils.SecurityAdminUtils;
import prerna.auth.utils.SecurityEngineUtils;
import prerna.auth.utils.SecurityQueryUtils;
import prerna.masterdatabase.utility.MasterDatabaseUtility;
import prerna.masterdatabase.utility.MetamodelVertex;
import prerna.om.InsightFile;
import prerna.reactor.AbstractReactor;
import prerna.sablecc2.om.PixelDataType;
import prerna.sablecc2.om.PixelOperationType;
import prerna.sablecc2.om.ReactorKeysEnum;
import prerna.sablecc2.om.nounmeta.NounMetadata;
import prerna.util.Constants;
import prerna.util.EngineSyncUtility;
import prerna.util.Utility;

public class DatabaseMetadataToPdfReactor extends AbstractReactor {
	
	private static final Logger classLogger = LogManager.getLogger(DatabaseMetadataToPdfReactor.class);

	private static final String CLASS_NAME = DatabaseMetadataToPdfReactor.class.getName();

	public DatabaseMetadataToPdfReactor() {
		this.keysToGet = new String[] { ReactorKeysEnum.DATABASE.getKey() };
	}

	@Override
	public NounMetadata execute() {
		Logger logger = getLogger(CLASS_NAME);
		organizeKeys();
		String databaseId = this.keyValue.get(this.keysToGet[0]);
		
		// security
		User user = this.insight.getUser();
		databaseId = SecurityQueryUtils.testUserEngineIdForAlias(this.insight.getUser(), databaseId);
		boolean isAdmin = SecurityAdminUtils.userIsAdmin(user);
		if (!isAdmin) {
			boolean isOwner = SecurityEngineUtils.userIsOwner(user, databaseId);
			if (!isOwner) {
				throw new IllegalArgumentException("Database " + databaseId + " does not exist or user does not have permissions to database. User must be the owner to perform this function.");
			}
		}
		
		Map databaseInfo = SecurityEngineUtils.getUserEngineList(this.insight.getUser(), databaseId, null).get(0);
		databaseInfo.putAll(SecurityEngineUtils.getAggregateEngineMetadata(databaseId, null, true));
		databaseInfo.putIfAbsent("description", "");
		databaseInfo.putIfAbsent("tags", new Vector());
		
		logger.info("Pulling database metadata for database " + databaseId);
		Map metamodelObject = new HashMap<>();
		{
			Map cacheMetamodel = EngineSyncUtility.getMetamodel(databaseId);
			if(cacheMetamodel != null) {
				metamodelObject.putAll(cacheMetamodel);
			} else {
				Map metamodel = MasterDatabaseUtility.getMetamodelRDBMS(databaseId, true);
				metamodelObject.putAll(metamodel);
				EngineSyncUtility.setMetamodel(databaseId, metamodel);
			}
		}

		logger.info("Pulling database logical names for database " + databaseId);
		Map> logicalNames = EngineSyncUtility.getMetamodelLogicalNamesCache(databaseId);
		if(logicalNames == null) {
			logicalNames = MasterDatabaseUtility.getDatabaseLogicalNames(databaseId);
			EngineSyncUtility.setMetamodelLogicalNames(databaseId, logicalNames);
		}
		logger.info("Pulling database descriptions for database " + databaseId);
		Map descriptions = EngineSyncUtility.getMetamodelDescriptionsCache(databaseId);
		if(descriptions == null) {
			descriptions = MasterDatabaseUtility.getDatabaseDescriptions(databaseId);
			EngineSyncUtility.setMetamodelDescriptions(databaseId, descriptions);
		}
	
		// now we will create the html of what we want to export
		StringBuilder htmlBuilder = new StringBuilder("");
		htmlBuilder.append("");
		htmlBuilder.append("");
		htmlBuilder.append("");
		htmlBuilder.append("");
		htmlBuilder.append("

Database Id: " + databaseId + "

"); htmlBuilder.append("

Database Name: " + databaseInfo.get("database_name") + "

"); htmlBuilder.append("

Database Type: " + databaseInfo.get("database_type") + "

"); if(databaseInfo.containsKey("description") && !((String) databaseInfo.get("description")).isEmpty()) { htmlBuilder.append("

Description: " + databaseInfo.get("description") + "

"); } if(databaseInfo.containsKey("tags") && !((Collection) databaseInfo.get("tags")).isEmpty()) { htmlBuilder.append(""); Collection tags = (Collection) databaseInfo.get("tags"); for(String tag : tags) { htmlBuilder.append(""); } htmlBuilder.append("
Tags
" + tag + "
"); } htmlBuilder.append("

Data Definitions:

"); Object[] nodes = (Object[]) metamodelObject.get("nodes"); for(Object nodeObject : nodes) { MetamodelVertex nodeMap = (MetamodelVertex) nodeObject; String conceptName = nodeMap.getConceptualName(); Set propNames = nodeMap.getPropSet(); htmlBuilder.append("

" + conceptName + "

"); htmlBuilder.append(""); for(String prop : propNames) { String uid = conceptName + "__" + prop; String logicalDataType = ((Map) metamodelObject.get("dataTypes")).get(uid); String physicalDataType = ((Map) metamodelObject.get("physicalTypes")).get(uid); String logicalNamesConcat = Strings.join(logicalNames.get(uid), ','); if(logicalNamesConcat == null) { logicalNamesConcat = ""; } String description = descriptions.get(uid); if(description == null) { description = ""; } htmlBuilder.append(""); } htmlBuilder.append("
NameLogical Data TypePhysical Data TypeLogical NamesDescription
" + prop + "" + logicalDataType + "" + physicalDataType + "" + logicalNamesConcat + "" + description + "
"); } htmlBuilder.append("

Generated on: " + ZonedDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ")) + "

"); htmlBuilder.append(""); // keep track for deleting at the end List tempPaths = new ArrayList<>(); String insightFolder = this.insight.getInsightFolder(); String random = Utility.getRandomString(5); String tempXhtmlPath = insightFolder + DIR_SEPARATOR + random + ".html"; String outputFileLocation = insightFolder + DIR_SEPARATOR + "Database_Metadata.pdf"; File tempXhtml = new File(tempXhtmlPath); try { FileUtils.writeStringToFile(tempXhtml, htmlBuilder.toString()); tempPaths.add(tempXhtmlPath); } catch (IOException e1) { logger.error(Constants.STACKTRACE, e1); } // Convert from xhtml to pdf FileOutputStream fos = null; try { logger.info("Converting html to PDF..."); fos = new FileOutputStream(outputFileLocation); ITextRenderer renderer = new ITextRenderer(); renderer.setDocument(tempXhtml.getAbsoluteFile()); renderer.layout(); renderer.createPDF(fos); logger.info("Done converting html to PDF..."); } catch (FileNotFoundException e) { logger.error(Constants.STACKTRACE, e); } catch (IOException ioe) { logger.error(Constants.STACKTRACE, ioe); } catch (Exception ex) { logger.error(Constants.STACKTRACE, ex); } finally { if(fos != null) { try { fos.close(); } catch (IOException e) { classLogger.error(Constants.STACKTRACE, e); } } } // delete temp files for (String path : tempPaths) { try { File f = new File(Utility.normalizePath(path)); if (f.exists()) { FileUtils.forceDelete(f); } } catch (IOException e) { logger.error(Constants.STACKTRACE, e); } } // store it in the insight so the FE can download it // only from the given insight String downloadKey = UUID.randomUUID().toString(); InsightFile insightFile = new InsightFile(); insightFile.setFileKey(downloadKey); insightFile.setDeleteOnInsightClose(true); insightFile.setFilePath(outputFileLocation); this.insight.addExportFile(downloadKey, insightFile); return new NounMetadata(downloadKey, PixelDataType.CONST_STRING, PixelOperationType.FILE_DOWNLOAD); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy