package com.temp;

import java.util.ArrayList;
import java.util.List;

import com.EmpBean;
import com.isomorphic.datasource.DSRequest;
import com.isomorphic.datasource.DSResponse;

public class EmployeeDMI {
	static ArrayList arraylist = new ArrayList();
	static void setData(){
		EmpBean objLwOne  = new EmpBean();
		objLwOne.setStrEmpNo(0);
		objLwOne.setStrEmpName("John");
		objLwOne.setStrEMPAddress("New York");
		arraylist.add(objLwOne);
		
		objLwOne  = new EmpBean();
		objLwOne.setStrEmpNo(1);
		
		objLwOne.setStrEmpName("Kelly");
		objLwOne.setStrEMPAddress("Washington");
		arraylist.add(objLwOne);
		
		objLwOne  = new EmpBean();
		objLwOne.setStrEmpNo(2);
		objLwOne.setStrEmpName("Bill");
		objLwOne.setStrEMPAddress("Washington");
		arraylist.add(objLwOne);
		objLwOne  = new EmpBean();
		objLwOne.setStrEmpNo(3);
		objLwOne.setStrEmpName("Micheal");
		objLwOne.setStrEMPAddress("Chicago");
		arraylist.add(objLwOne);
		
		
		objLwOne  = new EmpBean();
		objLwOne.setStrEmpNo(4);
		objLwOne.setStrEmpName("Kim");
		objLwOne.setStrEMPAddress("San Diego");
		arraylist.add(objLwOne);
		
	}
	
	public DSResponse fetchEmpTest(DSRequest iobjRequest) throws Exception {
		DSResponse oDsResponse = new DSResponse();
		if(null==arraylist || arraylist.size()==0)
			setData();
		oDsResponse.setData(arraylist);
		oDsResponse.setTotalRows(arraylist.size());
		oDsResponse.setStatus(DSResponse.STATUS_SUCCESS);
		return oDsResponse;
	}
	
	
	public ArrayList<DSResponse> updateEmpTest(ArrayList<DSRequest> iobjRequestList)
		throws Exception
		{
			ArrayList<DSResponse> oDSResponseList = new ArrayList<DSResponse>();
			@SuppressWarnings("unused")
			ArrayList<EmpBean> lobjProfileDefinitionList = new ArrayList<EmpBean>();
			
			//String strName = (String) iobjRequestList.get(0).getAttribute("strEmpName");
			int reqLength = iobjRequestList.size();
			
			for(int i =0; i<reqLength; i++){
				DSResponse dsResponse = new DSResponse();
	        	List lhmMsgData = new ArrayList();
	        	DSRequest dsRequest = iobjRequestList.get(i);
	        	String empid = dsRequest.getValues().get("strEmpNo").toString();
	        	EmpBean emp = (EmpBean) arraylist.get(Integer.parseInt(empid));
	        	
	        	if(dsRequest.getValues().containsKey("strEmpName")){
	        		emp.setStrEmpName(dsRequest.getValues().get("strEmpName").toString());
	        	}else{
	        		emp.setStrEmpName(dsRequest.getOldValues().get("strEmpName").toString());
	        	}
	        	
	        	if(dsRequest.getValues().containsKey("strEMPAddress")){
	        		emp.setStrEMPAddress(dsRequest.getValues().get("strEMPAddress").toString());
	        	}else{
	        		emp.setStrEMPAddress(dsRequest.getOldValues().get("strEMPAddress").toString());
	        	}
	        	
	        	if(dsRequest.getValues().containsKey("strEmpNo")){
	        		emp.setStrEmpNo(Integer.parseInt(dsRequest.getValues().get("strEmpNo").toString()));
	        	}else{
	        		emp.setStrEmpNo(Integer.parseInt(dsRequest.getOldValues().get("strEmpNo").toString()));
	        	}
	        	
        		
	        	lhmMsgData.add("Employee updated "+emp.getStrEmpName());
        		System.out.println("EMP NAME :: "+emp.getStrEmpName()+"EMP Address");
	        
	        	dsResponse.setErrors(lhmMsgData);	
		        dsResponse.setStatus(DSResponse.STATUS_SUCCESS);
	        	dsResponse.setData(emp);
		        		        
				oDSResponseList.add(dsResponse);
			}
			
			return oDSResponseList;
		}
	
	public ArrayList<DSResponse> saveEmpTest(ArrayList<DSRequest> iobjRequestList)
			throws Exception
			{
				ArrayList<DSResponse> oDSResponseList = new ArrayList<DSResponse>();
				@SuppressWarnings("unused")
				ArrayList<EmpBean> lobjProfileDefinitionList = new ArrayList<EmpBean>();
				
				//String strName = (String) iobjRequestList.get(0).getAttribute("strEmpName");
				int reqLength = iobjRequestList.size();
				
				for(int i =0; i<reqLength; i++){
					DSResponse dsResponse = new DSResponse();
		        	List lhmMsgData = new ArrayList();
		        	DSRequest dsRequest = iobjRequestList.get(i);
		        	String empid = dsRequest.getValues().get("strEmpNo").toString();
		        	EmpBean emp = (EmpBean) arraylist.get(Integer.parseInt(empid));
		        	
		        	if(dsRequest.getValues().containsKey("strEmpName")){
		        		emp.setStrEmpName(dsRequest.getValues().get("strEmpName").toString());
		        	}else{
		        		emp.setStrEmpName(dsRequest.getOldValues().get("strEmpName").toString());
		        	}
		        	
		        	if(dsRequest.getValues().containsKey("strEMPAddress")){
		        		emp.setStrEMPAddress(dsRequest.getValues().get("strEMPAddress").toString());
		        	}else{
		        		emp.setStrEMPAddress(dsRequest.getOldValues().get("strEMPAddress").toString());
		        	}
		        	
		        	if(dsRequest.getValues().containsKey("strEmpNo")){
		        		emp.setStrEmpNo(Integer.parseInt(dsRequest.getValues().get("strEmpNo").toString()));
		        	}else{
		        		emp.setStrEmpNo(Integer.parseInt(dsRequest.getOldValues().get("strEmpNo").toString()));
		        	}
		        	
	        		
		        	lhmMsgData.add("Employee updated "+emp.getStrEmpName());
	        		System.out.println("EMP NAME :: "+emp.getStrEmpName()+"EMP Address");
		        
		        	dsResponse.setErrors(lhmMsgData);	
			        dsResponse.setStatus(DSResponse.STATUS_SUCCESS);
		        	dsResponse.setData(emp);
			        		        
					oDSResponseList.add(dsResponse);
				}
				
				return oDSResponseList;
			}
}
