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

prerna.sablecc2.om.task.AbstractTask Maven / Gradle / Ivy

The newest version!
package prerna.sablecc2.om.task;

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.ds.shared.RawCachedWrapper;
import prerna.engine.api.IHeadersDataRow;
import prerna.query.querystruct.filters.GenRowFilters;
import prerna.reactor.export.FormatFactory;
import prerna.reactor.export.IFormatter;
import prerna.reactor.export.TableFormatter;
import prerna.sablecc2.om.task.options.TaskOptions;
import prerna.util.Constants;

public abstract class AbstractTask implements ITask {
	
	//logger
	protected transient static Logger classLogger = LogManager.getLogger(AbstractTask.class);

	protected String id;
	// this holds the options object for the FE
	protected transient TaskOptions taskOptions; 
	// this holds the header info for the FE
	protected transient List> headerInfo;
	// this holds the sort info for the FE
	protected transient List> sortInfo;
	// this holds explicitly defined filters on the qs
	protected transient List> filterInfo;
	
	// this holds the formatter to perform any viz specific transformations
	// of the data
	protected transient IFormatter formatter = null;
	// internal offset
	protected long internalOffset = 0;
	// num to collect
	protected int numCollect = 500;
	// size of the task
	protected long numRows = 0;
	// optimized query
	// so we know the limit is a fake one
	protected boolean isOptimize = false;
	
	private boolean meta = true;
	
	public AbstractTask() {
		this.sortInfo = new ArrayList>();
		this.headerInfo = new ArrayList>();
		// just default to a table formatter
		this.formatter = new TableFormatter();
	}
	
	/////////////////////////////////////
	
	/*
	 * abstract methods block
	 */
	
	/**
	 * Need to track a log of the sources
	 * @return
	 */
	public abstract List> getSource();
	
	////////////////////////////////////
	
	/**
	 * Collect data from an iterator
	 * Or return defined outputData
	 * @throws Exception 
	 */
	@Override
	public Map collect(boolean meta) throws Exception {
		Map collectedData = new HashMap(10);
		collectedData.put("data", getData());
		if(meta) {
			collectedData.put("headerInfo", this.getHeaderInfo());
			if(this.taskOptions != null && !this.taskOptions.isEmpty()) {
				collectedData.put("format", getFormatMap());
				collectedData.put("taskOptions", this.taskOptions.getOptions());
				collectedData.put("sortInfo", this.sortInfo);
				collectedData.put("filterInfo", this.filterInfo);
				long numRows = TaskUtility.getNumRows(this);
				if(numRows > 0) {
					collectedData.put("numRows", numRows);
				}
			}
		}
		collectedData.put("sources", getSource());
		collectedData.put("taskId", this.id);
		collectedData.put("numCollected", this.numCollect);
		return collectedData;
	}
	
	@Override
	public Map getMetaMap() {
		Map collectedData = new HashMap(10);
		collectedData.put("headerInfo", this.getHeaderInfo());
		collectedData.put("numCollected", this.numCollect);
		if(this.taskOptions != null && !this.taskOptions.isEmpty()) {
			collectedData.put("format", getFormatMap());
			collectedData.put("taskOptions", this.taskOptions.getOptions());
			collectedData.put("sortInfo", this.sortInfo);
			collectedData.put("filterInfo", this.filterInfo);
			long numRows = 0;
			try {
				numRows = TaskUtility.getNumRows(this);
			} catch (Exception e) {
				classLogger.error(Constants.STACKTRACE, e);
			}
			if(numRows > 0) {
				collectedData.put("numRows", numRows);
			}
		}
		collectedData.put("sources", getSource());
		return collectedData;
	}
	
	/**
	 * Returns structure based on the formatter: Example for Table
	 * {
	 * 	data : List of Arrays of Data,
	 * 	headers: headers of my data 
	 * }
	 */
	public Object getData() {
		this.formatter.clear();
		boolean collectAll = false;
		if(this.numCollect == -1) {
			collectAll = true;
		}
		int count = 0;
		// recall, a task is also an iterator!
		while(this.hasNext() && (collectAll || count < this.numCollect)) {
			IHeadersDataRow next = this.next();
			this.formatter.addData(next);
			count++;
		}
		return formatter.getFormattedData();
	}
	
	@Override
	public List flushOutIteratorAsGrid() {
		TableFormatter format = new TableFormatter();
		while(hasNext()) {
			IHeadersDataRow next = next();
			format.addData(next);
		}
		return format.getData();
	}
	
	// MOST IMPORTANT IS THE ID
	@Override
	public void setId(String newId) {
		this.id = newId;
	}
	
	@Override
	public String getId() {
		return this.id;
	}
	
	// OTHER GETTERS 
	
	@Override
	public TaskOptions getTaskOptions() {
		return this.taskOptions;
	}
	
	@Override
	public List> getHeaderInfo() {
		return this.headerInfo;
	}
	
	@Override
	public List> getSortInfo() {
		return this.sortInfo;
	}
	
	@Override
	public IFormatter getFormatter() {
		return this.formatter;
	}
	
	protected Map getFormatMap() {
		Map formatMap = new HashMap();
		formatMap.put("type", this.formatter.getFormatType());
		formatMap.put("options", this.formatter.getOptionsMap());
		return formatMap;
	}
	
	// OTHER SETTESR

	@Override
	public void setTaskOptions(TaskOptions taskOptions) {
		this.taskOptions = taskOptions;
	}
	
	@Override
	public void setFormat(String format) {
		this.formatter = FormatFactory.getFormatter(format);
	}
	
	@Override
	public void setFormat(IFormatter formatter) {
		this.formatter = formatter;
	}
	
	@Override
	public void setFormatOptions(Map optionValues) {
		this.formatter.setOptionsMap(optionValues);
	}
	
	@Override
	public void setHeaderInfo(List> headerInfo) {
		this.headerInfo = headerInfo;
	}
	
	@Override
	public void setFilterInfo(GenRowFilters grf) {
		this.filterInfo = grf.getFormatedFilters();
	}
	
	@Override
	public List> getFilterInfo() {
		return this.filterInfo;
	}
	
	@Override
	public void setSortInfo(List> sortInfo) {
		this.sortInfo = sortInfo;
	}
	
	@Override
	public void setLogger(Logger logger) {
		this.classLogger = logger;
	}
	
	@Override
	public int getNumCollect() {
		return this.numCollect;
	}
	
	@Override
	public void setNumCollect(int numCollect) {
		this.numCollect = numCollect;
	}
	
	@Override
	public void setMeta(boolean meta) {
		this.meta  = meta;
	}
	
	@Override
	public boolean getMeta() {
		return this.meta;
	}
	
	@Override
	public void optimizeQuery(int limit) throws Exception {
		// this does nothing by default
		// only makes sense for BasicIterator
		// since we modify the QS to only return this many values
	}
	
	@Override
	public boolean isOptimized() {
		return this.isOptimize;
	}
	
	@Override
	public void toOptimize(boolean toOptimize) {
		this.isOptimize = toOptimize;
	}
	
	// JUST TO MAKE IT EASIER TO DEBUG
	
	// dummy method to avoid errors on creating cache
	@Override
	public RawCachedWrapper createCache() throws Exception {
		return null;
	}
	
	// dummy
	@Override
	public String getPragma(String key) {
		return null;
	}

	@Override
	public String toString() {
		return this.id;
	}
	
	public long getNumRows() {
		return this.numRows;
	}

	public void setNumRows(long numRows) {
		this.numRows = numRows;
	}
	
	public long getInternalOffset() {
		return this.internalOffset;
	}
	
	public void setInternalOffset(long internalOffset) {
		this.internalOffset = internalOffset;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy