D:\CRM_Project\CRM_Metadata_Structure\NetBeamsICM_TestProject\ICM_DB_TestComponents\src\java\unstructured_data_parser\Excel2DAO_CellParserBusinessRule_1.java
  1 /*
  2  * To change this license header, choose License Headers in Project Properties.
  3  * To change this template file, choose Tools | Templates
  4  * and open the template in the editor.
  5  */
  6 package unstructured_data_parser;
  7 
  8 import constants.MainConstants;
  9 import java.util.ArrayList;
 10 import unstructured_data_idao.TokenValueDAO;
 11 import unstructured_data_idao.ExcelCellMap2DAO;
 12 import unstructured_data_idao.ExcelDAO_Constants;
 13 
 14 /**
 15  *
 16  * @author sameldin
 17  */
 18 public class Excel2DAO_CellParserBusinessRule_1 {
 19     
 20     ExcelCellMap2DAO classExcelCellMap2DAO = null;
 21     
 22     public Excel2DAO_CellParserBusinessRule_1(ExcelCellMap2DAO passedExcelCellMap2DAO,
 23                         ArrayList <TokenValueDAO> passedTokenValueDAOArrayList){
 24         
 25         classExcelCellMap2DAO = passedExcelCellMap2DAO;
 26         int cellTypeIndex = classExcelCellMap2DAO.getCellType();
 27         switch(cellTypeIndex)
 28         {
 29             case ExcelDAO_Constants.EXCEL_CELL_TYPE_BLANK:
 30                 classExcelCellMap2DAO.setCellRowEvaluationIndex(ExcelDAO_Constants.NO_EVALUATION);
 31                 classExcelCellMap2DAO.setCellEvaluationString(ExcelDAO_Constants.BLANK_EVALUATION_STRING);               
 32                 //System.out.print("EXCEL_CELL_TYPE_BLANK ");
 33                 break;
 34             case ExcelDAO_Constants.EXCEL_CELL_TYPE_NUMERIC:
 35                 classExcelCellMap2DAO.setCellRowEvaluationIndex(ExcelDAO_Constants.NUMERIC_DOUBLE_INDEX);
 36                 classExcelCellMap2DAO.setCellEvaluationString(ExcelDAO_Constants.NUMERIC_DOUBLE_EVALUATION_STRING);               
 37                 //System.out.print("EXCEL_CELL_TYPE_NUMERIC ");
 38                 break;
 39             case ExcelDAO_Constants.EXCEL_CELL_TYPE_STRING:
 40                 int result = parseCellString(classExcelCellMap2DAO.getStringCellvalue(), passedTokenValueDAOArrayList);
 41                 //System.out.println("EXCEL_CELL_TYPE_STRING result = " + result);
 42                 break;
 43             case ExcelDAO_Constants.EXCEL_CELL_TYPE_BOOLEAN:
 44                 classExcelCellMap2DAO.setCellRowEvaluationIndex(ExcelDAO_Constants.BOOLEAN_EVALUATION_INDEX);
 45                 classExcelCellMap2DAO.setCellEvaluationString(ExcelDAO_Constants.BOOLEAN_EVALUATION_STRING);               
 46                 //System.out.print("EXCEL_CELL_TYPE_BOOLEAN ");
 47                 break;
 48             case ExcelDAO_Constants.EXCEL_CELL_TYPE_FORMULA:
 49                 int success = evaluateFormulaString(classExcelCellMap2DAO.getFormulaCellvalue());
 50                 //System.out.print("EXCEL_CELL_TYPE_FORMULA ");
 51                 break;
 52             case ExcelDAO_Constants.EXCEL_CELL_TYPE_ERROR:
 53                 classExcelCellMap2DAO.setCellRowEvaluationIndex(ExcelDAO_Constants.NO_EVALUATION);
 54                 classExcelCellMap2DAO.setCellEvaluationString(ExcelDAO_Constants.ERROR_EVALUATION_STRING);               
 55                 //System.out.print("EXCEL_CELL_TYPE_ERROR ");
 56                 break;
 57             case ExcelDAO_Constants.EXCEL_CELL_TYPE_DEFAULT:
 58                 classExcelCellMap2DAO.setCellRowEvaluationIndex(ExcelDAO_Constants.NO_EVALUATION);
 59                 classExcelCellMap2DAO.setCellEvaluationString(ExcelDAO_Constants.ERROR_EVALUATION_STRING);               
 60                 //System.out.print("EXCEL_CELL_TYPE_DEFAULT ");
 61                 break;
 62            default:
 63                 System.out.print("default ");
 64         }
 65         
 66     }
 67      /*
 68         
 69         */   
 70     int parseCellString(String passedString, ArrayList <TokenValueDAO> passedTokenValueDAOArrayList) {
 71         
 72          // check is arrayList is null
 73         if(null == passedTokenValueDAOArrayList){
 74              System.out.print("Error: passedTokenValueDAOArrayList = null");
 75             return(MainConstants.FAILED);
 76         }
 77         String stringW_underscoreSeparator      = "";
 78         String stringW_spaceSeparator           = "";
 79         int index                       = 0;
 80         int tokenValue                  = 0;
 81         String possibleVariableName     = "";
 82         boolean found       = false;
 83         // search for perfect match
 84         for(index = 0; index < passedTokenValueDAOArrayList.size(); index++){
 85             
 86             TokenValueDAO localTokenValueDAO = passedTokenValueDAOArrayList.get(index);
 87             stringW_underscoreSeparator = localTokenValueDAO.getStringToken();
 88             tokenValue = localTokenValueDAO.getTokenValue();
 89             possibleVariableName = localTokenValueDAO.getVariableNameString();
 90             
 91             if(stringW_underscoreSeparator.equalsIgnoreCase(passedString)){
 92                 found = true;
 93                 break;
 94             }
 95             else{
 96                 stringW_spaceSeparator = 
 97                         stringW_underscoreSeparator.replace(MainConstants.UNDERSCORE_STRING, MainConstants.SPACE_STRING);
 98                 if(stringW_underscoreSeparator.equalsIgnoreCase(passedString)){
 99                     found = true;
100                     break;
101                 }
102             }
103         } //end of for loop
104         if(found){
105                 classExcelCellMap2DAO.setCellRowEvaluationIndex(ExcelDAO_Constants.STRING_KNOWN_HEADER_INDEX);
106                 classExcelCellMap2DAO.setCellEvaluationString(ExcelDAO_Constants.STRING_LABEL_EVALUATION);
107                 classExcelCellMap2DAO.setKnownStringTokenIndex(tokenValue);
108                 classExcelCellMap2DAO.setPossibleVariableName(possibleVariableName);
109                 //classExcelCellMap2DAO.
110         }
111         // search for partial match
112         else {
113             if(passedString != null){
114                 if(passedString.length() < ExcelDAO_Constants.STRING_LABEL_SIZE)
115                 {
116                     classExcelCellMap2DAO.setCellRowEvaluationIndex(ExcelDAO_Constants.STRING_LABEL_INDEX);
117                     classExcelCellMap2DAO.setKnownStringTokenIndex(ExcelDAO_Constants.STRING_LABEL_INDEX);
118                     classExcelCellMap2DAO.setCellEvaluationString(ExcelDAO_Constants.STRING_LABEL_EVALUATION);
119                     // replace Space with underscore as possible variable name
120                     passedString = passedString.replace(" ", "_");
121                     classExcelCellMap2DAO.setPossibleVariableName(passedString);
122                     return(MainConstants.SUCCESS);
123                 }
124                 if(passedString.length() >= ExcelDAO_Constants.STRING_COMMENT_SIZE)
125                 {
126                     classExcelCellMap2DAO.setCellRowEvaluationIndex(ExcelDAO_Constants.STRING_COMMENT_INDEX);
127                     classExcelCellMap2DAO.setKnownStringTokenIndex(ExcelDAO_Constants.STRING_COMMENT_INDEX);
128                     classExcelCellMap2DAO.setCellEvaluationString(ExcelDAO_Constants.STRING_COMMENT_EVALUATION);
129                     classExcelCellMap2DAO.setPossibleVariableName(MainConstants.NO_VALUE_IN_STRING);
130                     return(MainConstants.SUCCESS);
131                 }
132                 classExcelCellMap2DAO.setCellEvaluationString(ExcelDAO_Constants.UNKNOWN_STRING_STRING_EVALUATION);
133                 return(MainConstants.SUCCESS);                
134             }
135             return(MainConstants.FAILED);            
136         }            
137         return(MainConstants.SUCCESS);
138     }
139     /*
140     
141     */
142     int evaluateFormulaString(String formulaString){
143 
144         String formulaAllCapsString = formulaString.toUpperCase();
145         char equalChar = formulaAllCapsString.charAt(0);
146         if(equalChar != ExcelDAO_Constants.CELL_FUNCTION_STARTING_EQUAL_STRING){
147             System.out.println("Error: formula String - first char is not '=' ");
148             classExcelCellMap2DAO.setCellRowEvaluationIndex(ExcelDAO_Constants.ERROR_EVALUATION_INDEX);
149             classExcelCellMap2DAO.setCellEvaluationString(ExcelDAO_Constants.ERROR_EVALUATION_STRING);               
150             return(MainConstants.ERROR_RUN);
151         }
152         // check for "SUM" string
153         if(formulaAllCapsString.indexOf(ExcelDAO_Constants.CELL_FUNCTION_SUM_STRING) > 0){
154             classExcelCellMap2DAO.setCellRowEvaluationIndex(ExcelDAO_Constants.FORMULA_SUM_ROW_CELLS_INDEX);
155             classExcelCellMap2DAO.setCellEvaluationString(ExcelDAO_Constants.FORMULA_EVALUATION_STRING);               
156             return(MainConstants.SUCCESS);            
157         }
158         // check for "MAX" string
159         if(formulaAllCapsString.indexOf(ExcelDAO_Constants.CELL_FUNCTION_MAX_STRING) > 0){
160             classExcelCellMap2DAO.setCellRowEvaluationIndex(ExcelDAO_Constants.FORMULA_MAX_CELLS_INDEX);
161             classExcelCellMap2DAO.setCellEvaluationString(ExcelDAO_Constants.CELL_FUNCTION_MAX_STRING);               
162             return(MainConstants.SUCCESS);            
163         }
164         // check for "MIN" string
165         if(formulaAllCapsString.indexOf(ExcelDAO_Constants.CELL_FUNCTION_MIN_STRING) > 0){
166             classExcelCellMap2DAO.setCellRowEvaluationIndex(ExcelDAO_Constants.FORMULA_MIN_CELLS_INDEX);
167             classExcelCellMap2DAO.setCellEvaluationString(ExcelDAO_Constants.CELL_FUNCTION_MIN_STRING);               
168             return(MainConstants.SUCCESS);            
169         }
170         // check for "Average" string
171         if(formulaAllCapsString.indexOf(ExcelDAO_Constants.CELL_FUNCTION_AVERAGE_STRING) > 0){
172             classExcelCellMap2DAO.setCellRowEvaluationIndex(ExcelDAO_Constants.FORMULA_AVERAGE_CELLS_INDEX);
173             classExcelCellMap2DAO.setCellEvaluationString(ExcelDAO_Constants.CELL_FUNCTION_AVERAGE_STRING);               
174             return(MainConstants.SUCCESS);            
175         }                  
176         classExcelCellMap2DAO.setCellRowEvaluationIndex(ExcelDAO_Constants.ERROR_EVALUATION_INDEX);
177         classExcelCellMap2DAO.setCellEvaluationString(ExcelDAO_Constants.ERROR_EVALUATION_STRING);               
178         return(MainConstants.ERROR_RUN);
179     }
180 }
181