<?xml version="1.0" encoding="utf-8"?>
<AlvaoApplication xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" ModelVersion="1">
  <Applications>
    <Application id="45">
      <Name>Ticket Show Link Diagram</Name>
      <Description>If the selected ticket is in the defined service, the Ticket page will display the CMDB diagram tab with the linked object.</Description>
      <UniqueId>81413910-1eaa-4455-a797-057c7acd6796</UniqueId>
      <Version>2</Version>
      <AdvancedSettings />
      <Scripts>
        <Script id="95">
          <Name>ShowLinkDiagram</Name>
          <Code>using System;
using System.Linq;
using System.Globalization;
using Alvao.Context;
using Alvao.Apps.API;
using Alvao.API.AM;
using Alvao.API.SD;
using Alvao.API.Common;
using Alvao.API.Common.Model.Database;
using Alvao.API.Common.Model.CustomApps;
using Dapper;

public class EntityTab : IEntityTab
{
    public string Id {get; set;}
    public Entity Entity {get; set;}

    private const string TabName = "CMDB diagram";
    private const string ActivationColumnName = "ShowLinkDiagramActive";

    public EntityTab() 
    {
        Id = "TicketShowLinkDiagram";
        Entity = Entity.Request;
    }

    public EntityTabShowResult Show(int entityId, int personId)
    {    
        bool show = false;
        string name = TabName;
        string url = ""; // Url to show in the ticket tab.

        var ticket = Ticket.GetById(entityId);
        var listOfNodes = Relation.GetLinkedNodes(entityId);

        if (isServiceSupported(ticket.liHdTicketHdSectionId) &amp;&amp; // Check for a supported services.
            listOfNodes.Any() &amp;&amp; // Ticket has some related objects.
            PersonRights.IsSectionRoleMemberForTicket(personId, entityId, PersonRights.SectionRoles.Solver | PersonRights.SectionRoles.MainSolver | PersonRights.SectionRoles.Manager) &amp;&amp; // User is a member of the solver team for that service.
            PersonRights.HaveAnyAssetManagementRole(personId, false) &amp;&amp; // User has ANY AM role -&gt; can see objects.                                 
            PersonRights.HaveGlobalRole(personId, PersonRights.GlobalRoles.AssetAdministrators | PersonRights.GlobalRoles.AssetRelationsManagers | PersonRights.GlobalRoles.AssetRelationsReaders, false)) // Can see CMDB diagram.            	
        {
            string waUrl = DbProperty.WebAppUrl; // WebApp address.

            if (!string.IsNullOrEmpty(waUrl))
            {
                int objectId = 0;
                using (var scope = AlvaoContext.GetConnectionScope())
                {
                    try
                    {
                        // Find object that is linked to the ticket and is not removed.
                        objectId = scope.Connection.ExecuteScalar&lt;int&gt;(@"
                            SELECT TOP 1 NodeId FROM TicketNode TN
                            LEFT JOIN tblNode N ON N.intNodeId = TN.NodeId
                            WHERE TN.TicketId = @ticketId and TN.Removed is null and N.IsRemoved = 0", 
                        new { ticketId = entityId }, scope.LegacyTransaction);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    }
                }

                if (objectId &gt; 0 &amp;&amp; ObjectRight.CheckForUser(personId, objectId, Alvao.API.AM.Model.ObjectRight.Right.CanObjectRead))
                {
                    // Vlastní formulář
                    string diagram = "/LinkDiagram/Index/" + objectId;
                    url = waUrl + diagram;
                    show = true;
                }
            }
        }

        return new EntityTabShowResult (show, name, url);
    }

    private bool isServiceSupported(int sectionId)
    {
        return CustomColumn.GetValue("tHdSectionCust", ActivationColumnName, sectionId, new CultureInfo(1033), DbProperty.DefaultTimeZone) == "1";
    }
}</Code>
          <IsLibCode>false</IsLibCode>
          <Codesign>QFaLv/lUzYfpI9l9YkempGZHvCOFmYIWb7QSty7zvEKDDDAysL7NanoDxAL1/emcd/CQrZW9Wu/6LbwM03RwB/ibBLl3fpKru924tYxRAeyYJTnI4qKre4MvcjYRRK5u5ToS8+HzsYhn/EpfVyJWCQbWY/aaakSyNzmFQzgM1NLzwoqcPOQRzCxbA3WF6td0o6LpKs7JAy8uzsRYOu225SCxx9hasGJyDlUndUdxqJBEohQe8Ke3OQQpIjxSVucRlM7VYDmFTz0277ceMexM3yI9gdRlyF5IbrdzRGet7Stuv7jztWAkjP8bYOuofzh4MEFo3qbtreqvA1QUDP5plA==</Codesign>
        </Script>
      </Scripts>
    </Application>
  </Applications>
</AlvaoApplication>