﻿<?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="60">
      <Name>CopyNewPropertyValueToAnotherProperty</Name>
      <Description>Copy the new value of Phone Number property into property Phone of the same object, if it is empty.</Description>
      <Scripts>
        <Script id="138">
          <Name>CopyNewPropertyValueToAnotherProperty</Name>
          <Code>using System;
using System.Collections.Generic;
using System.Data;
using Microsoft.Data.SqlClient;
using Alvao.Apps.API;
using Dapper;

class ObjectPropertyAutoAction : IObjectPropertyAutoAction
{
    public Tuple&lt;bool, string&gt; OnObjectPropertyModifying(SqlConnection con, int propertyId, int personId, string newValue)
    {
        throw new NotImplementedException();
    }

    public void OnObjectPropertyModified(SqlConnection con, int propertyId, int personId)
    {
        string phoneValue = "";
        int phonePropertyId = 0;
        string telephoneNumber = "";
        string loadedPropertyName = "";
        int objectId = 0;
        Dictionary&lt;string, string&gt; properties = new Dictionary&lt;string, string&gt;(); // The dictionary of properties to update in database.   
    
        // Was the phone number edited? 
        (loadedPropertyName, objectId, telephoneNumber) = con.QueryFirstOrDefault&lt;(string, int, string)&gt;(@"
            select k.txtName as result, p.lintNodeId as objectId, p.txtValue as telephoneNumber
            from tblProperty p join tblKind k on k.intKindId = p.lintKindId
            where p.intPropertyId=@propertyId",
            new { propertyId });

        // Get the current Phone property value.
        if (loadedPropertyName.Equals(Settings.UpdatedPropertyName))
        {
            (phoneValue, phonePropertyId) = con.QueryFirstOrDefault&lt;(string, int)&gt;(@"
                select p.txtValue as newValue, p.intPropertyId as propertyId
                from tblProperty p
                join tblKind k on k.intKindId = p.lintKindId
                join tblNode n on n.intNodeId = p.lintNodeId
                where n.intNodeId=@objectId and k.txtName=@txtName",
                new { objectId, txtName = Settings.PropertyKindName }); 

            // If there was no value in the Phone property, it will be added.
            if (phonePropertyId != 0 &amp;&amp; string.IsNullOrEmpty(phoneValue))
            {
                properties.Add(Settings.PropertyKindName, telephoneNumber);

                Alvao.API.AM.ObjectProperty.Update(personId, objectId, properties);
            }
        }
    }
}
</Code>
          <IsLibCode>false</IsLibCode>
        </Script>
        <Script id="139">
          <Name>Settings</Name>
          <Code>public class Settings 
{
    /*
     * The action to copy the new value of Phone Number property into property Phone of the same object, if it is empty.
     */

    public static readonly string UpdatedPropertyName = "Phone number"; // Which property value was updated.
    public static readonly string PropertyKindName = "Phone"; // Which property kind will be updated.
}</Code>
          <IsLibCode>true</IsLibCode>
        </Script>
      </Scripts>
    </Application>
  </Applications>
</AlvaoApplication>