tags:

views:

89

answers:

1

i have a requirement This ‘if’ structure must be changed. As it is currently coded the agent version is only set when the policy GUID sent by RM matches a GUID of a policy on the server. We always want to set the version regardless of whether the GUIDs match.(what ever be the situation)

here is the coding

ResourcePolicy rp = null;
try
{
  int rpindex = allObjects.Find(new Guid(policyGuid));
  if (rpindex != -1)
  {
     rp = (ResourcePolicy)allObjects.GetAt(rpindex);
  }
}
catch (System.Exception err)
{
  SpoDebug.DebugTraceSevere(func, "Bad GUID: " + policyGuid + "  Exception: " + err.Message);
  rp = null;
}

if (rp == null)  // this the if loop we need to concentrate
{
  SpoDebug.DebugTraceSevere(func, "Unable to find ResourcePolicy with GUID: " + policyGuid);
}
else
{
  // Search for the specified host
  foreach (DataModelObject dmo in allObjects)
  {
     if (dmo is IResourcePolicy)
     {
        if (string.Compare(dmo.Name, hostName, true) == 0)
        {
           IResourcePolicy irp = (IResourcePolicy)dmo;
           irp.ResourcePolicy = rp;
           irp.AgentPolicyVersion.Version = Convert.ToInt64(policyVersion);
           irp.ResourcePolicyEnabled = Convert.ToBoolean(enabled);
           irp.AgentVersion = agentVersion;

so what i did i made the assignment (irp.AgentVersion = agentVersion;) outside if loop ( if (rp == null))

like this way but i am not getting version value

foreach (DataModelObject dmo in allObjects)
{
 if (dmo is IResourcePolicy)
 {
    if (string.Compare(dmo.Name, hostName, true) == 0)
    {
     irp.AgentVersion = agentVersion;
    }

Can any one suggest me what i have to do here

A: 

I think what you mean is:

ResourcePolicy rp = null;
try
{
    int rpindex = allObjects.Find(new Guid(policyGuid));
    if (rpindex != -1)
    {
        rp = (ResourcePolicy)allObjects.GetAt(rpindex);
    }
}
catch (System.Exception err)
{
    SpoDebug.DebugTraceSevere(func, "Bad GUID: " + policyGuid + "  Exception: " + err.Message);
}

if (rp == null)  // this the if loop we need to concentrate
{
    SpoDebug.DebugTraceSevere(func, "Unable to find ResourcePolicy with GUID: " + policyGuid);
}

// Search for the specified host
foreach (DataModelObject dmo in allObjects)
{
    if (dmo is IResourcePolicy && string.Compare(dmo.Name, hostName, true) == 0))
    {
        IResourcePolicy irp = (IResourcePolicy)dmo;
        irp.AgentVersion = agentVersion;

        if (rp != null)
        {
            irp.ResourcePolicy = rp;
            irp.AgentPolicyVersion.Version = Convert.ToInt64(policyVersion);
            irp.ResourcePolicyEnabled = Convert.ToBoolean(enabled);
        }

        // ...
    }
}

I've removed the else bit so the loop always gets executed, then added if (rp != null) inside the loop that prevents some part of it from executing. That way you don't have to duplicate the loop code itself, which is what I think you were doing?

Thorarin
As per the logic this will work but that time also not able to see version,,only after assigning resourcepolicy version is coming
peter