Saturday, December 12, 2015

Real Time Scenario:Read the CSV File and save Each Row into one Record in Custom Object.

1.Get CSV files from Document Object.

global class UserLogEvents implements Database.batchable<String>, Database.Stateful{
   private static final Integer SCOPE_SIZE = 100;
   public UserLogEvents (){}
   global list<Document> doc=new List<Document>();
  Blob logincontentFile;
global Iterable<String> start(Database.BatchableContext BC){  
       doc=[SELECT id,body,BodyLength,ContentType,CreatedDate,Description,DeveloperName,Name,Type FROM Document  WHERE CreatedDate >2015-11-18T16:30:02.000+0000 ];
       logincontentFile=doc[1].body;
       String[] filelines = new String[]{};
       string nameFile=logincontentFile.toString();
       return new Utility_RowIterator(nameFile,'\n');
 }
global void execute(Database.BatchableContext BC, List<string> filelines ){
    Map<id,user> userList=new map<id,user>([select id,name,Email,Division,Username from user]);
    List<LogHistory__c> accstoupload;
    accstoupload = new List<LogHistory__c>();
    for (Integer i=1;i<filelines.size();i++){
            String[] inputvalues = new String[]{};
            inputvalues = filelines[i].split(',');
           
            LogHistory__c a = new LogHistory__c();
            a.Name = inputvalues[0].remove('"');// event type
            a.USER_ID__c = inputvalues[4].remove('"'); // user id     
            a.CLIENT_IP__c = inputvalues[7].remove('"');//client ip
            a.SOURCE_IP__c = inputvalues[11].remove('"');
            a.TIMESTAMP_DERIVED__c = inputvalues[16].remove('"');
            string str=inputvalues[1].remove('"');
            string str1=str.substring(0,4)+'-'+str.substring(4,6)+'-'+str.substring(6,8)+':'+str.substring(8,10)+':'+str.substring(10,12);
            a.TIMESTAMPS__c=str1;
            system.debug('a.TIMESTAMP_DERIVED__c::::'+a.TIMESTAMP_DERIVED__c+'///'+a.TIMESTAMP__c);
            a.User_Division__c =userList.get( a.USER_ID__c).Division;
            a.User_Email__c=userList.get( a.USER_ID__c).email;
            a.User_Login_Mail__c=userList.get( a.USER_ID__c).Username;
            a.User_Name__c=userList.get( a.USER_ID__c).name;
            accstoupload.add(a);
            system.debug('accstoupload'+accstoupload);
        }
    try{
    insert accstoupload;
    }
    catch(Exception e){
    } 
 }
 global void finish(Database.BatchableContext BC){
   }
}

===============Utility_RowIterator Class Read the CSV files for big size data files============
public with sharing class Utility_RowIterator implements Iterator<String>, Iterable<String>
{
   private String m_Data;
   private Integer m_index = 0;
   private String m_rowDelimiter = '\n';

   public Utility_RowIterator(String fileData)
   {
      m_Data = fileData;
   }
   public Utility_RowIterator(String fileData, String rowDelimiter)
   {
      m_Data = fileData;
      m_rowDelimiter = rowDelimiter;
   }

   public Boolean hasNext()
   {
      return m_index < m_Data.length() ? true : false;
   }
   public String next()
   {    
      Integer key = m_Data.indexOf(m_rowDelimiter, m_index);

      if (key == -1)
        key = m_Data.length();

      String row = m_Data.subString(m_index, key);
      m_index = key + 1;
      return row;
   }
   public Iterator<String> Iterator()
   {
      return this;  
   }
}

No comments:

Post a Comment