Subversion Repositories SmartDukaan

Rev

Rev 308 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.utils;

import in.shop2020.datalogger.MsgType;
import in.shop2020.thrift.clients.DataLoggingClient;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.thrift.TException;

public class DataLogEntry {
        
        public static enum LogKeys{
                USERID,
                IP,
                STATUS,
                ITEMID,
                CARTID,
                PAYMENT_ID,
                TRANSACTION_ID
        }
        
        private final MsgType type;

        private final Map<String, String> values;
        
        public DataLogEntry(MsgType type){
                values =  new HashMap<String, String>();
                this.type = type;
        }
        
        public DataLogEntry(MsgType type, String userId, String ip){
                values =  new HashMap<String, String>();
                values.put(LogKeys.USERID.toString(), userId);
                values.put(LogKeys.IP.toString(), ip);
                this.type = type;
        }
        
        public DataLogEntry(MsgType type, long userId, String ip){
                values =  new HashMap<String, String>();
                values.put(LogKeys.USERID.toString(), userId+"");
                values.put(LogKeys.IP.toString(), ip);
                this.type = type;
        }
        
        public void addLogUnit(String key, String value){
                values.put(key, value);
        }
        
        public void log(){
                String s = new String();
                
                for(Entry<String, String> e: values.entrySet()){
                        s += e.getKey()+"="+e.getValue();
                }
                s+= "\n";
                
                submit(s);
        }
        
        private void submit(String message){
                
                try {
                        DataLoggingClient client = new DataLoggingClient();
                        
                        client.getClient().log(type, message);
                } catch (TException e) {
                        
                        e.printStackTrace();
                } catch (Exception e) {
                        
                        e.printStackTrace();
                }       
        }
}