Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package prerna.poi.main.helper;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import prerna.algorithm.api.SemossDataType;
import prerna.date.SemossDate;
import prerna.om.HeadersException;
import prerna.util.ArrayUtilityMethods;
import prerna.util.Constants;
@Deprecated
public class XLFileHelper {
private static final Logger classLogger = LogManager.getLogger(XLFileHelper.class);
int colStarter = 0;
private static final int NUM_ROWS_TO_PREDICT_TYPES = 1000;
private Workbook workbook = null;
private FileInputStream sourceFile = null;
private String fileLocation = null;
// contains the string to the excel sheet object
private Map sheetNames = new Hashtable();
// gets the sheet name -> headers for sheet
private Map original_headers = new Hashtable();
// gets the sheet name -> headers for sheet
private Map clean_headers = new Hashtable();
// used for iterating through the sheet
private Map sheetCounter = new Hashtable();
// used to assimilate the properties...
// TODO: need to come back and understand what that means...
// has to do with prediction of what things are related but need to look more
// into this
private Map> allProps = new Hashtable>();
/**
* Parse the new file passed
*
* @param fileLocation The String location of the fileLocation
*/
public void parse(String fileLocation) {
this.fileLocation = fileLocation;
createParser();
}
/**
* opens the workbook and gets all the sheets
*/
private void createParser() {
try {
sourceFile = new FileInputStream(fileLocation);
try {
workbook = WorkbookFactory.create(sourceFile);
} catch (EncryptedDocumentException e) {
classLogger.error(Constants.STACKTRACE, e);
}
// get all the sheets
int totalSheets = workbook.getNumberOfSheets();
// store all the sheets
for (int sheetIndex = 0; sheetIndex < totalSheets; sheetIndex++) {
Sheet sheet = workbook.getSheetAt(sheetIndex);
String nameOfSheet = sheet.getSheetName();
sheetNames.put(nameOfSheet, sheet);
String[] sheetHeaders = getSheetHeaders(sheet);
if (sheetHeaders == null) {
continue;
}
original_headers.put(nameOfSheet, sheetHeaders);
// grab the headerChecker
HeadersException headerChecker = HeadersException.getInstance();
List newUniqueCleanHeaders = new Vector();
int numCols = sheetHeaders.length;
for (int colIdx = 0; colIdx < numCols; colIdx++) {
String origHeader = sheetHeaders[colIdx];
if (origHeader.trim().isEmpty()) {
origHeader = "BLANK_HEADER";
}
String newHeader = headerChecker.recursivelyFixHeaders(origHeader, newUniqueCleanHeaders);
// now update the unique headers, as this will be used to match duplications
newUniqueCleanHeaders.add(newHeader);
}
// now store the clean headers
clean_headers.put(nameOfSheet, newUniqueCleanHeaders.toArray(new String[] {}));
}
} catch (FileNotFoundException e) {
classLogger.error(Constants.STACKTRACE, e);
} catch (IOException e) {
classLogger.error(Constants.STACKTRACE, e);
}
}
public String[] getHeaders(String sheetName) {
return clean_headers.get(sheetName);
}
// this is in case the user has a dumb excel
// where the top row is completely empty/null
// find the first non-null row
public String[] getSheetHeaders(Sheet sheet) {
int counter = 0;
Row headerRow = null;
while (headerRow == null && counter < sheet.getLastRowNum()) {
headerRow = sheet.getRow(counter);
counter++;
}
// at this point, the sheet is empty and can't do anything
if (headerRow == null) {
sheetNames.remove(sheet.getSheetName());
return null;
}
// get the headers
int colLength = headerRow.getLastCellNum();
Object[] sheetHeaders = getCells(headerRow, colLength);
// set the new start for the getNextRow for this sheet
sheetCounter.put(sheet.getSheetName(), counter);
return Arrays.copyOf(sheetHeaders, sheetHeaders.length, String[].class);
}
/////////////////// START ALL NEXT ROWS ///////////////////
public Object[] getNextRow(String sheetName) {
Sheet thisSheet = sheetNames.get(sheetName);
int counter = 1;
if (sheetCounter.containsKey(sheetName)) {
counter = sheetCounter.get(sheetName);
}
Object[] thisRow = null;
while (thisRow == null && counter <= thisSheet.getLastRowNum()) {
thisRow = getCells(thisSheet.getRow(counter), getHeaders(sheetName).length);
counter++;
}
// set counter back
sheetCounter.put(sheetName, counter);
// assimilate the properties
// TODO: this logic isn't valid since we get the headers on instantiation of the
// instance
// if(counter == 0) {
// for(int colIndex = colStarter; colIndex < thisRow.length; colIndex++) {
// putProp(thisRow[colIndex], sheetName);
// }
// }
return thisRow;
}
private Object[] getCells(Row row, int totalCol) {
int colLength = totalCol;
Object[] cols = new Object[colLength];
for (int colIndex = colStarter; colIndex < colLength; colIndex++) {
Cell thisCell = row.getCell(colIndex);
cols[colIndex] = getCell(thisCell);
}
return cols;
}
/////////////////// END ALL NEXT ROWS ///////////////////
/////////////////// START SPECIFIC HEADERS NEXT ROWS ///////////////////
public Object[] getNextRow(String sheetName, String[] headersToGet) {
String[] allHeaders = clean_headers.get(sheetName);
if (allHeaders.length == headersToGet.length) {
return getNextRow(sheetName);
}
Sheet thisSheet = sheetNames.get(sheetName);
int counter = 1;
if (sheetCounter.containsKey(sheetName)) {
counter = sheetCounter.get(sheetName);
}
Object[] thisRow = null;
while (thisRow == null && counter <= thisSheet.getLastRowNum()) {
thisRow = getCells(thisSheet.getRow(counter), allHeaders, headersToGet);
counter++;
}
// set counter back
sheetCounter.put(sheetName, counter);
// assimilate the properties
// TODO: this logic isn't valid since we get the headers on instantiation of the
// instance
// if(counter == 0) {
// for(int colIndex = colStarter; colIndex < thisRow.length; colIndex++) {
// putProp(thisRow[colIndex], sheetName);
// }
// }
return thisRow;
}
private Object[] getCells(Row row, String[] sheetHeaders, String[] headersToGet) {
int colLength = row.getLastCellNum();
return getCells(row, sheetHeaders, headersToGet, colLength);
}
private Object[] getCells(Row row, String[] sheetHeaders, String[] headersToGet, int colLength) {
List