﻿<?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="126">
      <Name>TicketShowLinkDiagram</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>
      <Scripts>
        <Script id="258">
          <Name>Settings</Name>
          <Code>public class Settings
{
    public const string TabName = "CMDB diagram";
    public static int[] SupportedServices = {  }; // List of supported services separated by a comma. Example: { 1, 2 }
}</Code>
          <IsLibCode>true</IsLibCode>
        </Script>
        <Script id="259">
          <Name>ShowLinkDiagram</Name>
          <Code>using System;
using System.Linq;
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;}

    public EntityTab() 
    {
        Id = Settings.TabName.GetHashCode().ToString();
        Entity = Entity.Request;
    }

    public EntityTabShowResult Show(int entityId, int personId)
    {    
        bool show = false;
        string name = Settings.TabName;
        string url = ""; // Url to show in the ticket tab.

        var ticket = Ticket.GetById(entityId);
        var listOfNodes = Relation.GetLinkedNodes(entityId);

        if (Settings.SupportedServices.Contains(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);
    }
}
</Code>
          <IsLibCode>false</IsLibCode>
        </Script>
      </Scripts>
    </Application>
  </Applications>
</AlvaoApplication>