﻿<?xml version="1.0"?>
<AlvaoApplication xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ModelVersion="1">
  <Applications>
    <Application id="30">
      <Name>Set Computer Detection Profile</Name>
      <Description>When you move a computer from the Storage room, a detection profile named "Standard" is automatically set for the computer.</Description>
      <Scripts>
        <Script id="58">
          <Name>SetComputerDetectionProfile</Name>
          <Code>using System;
using System.Data;
using Microsoft.Data.SqlClient;
using Alvao.Apps.API;
using Dapper;

class ObjectMoveAutoAction : IObjectMoveAutoAction
{
    public Tuple&lt;bool, string&gt; OnObjectMoving(SqlConnection con, int objectId, int newParentObjectId, int personId)
    {
        throw new NotImplementedException();
    }

    public void OnObjectMoved(SqlConnection con, int objectId, int  oldParentObjectId, int personId)
    {
        var movedObject = Alvao.API.AM.Object.GetById(personId, objectId);
        int detectProfileId;

        // Is the moved object computer?
        if (movedObject.lintClassId.HasValue &amp;&amp; 
            movedObject.lintClassId.Value == Settings.movedObjectClassIdToCheck)
        {
            // Extracting the detection profile by its name.
            detectProfileId = con.QueryFirstOrDefault&lt;int&gt;(@"SELECT id from DetectProfile WHERE ProfileName = @detectProfileName", 
                new { detectProfileName = Settings.detectionProfileName });
            if (detectProfileId &lt; 1)
                return;

            // Has computer already the detection profile set up? If so, end the action itself.
            if (movedObject.DetectProfileId.HasValue &amp;&amp; movedObject.DetectProfileId.Value == detectProfileId)
                return;

            // Was the object in the storage room? If so and it did not have a detection profile named Standard, assign it to him.
            if (Alvao.API.AM.Object.IsDescendantOf(oldParentObjectId, Settings.oldParentobjectClassId))
            { 
                con.Execute(@"UPDATE tblNode
                    SET DetectProfileId = @profileId
                    WHERE intNodeId = @nodeId",
                    new { profileId = detectProfileId, nodeId = movedObject.intNodeId });
            }
        } 
    }
}</Code>
          <IsLibCode>false</IsLibCode>
        </Script>
        <Script id="64">
          <Name>Settings</Name>
          <Code>public class Settings 
{
    /*
     * When you move a computer from the Storage room, a detection profile named "Standard" is automatically set for the computer.
     */

    public static readonly int movedObjectClassIdToCheck = (int)Alvao.API.AM.Model.ObjectClass.Code.Computer; // Object class ID of the moved object.
    public static readonly int oldParentobjectClassId = (int)Alvao.API.AM.Model.ObjectClass.Code.Warehouse; // Was moved object somewhere under the object of this Object class ID?
    public static readonly string detectionProfileName = "Standard"; // Name of the detection profile in database.
}</Code>
          <IsLibCode>true</IsLibCode>
        </Script>
      </Scripts>
    </Application>
  </Applications>
</AlvaoApplication>