﻿<?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="38">
      <Name>ObjectManufacturerSupport</Name>
      <Description>In the Object page, adds the Manufacturer support command that redirecting user to the manufacturer's web for the selected object based on the object properties Manufacturer, and Serial number (or BIOS serial number). Works only for Dell and Lenovo.</Description>
      <Scripts>
        <Script id="103">
          <Name>Localization</Name>
          <Code>using System.Linq;
using System.Collections.Generic;
using Microsoft.Data.SqlClient;
using Alvao.API.Common;

public class Localization 
{
    public static List&lt;LocalizationItem&gt; Localizations {get {
        return new List&lt;LocalizationItem&gt;() {
            new LocalizationItem(1029, "CommandName", "Podpora výrobce"),
            new LocalizationItem(1031, "CommandName", "Herstellerunterstützung"),
            new LocalizationItem(1033, "CommandName", "Manufacturer support"),            
            new LocalizationItem(1045, "CommandName", "Wsparcie producenta"),
            new LocalizationItem(1051, "CommandName", "Podpora výrobcu")
        };
    } }

    public static string GetLocalization(int personId, string name)
    {
        var localeId = GetPersonLocaleId(personId);
        var translation = FindInList(localeId, name);
        if (string.IsNullOrEmpty(translation))
        {
            localeId = GetDefaultLocaleId();
            translation = FindInList(localeId, name);
        }
        return translation;
    }

    private static string FindInList(int localeId, string name)
    {
        return Localizations.FirstOrDefault(x=&gt;x.LocaleId == localeId &amp;&amp; x.Name == name)?.Translation;
    }

    private static int GetDefaultLocaleId()
    {
        var locale = Person.GetCultureInfoOrDefault(new Alvao.API.Common.Model.Database.tPerson()).LCID;
        return locale;
    }

    private static int GetPersonLocaleId(int personId)
    {
        var locale = Person.GetById(personId).iPersonLocaleId;
        if (!locale.HasValue)
        {
            return GetDefaultLocaleId();
        }
        return locale.Value;
    }
}

public class LocalizationItem
{
    public int LocaleId {get; set;}
    public string Name {get; set;}
    public string Translation {get; set;}

    public  LocalizationItem(int localeId, string name, string translation)
    {
        this.LocaleId = localeId;
        this.Name = name;
        this.Translation = translation;
    }
}</Code>
          <IsLibCode>true</IsLibCode>
        </Script>
        <Script id="104">
          <Name>ManufacturerSupport</Name>
          <Code>using Alvao.API.Common.Model.CustomApps;
using Alvao.Apps.API;
using Alvao.Context;
using Dapper;

public class ManufacturerSupport : IEntityCommand 
{
    public string Id {get; set;}
    public Entity Entity {get; set;}

    public ManufacturerSupport()
    {
        Id = "Manufacturer support".GetHashCode().ToString();
        Entity = Entity.Object;
    }

    public EntityCommandShowResult Show(int entityId, int personId)
    {   
        int position = 2; 
        string icon = "Manufacturing"; 
        string name = Localization.GetLocalization(personId, "CommandName"); 
        bool show = !string.IsNullOrEmpty(GetManufacturerSupportUrl(entityId));
               
        return new EntityCommandShowResult (show, name, icon, position);
    }

    public CommandResult Run(int entityId, int personId)
    {
        MessageType messageType = MessageType.None; 
        string messageText = string.Empty;
        string navigateToUrl = GetManufacturerSupportUrl(entityId);

        return new CommandResult(messageType, messageText, navigateToUrl);
    }

    private string GetManufacturerSupportUrl(int objectId)
    {
        using (var scope = AlvaoContext.GetConnectionScope())
        {
            //Retrieving the required object data from the database.
            var objectModel = scope.Connection.QueryFirstOrDefault&lt;ManufacturerSupportObjectModel&gt;(@"
                select
                    [12] SerialNumber,
                    [16] Manufacturer,
                    [87] BiosSerialNumber
                from (
                    select p.txtValue, k.intKindCode from tblProperty p
                    join tblKind k on k.intKindId = p.lintKindId and k.intKindCode in (12, 16, 87)
                    where p.lintNodeId = @objectId
                ) d
                pivot
                (
                    max(txtValue)
                    for intKindCode in ([12], [16], [87])
                ) piv;", new { objectId }
            );

            if (objectModel == null){
                return string.Empty;
            }

            string serialNumber = string.IsNullOrEmpty(objectModel.SerialNumber) ? objectModel.BiosSerialNumber : objectModel.SerialNumber;

            //Check if the object has filled properties.
            if (!string.IsNullOrEmpty(objectModel.Manufacturer) &amp;&amp; !string.IsNullOrEmpty(serialNumber))
            {
                string url = string.Empty;

                switch (objectModel.Manufacturer.ToLower()){
                    case "dell":
                    case "dell inc.":
                        url = "https://www.dell.com/support/home/product-support/servicetag/" + serialNumber;
                        break;
                    case "lenovo":
                        url = "https://pcsupport.lenovo.com/products/" + serialNumber;
                        break;
                }

                return url;
            }

            return string.Empty;
        }
    }

    public class ManufacturerSupportObjectModel {
        public string Manufacturer {get; set;}
        public string SerialNumber {get; set;}
        public string BiosSerialNumber {get; set;}
    }
}
</Code>
          <IsLibCode>false</IsLibCode>
        </Script>
      </Scripts>
    </Application>
  </Applications>
</AlvaoApplication>