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

prerna.reactor.insights.GetInsightMetamodelReactor Maven / Gradle / Ivy

The newest version!
package prerna.reactor.insights;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import prerna.auth.utils.SecurityInsightUtils;
import prerna.auth.utils.SecurityProjectUtils;
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;

public class GetInsightMetamodelReactor extends AbstractReactor {
	
	private static final Logger classLogger = LogManager.getLogger(GetInsightMetamodelReactor.class);
	
	public GetInsightMetamodelReactor() {
		this.keysToGet = new String[]{
				ReactorKeysEnum.PROJECT.getKey(),
				ReactorKeysEnum.ID.getKey()
				};
	}

	@Override
	public NounMetadata execute() {
		organizeKeys();
		String projectId = this.keyValue.get(this.keysToGet[0]);
		String id = this.keyValue.get(this.keysToGet[1]);
		
		projectId = SecurityProjectUtils.testUserProjectIdForAlias(this.insight.getUser(), projectId);
		if(!SecurityInsightUtils.userCanViewInsight(this.insight.getUser(), projectId, id)) {
			throw new IllegalArgumentException("Insight does not exist or user does not have permission to view this insight");
		}
		
		List insightFrames = SecurityInsightUtils.getInsightFrames(projectId, id);
		
		Map> groupedByFrame = groupConcepts(insightFrames);
		
		Map result = new HashMap<>();
		for (String cn : groupedByFrame.keySet()) {
			List objs = groupedByFrame.get(cn);
			Map frameMetamodel = buildResponsePerFrame(cn, objs);
			result.put(cn, frameMetamodel);
		}
		
		return new NounMetadata(result, PixelDataType.CUSTOM_DATA_STRUCTURE, PixelOperationType.INSIGHT_METAMODEL);
	}
	
	private Map buildResponsePerFrame(String cn, List o) {
		Map results = new HashMap<>();
		List edges = new ArrayList<>();
		results.put("edges", edges);

		Map nodes = new HashMap<>();
		List propSet = new ArrayList<>();
		nodes.put("propSet", propSet);
		nodes.put("conceptualName", cn);
				
		Map additionalDataTypes = new HashMap<>();
		Map dataTypes = new HashMap<>();
		
		results.put("propSet", propSet);
		results.put("additionalDataTypes", additionalDataTypes);
		results.put("dataTypes", dataTypes);
		
		for (Object[] ob : o) {
			String alias = ob[2].toString();
			String colName = ob[4].toString();
			String colType = ob[5].toString();
			String sadtl = null;
			
			String unique = cn + "__" + colName;
			
			Object adtl = ob[6];
			if (adtl != null) {
				sadtl = adtl.toString();
				additionalDataTypes.put(unique, sadtl);
			}
			
			propSet.add(colName);
			dataTypes.put(unique, colType);
		}
		
		return results;
	}
	
	private Map> groupConcepts(List insightFrames) {
		Map> groupedByFrame = new HashMap<>();
		for (Object[] ifs : insightFrames) {
			Object cn = ifs[2];
			if (cn == null) {
				continue;
			}
			
			String scn = cn.toString();
			
			List curr;
			if (groupedByFrame.containsKey(scn)) {
				curr = groupedByFrame.get(scn);
			} else {
				curr = new ArrayList<>();
			}
			curr.add(ifs);
			groupedByFrame.put(scn, curr);
		}
		
		return groupedByFrame;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy