package com.oracle.ucm.utils.response;

import com.oracle.ucm.generic.Field;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import oracle.stellent.ridc.model.DataBinder;
import oracle.stellent.ridc.model.DataObject;

public class ucmResponse
{
    private String m_statusCode = null;
    private String m_statusMsg = null;
    private HashMap m_metafields = null;
    public ucmResponse() {
        super();
    }
    
    public ucmResponse(List<Field> flist) 
    {
        m_metafields = new HashMap();
        for (Field curfield:flist)
        {
            String cname = curfield.getName();
            String cval = curfield.getValue();
            if ((cname == null) || (cname.length()==0) || (cval == null) || (cval.length()==0))
                continue;
            if ("StatusCode".equals(cname))
            {
                m_statusCode = cval;
            }            
            else if ("StatusMessage".equals(cname))
            {
                m_statusMsg = cval;    
            }
            else 
            {
                m_metafields.put(cname, cval);
            }
            
            
        } 
        
        if (m_statusCode == null)
        {
            m_statusCode = "0";
            m_statusMsg = "Success";
        }
    }
    
    public ucmResponse(DataBinder binder) 
    {
        m_metafields = new HashMap();
        DataObject ldo = binder.getLocalData();
        Set dbkeys =  ldo.keySet();
        Iterator kit = dbkeys.iterator();
        while (kit.hasNext())
        {
            String cname = (String)kit.next();
            String cval = (String)ldo.get(cname);
            if ((cname == null) || (cname.length()==0) || (cval == null) || (cval.length()==0))
                continue;
            if ("StatusCode".equals(cname))
            {
                m_statusCode = cval;
            }            
            else if ("StatusMessage".equals(cname))
            {
                m_statusMsg = cval;    
            }
            else 
            {
                m_metafields.put(cname, cval);
            }   
        } 
        
        if (m_statusCode == null)
        {
            m_statusCode = "0";
            m_statusMsg = "Success";
        }
    }
    
    public String getStatusCode() {
        return m_statusCode;
    }
    
    public String getStatusMsg() {
        return m_statusMsg;
    }
    
    public HashMap getMetaFields() {
        return m_metafields;
    }
    
    public void setStatusCode(String code) {
        m_statusCode = code;
    }
}
