package jsonPackage;

import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import main_package.DataBaseConnection;
import main_package.DataBaseMethodsHemant;
import main_package.UserInfo;

@ManagedBean(name = "myNidhiShareholderList")
@ViewScoped

public class MyNidhiShareholderListJson 
{
    private String json;

    
    
    public MyNidhiShareholderListJson() 
    {
        JSONObject mainobj = new JSONObject();
        JSONArray mainArray = new JSONArray();

        DataBaseMethodsHemant dbn = new DataBaseMethodsHemant();
        
        Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        String branchId = params.get("branchId");
        String dsacode = params.get("dsacode");
        String center = params.get("center");
        
        if (dsacode == null) 
        {
        	dsacode = "all";
		}
        
        if (branchId == null) 
        {
        	branchId = "-1";
		}
        
        if (center == null) 
        {
        	center = "all";
		}

        Connection conn = DataBaseConnection.JAVAConnection();

        try
        {
            ArrayList<UserInfo> list = dbn.allShareHolders(Integer.parseInt(branchId), dsacode, conn, center);

            for(UserInfo uu : list)
            {
                JSONObject obj = new JSONObject();

                obj.put("name", uu.getFirst_applicantName());
                obj.put("fatherName", uu.getFather_husband_name());
                obj.put("mobile", uu.getContactNo());
                obj.put("memberId", uu.getShareHolderNo());
                obj.put("address", uu.getAddress());

                mainArray.add(obj);
            }

            mainobj.put("status", "success");
            mainobj.put("shareholderList", mainArray);
        }
        catch(Exception e)
        {
            e.printStackTrace();

            mainobj.put("status", "error");
            mainobj.put("message", e.getMessage());
        }

        json = mainobj.toJSONString();
    }
    
    

    public void renderJson() throws IOException 
    {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext externalContext = facesContext.getExternalContext();

        externalContext.setResponseContentType("application/json");
        externalContext.setResponseCharacterEncoding("UTF-8");

        externalContext.getResponseOutputWriter().write(json);

        facesContext.responseComplete();
    }
    
    

    public String getJson() {
        return json;
    }

    public void setJson(String json) {
        this.json = json;
    }
    
}