//////////////////////////////////////////////////////////////////////////////////////////////////
//  function: cmclientdlg::ondeleteitemlistparam()
//
//  parameters: (nmhdr* pnmhdr, lresult* presult) 
//
//  description: for each row of list, it calls the release 
//
//  returns: void
//
//////////////////////////////////////////////////////////////////////////////////////////////////
void cmclientdlg::ondeleteitemlistparam(nmhdr* pnmhdr, lresult* presult) 
{
// we have to release lparam that i filled with object of isoapmapper
    nmlistview   *tempvar = (nmlistview*)pnmhdr;;
    if (reinterpret_cast <iunknown*>(tempvar->lparam))
        (reinterpret_cast <iunknown*>(tempvar->lparam))->release();
    
    *presult = 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//  function: cmclientdlg::ondeleteitemtree()
//
//  parameters: (nmhdr* pnmhdr, lresult* presult)
//
//  description: for each tree elements, it calls the release method
//  returns: void
//
//////////////////////////////////////////////////////////////////////////////////////////////////
void cmclientdlg::ondeleteitemtree(nmhdr* pnmhdr, lresult* presult) 
{
    // we have to release lparam that i filled with object
    nmtreeview   *tempvar = (nmtreeview*)pnmhdr;;
    if (reinterpret_cast <iunknown*>(tempvar->itemold.lparam))
        (reinterpret_cast <iunknown*>(tempvar->itemold.lparam))->release();
    *presult = 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//  function: cmclientdlg::onselchangedtree()
//
//  parameters: (nmhdr* pnmhdr, lresult* presult)
//
//  description: for selection on tree, it updates the list
//
//  returns: void
//
//////////////////////////////////////////////////////////////////////////////////////////////////
void cmclientdlg::onselchangedtree(nmhdr* pnmhdr, lresult* presult) 
{
    // if the selected is operation, update the list with its parameters
    nmtreeview* pnmtreeview = (nmtreeview*)pnmhdr;
    iunknown *punk = reinterpret_cast<iunknown *>(pnmtreeview->itemnew.lparam);
    if (! punk)
        return;
    iwsdloperation *poper = 0;
    m_strparameter.empty();
    updatedata(false);
    if(succeeded(punk->queryinterface(__uuidof(iwsdloperation), reinterpret_cast<void **>(&poper))))
    {
        if (updatelist() != 1)
            msg("parameter list can not be created!");
    }
    *presult = 0;
cleanup:
    if (poper)
        poper->release();
    
    return;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: cmclientdlg::onload()
//
//  parameters: no parameters
//
//  description:  takes the service, ports and operations and fills the tree
//
//  returns: void
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
void cmclientdlg::onload() 
{
    uses_conversion;
    updatedata(true);
// chech if wsdl file is given, if not, return
    if (checkforurl() == -1)
        return;
// delete the tree if exist, if a tree exist and cant be deleted, return
    if (!destroytree())
        return;
    hresult                            hr                        =    s_ok;
    bstr                            bstrwsdlfilename        =   0;
    bstr                            bstrservicename            =    0;
    bstr                            bstrportname            =    0;
    bstr                            bstroperationname        =    0;
    int                             flag                    =   1;
    int                             flag_service            =   0;
    int                             flag_port               =   0;
    int                             flag_operation          =   0;
    ccomptr<ienumwsdlservice>        pienumwsdlservices;
    ccomptr<ienumwsdlports>            pienumwsdlports;
    ccomptr<ienumwsdloperations>    pienumwsdlops;
    ccomptr<iwsdloperation>            pioperation; 
    ccomptr<iwsdlreader>            piwsdlreader;
    ccomptr<iwsdlservice>            piwsdlservice;
    ccomptr<iwsdlport>                piwsdlport;
    long                            cfetched;
    htreeitem                        hservice;
    htreeitem                        hport;
    htreeitem                        hoperation;
    // take the name of wsdl file
    bstrwsdlfilename =  m_strurl.allocsysstring();
    if (bstrwsdlfilename == null)
        return;
    hr = cocreateinstance(__uuidof(wsdlreader), null, clsctx_inproc_server, __uuidof(iwsdlreader),
                        (void**)&piwsdlreader);
    check_hresult(hr, "can not create the  object of the clsid_wsdlreader");
    // loading needs wsdl and wsml files, but i dont know wsml file and i pass ""
    hr = piwsdlreader->load(bstrwsdlfilename, l"");    
       check_hresult(hr, "loading wsdl and wsml files failed!");
    // get soap service
    hr = piwsdlreader->getsoapservices(&pienumwsdlservices);
    check_hresult(hr, "can not get services");
    if (!pienumwsdlservices)
        msg("can not get services");
    while((hr = pienumwsdlservices->next(1, &piwsdlservice, &cfetched)) == s_ok)
    {
        // at least one time this loop should go inside; if it does not, the flag wont be updated
        // so we can not continue and should destroy the tree if any part created
        flag_service   =   1;
        // get service name
        hr = piwsdlservice->get_name(&bstrservicename);
        check_hresult(hr, "can not get service names");
        // add the name of service in to tree
        // first field is null, it means insert this as root
        hservice= addtotree(null,tvi_sort,w2a(bstrservicename),tvif_text,piwsdlservice);
        ::sysfreestring(bstrservicename);
        if (!hservice)
        {
            flag = 0;
            goto cleanup;
        }
        hr = piwsdlservice->getsoapports(&pienumwsdlports);
        check_hresult(hr, "can not get ports");
        if (!pienumwsdlports)
            msg("can not get ports");
        while((hr = pienumwsdlports->next(1,&piwsdlport, &cfetched)) == s_ok)
        {
            // at least one time this loop should go inside; if it does not, the flag wont be updated
            // so we can not continue and should destroy the tree if any part created
            flag_port  =   1;
             // get port name
            hr = piwsdlport->get_name(&bstrportname);
            check_hresult(hr, "can not get port names");
            // add to tree but as a child of service
            hport= addtotree(hservice,tvi_sort,w2a(bstrportname),tvif_text,piwsdlport);
            ::sysfreestring(bstrportname);
            if (!hport)
            {
                flag = 0;
                goto cleanup;
            }
            hr = piwsdlport->getsoapoperations(&pienumwsdlops);
            check_hresult(hr, "can not get operations");
            if (!pienumwsdlops)
                msg("can not get operations");
            while((hr = pienumwsdlops->next(1,&pioperation, &cfetched)) == s_ok)
            {
             // at least one time this loop should go inside; if it does not, the flag wont be updated
             // so we can not continue and should destroy the tree if any part created
                flag_operation  =   1;
                hr = pioperation->get_name(&bstroperationname);
                check_hresult(hr, "can not get operation names");
                hoperation= addtotree(hport,tvi_sort,w2a(bstroperationname),tvif_text,pioperation);
                ::sysfreestring(bstroperationname);
                if (!hoperation)
                {
                    flag = 0;
                    goto cleanup;
                }
                // we do release by assigning to 0
                pioperation= 0;
            }
            if (flag_operation == 0)
            {
                flag =0;
                msg("could not load  operations!");
            }
            //// we do release by assigning to 0
            piwsdlport = 0;
        }
        if (flag_port == 0)
        {
            flag =0;
            msg("could not load  ports!");
        }
        //// we do release by assigning to 0
        piwsdlservice = 0;
    }
    
    if (flag_service == 0)
    {
        flag =0;
        msg("could not load  service!");
    }
    updatedata(false);
cleanup:
    ::sysfreestring(bstrwsdlfilename);
       ::sysfreestring(bstrservicename);
    ::sysfreestring(bstrportname);
    ::sysfreestring(bstroperationname);
    if (flag == 0)
        destroytree();
    return;
}