D:\CRM_Project\CRM_Metadata_Structure\NetBeamsICM_TestProject\ICM_DB_TestComponents\src\java\unstructured_data_parser\ParsingSOAP_XML_MessagesFile.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 java.io.*;
  9 import java.io.File;
 10 import java.net.URL;
 11 
 12 import java.io.ByteArrayInputStream;
 13 import java.io.InputStream;
 14 
 15 import javax.xml.soap.MessageFactory;
 16 import javax.xml.soap.MimeHeaders;
 17 import javax.xml.soap.SOAPBody;
 18 import javax.xml.soap.SOAPMessage;
 19 import javax.xml.soap.SOAPElement;  
 20 import javax.xml.soap.SOAPException; 
 21 
 22 import org.w3c.dom.Node;
 23 import org.w3c.dom.NodeList;
 24 import unstructured_data_idao.SOAP_XML_Constants;
 25 import unstructured_data_idao.SimpleInputExcelSheetDAO;
 26 
 27 import java.util.ArrayList;
 28 import java.util.StringTokenizer;
 29 import constants.MainConstants;
 30 import java.lang.reflect.Method;
 31 import java.lang.reflect.*;
 32 
 33 /**
 34  *
 35  * @author sameldin
 36  */
 37 public class ParsingSOAP_XML_MessagesFile {
 38     
 39     String folderName               = "D:/CRM_Project/CRM_Metadata_Structure/ICM_InputData/SOAP_XML_InputFiles/";
 40     String fileName                 = "SimpleInputExcelSheet_XML.xml";
 41     String xmlFolderName            = "D:\\CRM_Project\\CRM_Metadata_Structure\\ICM_InputData\\SOAP_XML_InputFiles";
 42     
 43     
 44     public ParsingSOAP_XML_MessagesFile() {
 45         
 46         try {
 47                 File inputSOAP_XMLfile = new File(folderName + fileName);
 48                 InputStream localInputStream = new FileInputStream(inputSOAP_XMLfile);
 49                 // create message factory
 50                 MessageFactory localMessageFactory = MessageFactory.newInstance();
 51                 // headers for a SOAP message
 52                 MimeHeaders localMimeHeaders = new MimeHeaders();     
 53                 localMimeHeaders.addHeader("Content-Type", "text/xml");
 54                 //=================================================
 55                 // create the SOAPMessage
 56                 SOAPMessage soapMessage = localMessageFactory.createMessage(localMimeHeaders,localInputStream);
 57                 // get the body
 58                 SOAPBody soapBody = soapMessage.getSOAPBody();
 59                 // find your node based on tag name
 60                 //NodeList nodes = soapBody.getElementsByTagName("class_information");
 61                 //int nodeListlength = nodes.getLength();
 62                 NodeList allNodeslist  = soapBody.getElementsByTagName("*");
 63                 //=====================================================
 64                 //=====================================================
 65                 System.out.println("=====================================================");
 66                 // for simplicy and testing purpose we are creating an arryList of SimpleInputExcelSheetDAO
 67                 // create the arralist and add the object to it 
 68                 ArrayList <SimpleInputExcelSheetDAO> SimpleInputExcelSheetDAOArrayList = new ArrayList <SimpleInputExcelSheetDAO>();
 69                 SimpleInputExcelSheetDAO localSimpleInputExcelSheetDAO = null;
 70                 
 71                 boolean dao_fieldsFirstRun    = true;
 72                 boolean daoClassCreatedFlag    = false;
 73                 // temp variables
 74                 Method [] allMethods = null;
 75                 Field [] allDAO_DeclaredFields = null;
 76                 Class daoClass = null;
 77                 int fieldID_Number = 0;
 78                 String fieldName = "";
 79                 String fieldType = "";
 80                 String fieldContent = "";
 81                 int index = 0;
 82                 for(index = 0; index < allNodeslist.getLength(); index++) {
 83                      Node tempNode = allNodeslist.item(index);
 84                      if(null == tempNode){
 85                         System.out.println("node = null");
 86                         continue;
 87                      }
 88                     String tempNodename =   tempNode.getNodeName();   
 89                     tempNodename = tempNodename.trim();                    
 90                     int tagProcessingIndex = getTagProcessIndex(tempNodename);
 91                     switch (tagProcessingIndex)
 92                     {
 93                         case SOAP_XML_Constants.CLASS_INFORMATION_XML_PROCESS_INDEX:
 94                            break;
 95                         case SOAP_XML_Constants.PACKAGE_NAME_XML_PROCESS_INDEX:
 96                            break;
 97                         case SOAP_XML_Constants.CLASS_PATH_XML_PROCESS_INDEX:
 98                            break;
 99                         case SOAP_XML_Constants.CLASS_NAME_XML_PROCESS_INDEX:
100                            break;
101                         case SOAP_XML_Constants.DAO_OBJECT_XML_PROCESS_INDEX:
102                                 localSimpleInputExcelSheetDAO = getDAO_Object();
103                                 SimpleInputExcelSheetDAOArrayList.add(localSimpleInputExcelSheetDAO);
104                                 daoClass = localSimpleInputExcelSheetDAO.getClass();
105                                 allMethods = daoClass.getMethods();
106 //                                allDAO_DeclaredFields = daoClass.getFields();
107                                 allDAO_DeclaredFields = daoClass.getDeclaredFields();
108                                 //Field tempField = allDAO_DeclaredFields[0];
109                                 //tempField.setAccessible(true);
110                                 //tempField
111                                 //Modifier.isPrivate
112                                 // Allow modification on the field
113                                 // field.setAccessible(true);
114                            break;
115                         case SOAP_XML_Constants.DAO_FIELDS_XML_PROCESS_INDEX:
116                             // load the vaues into the class field
117                             // set flag to load the values
118                             // use reflection to figure out the variable name and type
119                             // skip the first run
120                             if(dao_fieldsFirstRun){
121                                 dao_fieldsFirstRun    = false;
122                                 break;
123                             }
124 //                            if(null == allMethods){
125 //                                System.out.println("\nallMethods = null");
126 //                                break;
127 //                            }
128                             int count = 0;
129                             boolean nameFoundFlag = false;
130 //                            String comparisonFieldName ="set" + fieldName;
131 //                            comparisonFieldName = comparisonFieldName.toLowerCase();
132                             String comparisonFieldName = fieldName.toLowerCase();
133                             if(null == allDAO_DeclaredFields) {
134                                 
135                                 break;                                
136                             }
137                             for(count = 0; count < allDAO_DeclaredFields.length; count++) {
138                                 
139                                 Field tempFieldObject   = allDAO_DeclaredFields[count];
140                                 String tempFieldName    = tempFieldObject.getName();
141                                 tempFieldName           = tempFieldName.toLowerCase();
142                                 int stringPosition = tempFieldName.indexOf(comparisonFieldName);
143                                 if(stringPosition > -1) {
144                                     nameFoundFlag = true;
145                                     if(fieldType.equalsIgnoreCase("double"))
146                                     {
147                                         double tempDoublevalue = Double.parseDouble(fieldContent);
148                                         tempFieldObject.setAccessible(true);
149                                         tempFieldObject.setDouble(localSimpleInputExcelSheetDAO, tempDoublevalue);
150                                         tempFieldObject.setAccessible(false);
151                                     }
152                                     else
153                                     if(fieldType.equalsIgnoreCase("String")){
154                                         tempFieldObject.setAccessible(true);
155                                         tempFieldObject.set(localSimpleInputExcelSheetDAO, fieldContent);
156                                         tempFieldObject.setAccessible(false);
157                                     }                                                                       
158                             }
159 //=====================================================================                            
160 //=====================================================================                            
161 //                            for(count =0; count < allMethods.length; count++) {
162 //                             
163 //                                Method targetMethod = allMethods[count];
164 //                                String tempMethodName = targetMethod.getName();
165 //                                String comparisonMethodName = tempMethodName.toLowerCase();
166 //                                int stringPosition = comparisonMethodName.indexOf(comparisonFieldName);
167 //                                if(stringPosition > -1) {
168 //                                    nameFoundFlag = true;
169 //                                    if(fieldType.equalsIgnoreCase("double"))
170 //                                    {
171 //                                        double tempDoublevalue = Double.parseDouble(fieldContent);
172 ////                                        Object doubleObject = new Double(tempDoublevalue);
173 ////                                        Method m = daoClass.getDeclaredMethod(tempMethodName, Double.class);
174 //                                        //Method m = daoClass.getDeclaredMethod(localSimpleInputExcelSheetDAO, tempDoublevalue);
175 //                                       // m.setAccessible(true);
176 //
177 ////                                        m.invoke(daoClass, doubleObject);
178 //
179 //                                    }
180 ////                                    else
181 //                                    if(fieldType.equalsIgnoreCase("String"))
182 //                                    {
183 //                                        targetMethod.invoke(localSimpleInputExcelSheetDAO, fieldContent);
184 //                                        int gg = 0;
185 //                                    }
186 //                                    break;
187 //                                }
188 //=====================================================================                            
189 //=====================================================================                            
190                             }
191                             int vv =count;
192                             //int result = callDAO_Method();
193                             //result = setDAO_Field();
194 // Method doIt = A.class.getDeclaredMethod("doIt", int[].class, int[].class);                            
195                             //reset the field values
196                             if(nameFoundFlag){
197                                 fieldID_Number = 0;
198                                 fieldName = "";
199                                 fieldType = "";
200                                 fieldContent = "";
201                             }                            
202                             // start looking for variable name-type and set the value
203                            break;
204                         case SOAP_XML_Constants.FIELD_ID_XML_PROCESS_INDEX:
205                             String nodeValueString = tempNode.getTextContent();
206                             nodeValueString = nodeValueString.trim();
207                             fieldID_Number = Integer.parseInt(nodeValueString);
208                             // store the ID
209                            break;
210                         case SOAP_XML_Constants.FIELD_NAME_XML_PROCESS_INDEX:
211                             fieldName = tempNode.getTextContent();
212                             // store name
213                            break;
214                         case SOAP_XML_Constants.FIELD_TYPE_XML_PROCESS_INDEX:
215                             // store the type
216                             fieldType = tempNode.getTextContent();
217                            break;
218                         case SOAP_XML_Constants.FIELD_VALUE_XML_PROCESS_INDEX:
219                             // store 
220                             fieldContent = tempNode.getTextContent();
221                            break;
222                         default:
223                     }
224                     
225                
226 //System.out.print("node.getNodeName()  " + tempNode.getNodeName());
227 ////System.out.print("\tnode.getLocalName()  " + node.getLocalName());
228 //System.out.print("\tnode.getNodeValue()  " + tempNode.getNodeValue());
229 //System.out.print("\tgetTextContent()  " + tempNode.getTextContent());
230 //System.out.println("\n=====================================================");
231                      
232                 }
233 
234 
235             int stopDebugger = 0;
236             
237         }
238         catch(Exception e){
239             e.printStackTrace();
240         }
241     }
242     /*
243      * This method would create DAO based on a number of thing including instantiating from the .class folder
244     
245     */
246     SimpleInputExcelSheetDAO getDAO_Object() {
247         SimpleInputExcelSheetDAO methodlSimpleInputExcelSheetDAO = new SimpleInputExcelSheetDAO();
248         return(methodlSimpleInputExcelSheetDAO);
249     }
250     /*
251     
252     */
253     int  callDAO_Method(){
254 
255         return(MainConstants.SUCCESS);        
256     }
257     /*
258     
259     */
260     int  setDAO_Field(){
261 
262         return(MainConstants.SUCCESS);        
263     }
264     /*
265     
266     */
267     int getTagProcessIndex(String tagname) {
268         
269         int tagProcessIndex = 0;
270         int stringLength = tagname.length();
271         String last3Chars = tagname.substring((stringLength - 3), stringLength);
272         tagProcessIndex = Integer.parseInt(last3Chars);
273         return (tagProcessIndex);
274     }
275     /*
276     
277     */
278     public static void main(String[] args) throws Exception {
279         
280         ParsingSOAP_XML_MessagesFile localParsingSOAP_XML_MessagesFile = new ParsingSOAP_XML_MessagesFile();
281     }    
282 }
283