﻿<?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="75">
      <Name>Check Object Mandatory Properties</Name>
      <Description>Objects that do not have all the mandatory properties filled in cannot be moved from the Storage room to another location in the object tree.</Description>
      <Scripts>
        <Script id="159">
          <Name>CheckObjectMandatoryProperties</Name>
          <Code>using System;
using System.Data;
using Microsoft.Data.SqlClient;
using Alvao.Apps.API;

class ObjectMoveAutoAction : IObjectMoveAutoAction
{
    public Tuple&lt;bool, string&gt; OnObjectMoving(SqlConnection con, int objectId, int newParentObjectId, int personId)
    {
        var objectToMove = Alvao.API.AM.Object.GetById(personId, objectId);

	    // Is any value for mandatory properties missing?
        if (objectToMove.RequiredPropertyAlert == true)
        {
		    // If the object being moved was in the Storage room and some mandatory properties are not filled in, show an error.
            if (Alvao.API.AM.Object.IsDescendantOf(objectId, Settings.ObjectClassToCheckId)
                &amp;&amp; (newParentObjectId == 0 || !Alvao.API.AM.Object.IsDescendantOf(newParentObjectId, Settings.ObjectClassToCheckId)))
            {
                return Tuple.Create(false, Settings.ErrorMessage);
            }
        }

        return Tuple.Create(true, (string)null);
    }

    public void OnObjectMoved(SqlConnection con, int objectId, int  oldParentObjectId, int personId)
    {
        throw new NotImplementedException();
    }
}</Code>
          <IsLibCode>false</IsLibCode>
        </Script>
        <Script id="160">
          <Name>Settings</Name>
          <Code>public class Settings
{
    /*
     * Objects that do not have all the mandatory properties filled in cannot be moved from the Storage room to another location in the object tree.
     */

    public static readonly int ObjectClassToCheckId = (int)Alvao.API.AM.Model.ObjectClass.Code.Warehouse; // Object class ID.
    public static readonly string ErrorMessage = "Fill in all mandatory properties of the object before moving from the storage room."; // Error message.
}</Code>
          <IsLibCode>true</IsLibCode>
        </Script>
      </Scripts>
    </Application>
  </Applications>
</AlvaoApplication>