﻿<?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="36">
      <Name>ObjectKnowledgeBase</Name>
      <Description>In the Object page, adds the Knowledge base tab displaying knowledge base articles that contain the manufacturer and model of the object.</Description>
      <Scripts>
        <Script id="105">
          <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, "TabName", "Báze znalostí"),
            new LocalizationItem(1031, "TabName", "Wissensdatenbank"),
            new LocalizationItem(1033, "TabName", "Knowledge base"),            
            new LocalizationItem(1045, "TabName", "Baza wiedzy"),
            new LocalizationItem(1051, "TabName", "Báza znalostí")
        };
    } }

    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="106">
          <Name>ObjectKnowledgeBaseTab</Name>
          <Code>using System;
using System.Data;
using Alvao.Global;
using Alvao.API.Common;
using Alvao.API.Common.Model.CustomApps;
using Alvao.Apps.API;
using Alvao.Context;
using Dapper;

public class ObjectKnowledgeBaseTab : IEntityTab
{
    public string Id { get; set; }
    public Entity Entity { get; set; }

    public ObjectKnowledgeBaseTab()
    {
        Id = "ObjectKnowledgeBase".GetHashCode().ToString();
        Entity = Entity.Object;
    }

    public EntityTabShowResult Show(int entityId, int personId)
    {
        bool show = false;
        string name = Localization.GetLocalization(personId, "TabName");
        string url = string.Empty;

        try
        {
            using (var scope = AlvaoContext.GetConnectionScope())
            {
                //Retrieving the required object data from the database.
                var objectModel = scope.Connection.QueryFirstOrDefault&lt;ObjectKnowledgeBaseModel&gt;(@"
                    select
                        [16] Manufacturer,
                        [96] Model
                    from (
                        select p.txtValue, k.intKindCode from tblProperty p
                        join tblKind k on k.intKindId = p.lintKindId and k.intKindCode in (16, 96)
                        where p.lintNodeId = @objectId
                    ) d
                    pivot
                    (
                        max(txtValue)
                        for intKindCode in ([16], [96])
                    ) piv;", new { objectId = entityId }
                );

                if (objectModel == null)
                {
                    return new EntityTabShowResult(show, name, url);
                };

                //Check if the object has filled properties.
                if (!string.IsNullOrEmpty(objectModel.Manufacturer) || !string.IsNullOrEmpty(objectModel.Model))
                {
                    string webAppUrl = DbProperty.WebAppUrl;
                    if (!string.IsNullOrEmpty(webAppUrl))
                    {
                        //Address of tab with articles from knowledge base
                        url = webAppUrl + "/KnowledgeBase?searchedText=" + objectModel.Manufacturer + " " + objectModel.Model;
                        show = true;
                    }
                }
            }
        }
        catch
        {
            return new EntityTabShowResult(show, name, url);
        }

        return new EntityTabShowResult(show, name, url);
    }
}

public class ObjectKnowledgeBaseModel
{
    public string Manufacturer { get; set; }
    public string Model { get; set; }
}
</Code>
          <IsLibCode>false</IsLibCode>
        </Script>
      </Scripts>
    </Application>
  </Applications>
</AlvaoApplication>