Quintiq file version 2.0
#parent: #root
Method EditKBBranch (
  Scenario scenario,
  String kbname
) id:Method_ApplicationLibMacroPlanner_EditKBBranch
{
  #keys: '[11122.0.58944389]'
  Body:
  [*
    //Edit a kb branch with active dataset
    componentmds := ComponentMDS::CreateForLoadedMDSsOnly( Application );
    mdsinstance := selectobject( componentmds, ComponentMDSKinds.ComponentMDSInstances, m, m.MDSID().Equals( scenario.DatasetMDSID() ) );
    mdshandle := mdsinstance.GetMDSHandle();
    
    kbinfo := null( MDSObjectKB );
    if ( scenario.istype( ScenarioMP ) )
    {
      kbinfo := select( mdsinstance.GetMDSHandle().AsMacroPlan().MDSMacroPlan(), MDSKBs, kb, kb.Name().ToUpper() = kbname.ToUpper() );
    }
    
    
    
    kbfullid := KBFullID::Create( kbinfo.Domain(), kbinfo.Name(), kbinfo.Branch(), kbinfo.Version(), kbinfo.ChangeNr() );
    editor := CKBEditor::Create();
    ckbhandle := editor.LoadNewestKBChange( kbfullid );
    
    // lock
    editor.LockKB( ckbhandle.CKB().ID(), mdshandle );
    
    // edit
    dlg := construct( dlgKnowledgeBaseEditor );
    result := dlg.EditKnowledgeBase( mdshandle, ckbhandle, this );
    if ( result > 0 and dlg.Data().NeedsSave() )
    {
      // save
      editor.SaveMDSKB( mdshandle, kbfullid.KBID(), 'Edit branch' );
    
      // delete old KB
      infos := editor.GetKBStructure( kbfullid.KBID() );
      traverse( infos, Elements.KBFullID, i, i.Branch() = kbinfo.Branch() and i.ChangeNr() < kbinfo.ChangeNr() )
      {
        editor.DeleteKBChange( i );
      }
    }
    else
    {
      editor.UnlockKB( ckbhandle.CKB().ID(), mdshandle );
    }
    
    //give some time for kbinfo.ChangeNr() to get updated
    Kernel.Sleep( 1000 );
    ScenarioManager.UpdateScenarioKBInfo( scenario, kbinfo.Name(), kbinfo.Branch(), kbinfo.ChangeNr(), scenario.State(), scenario.KBDescription() )
  *]
}
