diff --git a/F4SDM42WebApi/F4SDHelperService.cs b/F4SDM42WebApi/F4SDHelperService.cs index a68fa60..38577e8 100644 --- a/F4SDM42WebApi/F4SDHelperService.cs +++ b/F4SDM42WebApi/F4SDHelperService.cs @@ -1,6 +1,13 @@ -using System; +using C4IT.F4SDM; +using C4IT.FASD.Base; +using C4IT.Logging; +using Matrix42.Common; +using Newtonsoft.Json; +using System; +using System.Collections.Specialized; using System.Collections.Generic; using System.Data; +using System.Data.SqlClient; using System.Dynamic; using System.Globalization; using System.IO; @@ -13,18 +20,9 @@ using System.Reflection; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; - -using Matrix42.Common; -using Matrix42.WebApi.Contracts; using update4u.SPS.DataLayer; using update4u.SPS.DataLayer.Transaction; - -using Newtonsoft.Json; - -using C4IT.F4SDM; -using C4IT.FASD.Base; -using C4IT.Logging; - +using static C4IT.FASD.Base.cF4SDTicket; using static C4IT.Logging.cLogManager; namespace C4IT.F4SD @@ -52,11 +50,11 @@ namespace C4IT.F4SD private const string placeHolderSubject = "PARAM_SUBJECT"; private const string placeHolderDescription = "PARAM_DESCRIPTION"; - private static bool? ticketAndServiceRequestEnabledCache; private const string c4itf4sdmonLinkBase = "f4sdsend://localhost"; private const string F4SDTicketTableName = "M42WPM-TICKETS-INFOS"; private const string F4SDTicketStatusColumnName = "STATUS"; + private static bool? ticketAndServiceRequestEnabledCache; public static IEnumerable ReadLines(Func streamProvider, @@ -73,6 +71,39 @@ namespace C4IT.F4SD } } + private static NameValueCollection ParseQueryString(string query) + { + var values = new NameValueCollection(); + query = (query ?? string.Empty).TrimStart('?'); + + if (string.IsNullOrWhiteSpace(query)) + return values; + + foreach (var pair in query.Split(new[] { '&' }, StringSplitOptions.RemoveEmptyEntries)) + { + var parts = pair.Split(new[] { '=' }, 2); + var key = WebUtility.UrlDecode(parts[0]); + var value = parts.Length > 1 ? WebUtility.UrlDecode(parts[1]) : string.Empty; + + if (!string.IsNullOrEmpty(key)) + values.Add(key, value); + } + + return values; + } + + private static string ToQueryString(NameValueCollection queryString) + { + if (queryString == null || queryString.Count == 0) + return string.Empty; + + return string.Join("&", + queryString.AllKeys + .Where(key => !string.IsNullOrEmpty(key)) + .SelectMany(key => (queryString.GetValues(key) ?? new[] { string.Empty }) + .Select(value => $"{WebUtility.UrlEncode(key)}={WebUtility.UrlEncode(value)}"))); + } + internal async Task getDirectLinkF4SD(Guid EOID, string type) { var CM = MethodBase.GetCurrentMethod(); @@ -81,7 +112,7 @@ namespace C4IT.F4SD { LogEntry($"Generating F4SD URI for type: '{type}', GUID: '{EOID}'", LogLevels.Debug); var builder = new UriBuilder(c4itf4sdmonLinkBase); - var queryString = UrlQueryBuilder.Parse(builder.Query); + var queryString = ParseQueryString(builder.Query); switch (type.ToLowerInvariant().Split('.').Last()) { @@ -140,7 +171,7 @@ namespace C4IT.F4SD break; } - builder.Query = queryString.ToString(); + builder.Query = ToQueryString(queryString); return string.IsNullOrEmpty(builder.Query) ? null : builder.ToString(); } catch (Exception E) @@ -155,7 +186,6 @@ namespace C4IT.F4SD } - [Obsolete] internal async Task> getTicketListByUser(string userSid, int hours, int queueoption, List queues) { var CM = MethodBase.GetCurrentMethod(); @@ -269,116 +299,6 @@ namespace C4IT.F4SD } } - internal async Task> getTicketListByUser(Guid userId, int hours, int queueoption, List queues) - { - var CM = MethodBase.GetCurrentMethod(); - LogMethodBegin(CM); - try - { - LogEntry($"Generating ticket list for userId: '{userId}', {hours} hours", LogLevels.Debug); - await Task.Delay(0); - - var tickets = new List(); - var activityFilter = GetActivityFilter(userId, hours, ticketAndServiceRequestEnabled(), queueoption, queues); - LogEntry($"ASql Filter: {activityFilter}"); - - var activityTable = FragmentRequestBase.SimpleLoad(SPSActivityClassBaseID, - "[Expression-ObjectID] as EOID, TicketNumber, Subject, Service.ID as ServiceId, Service.Name as ServiceName, SUBQUERY(BasicSchemaObjectType as bso, bso.Name, bso.id = base.T(SPSCommonClassBase).TypeID) as ActivityType" + - ", COALESCE(T(SPSActivityClassIncident).Asset.T(SPSComputerClassBase).Name, T(SPSActivityClassIncident).Asset.T(SPSAssetClassSIMCard).PhoneNumber, T(SPSActivityClassIncident).Asset.Name, T(SPSActivityClassIncident).Asset.objectid) as AssetName" + - ", SUBQUERY(BasicSchemaObjectType AS t, t.Name, t.ID=base.T(SPSActivityClassIncident).Asset.T(SPSCommonClassBase).TypeID) as AssetCIName", - activityFilter); - - if (activityTable?.Rows == null || activityTable.Rows.Count == 0) - { - LogEntry($"No activity entries found for userId: '{userId}'", LogLevels.Warning); - return null; - } - - for (int i = 0; i < activityTable.Rows.Count; i++) - { - DataRow entry = activityTable.Rows[i]; - var activityEOId = getGuidFromObject(entry["EOID"]); - var ticketNumber = getStringFromObject(entry["TicketNumber"]); - var activityType = getStringFromObject(entry["ActivityType"]); - var assetName = getStringFromObject(entry["AssetName"]); - var ServiceName = getStringFromObject(entry["ServiceName"]); - var assetCIName = getStringFromObject(entry["AssetCIName"]); - var subject = getStringFromObject(entry["Subject"]); - - var stateData = GetActivityState(activityEOId); - var state = stateData.Item1; - var stateDisp = stateData.Item2; - - LogEntry($"Activity found {i + 1}/{activityTable.Rows.Count}: ObjectID={activityEOId}, TicketNumber={ticketNumber}, Subject={subject}, State={state}", LogLevels.Debug); - - if (string.IsNullOrEmpty(ticketNumber)) - { - LogEntry($"No TicketNumber found for activity entry", LogLevels.Warning); - continue; - } - - if (string.IsNullOrEmpty(assetName)) - { - LogEntry($"No AssetName found for activity entry", LogLevels.Debug); - } - - if (string.IsNullOrEmpty(assetCIName)) - { - LogEntry($"No AssetCIName found for activity entry", LogLevels.Debug); - } - var ServiceId = getGuidFromObject(entry["ServiceId"]); - if (ServiceId == Guid.Empty) - { - LogEntry($"no ServiceId found for activity entry", LogLevels.Debug); - } - var AssetCIName = getStringFromObject(entry["AssetCIName"]); - if (AssetCIName == string.Empty) - { - LogEntry($"no AssetCIName found for activity entry", LogLevels.Debug); - } - var Subject = getStringFromObject(entry["Subject"]); - if (Subject == string.Empty) - { - LogEntry($"no Subject found for activity entry", LogLevels.Warning); - continue; - } - - if (string.IsNullOrEmpty(subject)) - { - LogEntry($"No Subject found for activity entry", LogLevels.Warning); - continue; - } - - tickets.Add(new cF4SDTicketSummary() - { - TicketObjectId = activityEOId, - Name = ticketNumber, - ActivityType = activityType, - AssetCIName = assetCIName, - AssetName = assetName, - ServiceId = ServiceId, - ServiceName = ServiceName, - StatusId = state, - Status = stateDisp, - Summary = subject, - IsPrimaryAccount = true - }); - } - - tickets = tickets.OrderByDescending(x => x.Name).ToList(); - return tickets; - } - catch (Exception E) - { - LogException(E); - return null; - } - finally - { - LogMethodEnd(CM); - } - } - private static readonly string[] TicketOverviewKeys = new[] { "TicketsNew", @@ -393,62 +313,6 @@ namespace C4IT.F4SD "UnassignedTicketsCritical" }; - public class cF4SDTicket : cF4SDTicketSummary - { - public enum enumTicketCreationSource - { - Unknown = 0, - Mail = 1, - Phone = 2, - F4SD = 3 - } - - public class cTicketJournalItem - { - public DateTime CreationDate { get; set; } - public string Header { get; set; } - public string CreatedBy { get; set; } - public string DescriptionHtml { get; set; } - public string Description { get; set; } - public bool IsVisibleForUser { get; set; } - public Guid ActivityObjectId { get; set; } - public Guid JournalId { get; set; } - } - - public Guid AffectedUserId { get; set; } - public Guid AssetId { get; set; } - public DateTime CreationDate { get; set; } - public DateTime? ClosingDate { get; set; } - public int CreationSourceId { get; set; } - public string CreationSource { get; set; } - - public string Description { get; set; } - public string DescriptionHtml { get; set; } - public int PriorityId { get; set; } - public string Priority { get; set; } - public Guid CategoryId { get; set; } - public string Category { get; set; } - public string CategoryHierarchical { get; set; } - public string CIName { get; set; } - public string DirectLinkEdit { get; set; } - public Guid AssetCIId { get; set; } - public int AssetSKUAssetGroupId { get; set; } - public string AssetSKUAssetGroup { get; set; } - public int AssetSKUTypeId { get; set; } - public string AssetSKUType { get; set; } - public string DirectLinkPreview { get; set; } - public string DirectLinkClose { get; set; } - public string AffectedUser { get; set; } - public string SolutionHtml { get; set; } - public string Solution { get; set; } - public string AssetDomain { get; set; } - public string Urgency { get; set; } - public int UrgencyId { get; set; } - public string Impact { get; set; } - public int ImpactId { get; set; } - - } - public class TicketOverviewCountsResult { [JsonProperty("counts")] @@ -497,7 +361,6 @@ namespace C4IT.F4SD public string ActivityType { get; set; } } - [Obsolete] internal async Task getTicketOverviewCounts( string sid, string scope, @@ -555,64 +418,6 @@ namespace C4IT.F4SD } } - internal async Task getTicketOverviewCounts( - Guid userId, - string scope, - IEnumerable keys, - int queueoption, - List queues) - { - var CM = MethodBase.GetCurrentMethod(); - LogMethodBegin(CM); - try - { - var useRoleScope = string.Equals(scope, "role", StringComparison.OrdinalIgnoreCase); - var normalizedKeys = (keys ?? Array.Empty()) - .Where(k => !string.IsNullOrWhiteSpace(k)) - .Distinct(StringComparer.OrdinalIgnoreCase) - .ToList(); - - if (normalizedKeys.Count == 0) - { - normalizedKeys.AddRange(TicketOverviewKeys); - } - - var entries = await LoadTicketOverviewEntries(userId, useRoleScope, queueoption, queues); - List unassignedEntries = null; - if (!useRoleScope && normalizedKeys.Any(IsUnassignedOverviewKey)) - { - unassignedEntries = await LoadTicketOverviewUnassignedEntriesForPersonalScope(userId, queueoption, queues); - } - - var counts = new Dictionary(StringComparer.OrdinalIgnoreCase); - - foreach (var key in normalizedKeys) - { - if (!useRoleScope && IsUnassignedOverviewKey(key)) - { - counts[key] = (unassignedEntries ?? new List()) - .Count(entry => MatchesTicketOverviewKey(entry, key)); - } - else - { - counts[key] = entries.Count(entry => MatchesTicketOverviewKey(entry, key)); - } - } - - return new TicketOverviewCountsResult { Counts = counts }; - } - catch (Exception E) - { - LogException(E); - return new TicketOverviewCountsResult(); - } - finally - { - LogMethodEnd(CM); - } - } - - [Obsolete] internal async Task getTicketOverviewCountsByRoles( string sid, IEnumerable roleGuids, @@ -676,70 +481,6 @@ namespace C4IT.F4SD } } - internal async Task getTicketOverviewCountsByRoles( - Guid userId, - IEnumerable roleGuids, - IEnumerable keys, - int queueoption, - List queues) - { - var CM = MethodBase.GetCurrentMethod(); - LogMethodBegin(CM); - try - { - var normalizedKeys = (keys ?? Array.Empty()) - .Where(k => !string.IsNullOrWhiteSpace(k)) - .Distinct(StringComparer.OrdinalIgnoreCase) - .ToList(); - - if (normalizedKeys.Count == 0) - { - normalizedKeys.AddRange(TicketOverviewKeys); - } - - var roleIds = await ResolveTicketOverviewRoleIdsAsync(userId, roleGuids); - if (roleIds.Count == 0) - return new TicketOverviewCountsByRoleResult(); - - var entries = await LoadTicketOverviewEntriesByRoleIds(roleIds, queueoption, queues); - var entriesByRole = entries - .GroupBy(entry => entry.RecipientRoleId) - .ToDictionary(group => group.Key, group => group.ToList()); - - var countsByRole = new Dictionary>(StringComparer.OrdinalIgnoreCase); - foreach (var roleId in roleIds) - { - var roleEntries = entriesByRole.TryGetValue(roleId, out var list) - ? list - : new List(); - - var counts = new Dictionary(StringComparer.OrdinalIgnoreCase); - foreach (var key in normalizedKeys) - { - counts[key] = roleEntries.Count(entry => MatchesTicketOverviewKey(entry, key)); - } - - countsByRole[roleId.ToString()] = counts; - } - - return new TicketOverviewCountsByRoleResult - { - GeneratedAtUtc = DateTime.UtcNow, - CountsByRole = countsByRole - }; - } - catch (Exception E) - { - LogException(E); - return new TicketOverviewCountsByRoleResult(); - } - finally - { - LogMethodEnd(CM); - } - } - - [Obsolete] internal async Task> getTicketOverviewRelations( string sid, string scope, @@ -819,86 +560,6 @@ namespace C4IT.F4SD } } - internal async Task> getTicketOverviewRelations( - Guid userId, - string scope, - string key, - int count, - int queueoption, - List queues) - { - var CM = MethodBase.GetCurrentMethod(); - LogMethodBegin(CM); - try - { - if (string.IsNullOrWhiteSpace(key)) - return new List(); - - var useRoleScope = string.Equals(scope, "role", StringComparison.OrdinalIgnoreCase); - List entries; - if (!useRoleScope && IsUnassignedOverviewKey(key)) - { - entries = await LoadTicketOverviewUnassignedEntriesForPersonalScope(userId, queueoption, queues); - } - else - { - entries = await LoadTicketOverviewEntries(userId, useRoleScope, queueoption, queues); - } - - var filtered = entries - .Where(entry => MatchesTicketOverviewKey(entry, key)) - .OrderByDescending(entry => entry.CreatedDate) - .ToList(); - - if (count > 0) - filtered = filtered.Take(count).ToList(); - - var relations = new List(filtered.Count); - foreach (var entry in filtered) - { - var relation = new TicketOverviewRelationDto - { - Type = enumF4sdSearchResultClass.Ticket, - DisplayName = entry.TicketNumber ?? string.Empty, - Name = entry.TicketNumber ?? string.Empty, - id = entry.TicketId, - Status = enumF4sdSearchResultStatus.Active, - Infos = new Dictionary - { - ["Summary"] = entry.Summary ?? string.Empty, - ["StatusId"] = ConvertM42State(entry.State), - ["ActivityType"] = entry.ActivityType ?? string.Empty, - ["UserDisplayName"] = entry.InitiatorDisplayName ?? string.Empty, - ["UserAccount"] = entry.InitiatorAccount ?? string.Empty, - ["UserDomain"] = entry.InitiatorDomain ?? string.Empty, - ["UserSid"] = entry.InitiatorSid ?? string.Empty, - ["Sids"] = entry.InitiatorSid ?? string.Empty, - ["UserId"] = entry.InitiatorId != Guid.Empty ? entry.InitiatorId.ToString() : string.Empty, - ["UserGuid"] = entry.InitiatorId != Guid.Empty ? entry.InitiatorId.ToString() : string.Empty - }, - Identities = new List - { - new cF4sdIdentityEntry { Class = enumFasdInformationClass.Ticket, Id = entry.TicketId }, - new cF4sdIdentityEntry { Class = enumFasdInformationClass.User, Id = entry.InitiatorId } - } - }; - relations.Add(relation); - } - - return relations; - } - catch (Exception E) - { - LogException(E); - return new List(); - } - finally - { - LogMethodEnd(CM); - } - } - - [Obsolete] private async Task> LoadTicketOverviewEntries( string sid, bool useRoleScope, @@ -928,34 +589,6 @@ namespace C4IT.F4SD } } - private async Task> LoadTicketOverviewEntries( - Guid userId, - bool useRoleScope, - int queueoption, - List queues) - { - var CM = MethodBase.GetCurrentMethod(); - LogMethodBegin(CM); - try - { - if (userId == Guid.Empty) - return new List(); - - var filter = await BuildTicketOverviewFilterAsync(userId, useRoleScope, queueoption, queues); - return await LoadTicketOverviewEntriesByFilter(filter); - } - catch (Exception E) - { - LogException(E); - return new List(); - } - finally - { - LogMethodEnd(CM); - } - } - - [Obsolete] private async Task> LoadTicketOverviewUnassignedEntriesForPersonalScope( string sid, int queueoption, @@ -989,39 +622,6 @@ namespace C4IT.F4SD } } - private async Task> LoadTicketOverviewUnassignedEntriesForPersonalScope( - Guid userId, - int queueoption, - List queues) - { - var CM = MethodBase.GetCurrentMethod(); - LogMethodBegin(CM); - try - { - await Task.Delay(0); - - if (userId == Guid.Empty) - return new List(); - - var roleIds = await ResolveTicketOverviewRoleIdsAsync(userId, null); - var filter = BuildTicketOverviewFilterForRoleIds(roleIds, null, queueoption, queues); - if (string.IsNullOrWhiteSpace(filter)) - return new List(); - - filter += " AND Recipient IS NULL"; - return await LoadTicketOverviewEntriesByFilter(filter); - } - catch (Exception E) - { - LogException(E); - return new List(); - } - finally - { - LogMethodEnd(CM); - } - } - private async Task> LoadTicketOverviewEntriesByRoleIds( IEnumerable roleIds, int queueoption, @@ -1172,7 +772,6 @@ namespace C4IT.F4SD return null; } - [Obsolete] private async Task BuildTicketOverviewFilterAsync( string sid, bool useRoleScope, @@ -1192,25 +791,6 @@ namespace C4IT.F4SD return BuildTicketOverviewFilterForRoleIds(roleIds, filter); } - private async Task BuildTicketOverviewFilterAsync( - Guid userId, - bool useRoleScope, - int queueoption, - List queues) - { - var filter = BuildTicketOverviewBaseFilter(queueoption, queues); - - if (!useRoleScope) - { - var recipientFilter = $"Recipient = '{Escape(userId.ToString("D"))}'"; - filter += $" AND ({recipientFilter})"; - return filter; - } - - var roleIds = await ResolveTicketOverviewRoleIdsAsync(userId, null); - return BuildTicketOverviewFilterForRoleIds(roleIds, filter); - } - private string BuildTicketOverviewBaseFilter(int queueoption, List queues) { var filter = "T(SPSCommonClassBase).State <> 204"; @@ -1309,7 +889,6 @@ namespace C4IT.F4SD return filter; } - [Obsolete] private async Task> ResolveTicketOverviewRoleIdsAsync(string sid, IEnumerable roleGuids) { var roleIds = (roleGuids ?? Enumerable.Empty()) @@ -1335,26 +914,6 @@ namespace C4IT.F4SD .ToList(); } - private async Task> ResolveTicketOverviewRoleIdsAsync(Guid userId, IEnumerable roleGuids) - { - var roleIds = (roleGuids ?? Enumerable.Empty()) - .Where(id => id != Guid.Empty) - .Distinct() - .ToList(); - - if (roleIds.Count > 0) - return roleIds; - - if (userId == Guid.Empty) - return new List(); - - var roles = await getRoleMembershipById(userId) ?? new List(); - return roles - .Where(role => role != null && role.Id != Guid.Empty) - .Select(role => role.Id) - .Distinct() - .ToList(); - } private static bool IsUnassignedOverviewKey(string key) { if (string.IsNullOrWhiteSpace(key)) @@ -1462,8 +1021,6 @@ namespace C4IT.F4SD { return input?.Replace("'", "''"); } - - [Obsolete] private string GetActivityFilter( string userSid, int hours, @@ -1551,92 +1108,6 @@ namespace C4IT.F4SD return filter; } - private string GetActivityFilter( - Guid userId, - int hours, - bool ticketAndServiceRequestEnabled, - int queueoption, - List queues - ) - { - // Baseline-Filter auf User und Datum - var filter = $"Initiator = '{Escape(userId.ToString("D"))}'"; - - string fStartDate = AsqlHelper.PrepareDateTime(DateTime.UtcNow.AddHours(-hours), true); - string fEndDate = AsqlHelper.PrepareDateTime(DateTime.UtcNow, true); - - // Incident vs. Ticket/ServiceRequest - if (ticketAndServiceRequestEnabled) - { - filter += - " AND (UsedInTypeSPSActivityTypeIncident IS NOT NULL" + - " OR UsedInTypeSPSActivityTypeTicket IS NOT NULL" + - " OR UsedInTypeSPSActivityTypeServiceRequest IS NOT NULL)"; - } - else - { - filter += " AND (UsedInTypeSPSActivityTypeIncident IS NOT NULL)"; - } - - // Offene bzw. kürzlich geschlossene Objekte - filter += - " AND (T(SPSCommonClassBase).State <> 204" + - $" OR (ClosedDate > {fStartDate} AND ClosedDate < {fEndDate}))"; - - // Queue-Filter nur, wenn tatsächlich Queues übergeben wurden - if (queues != null && queues.Count > 0) - { - // URL-escaping für Name und ID - var escapedNames = queues - .Select(q => $"'{Escape(q.QueueName)}'") - .ToList(); - var escapedIds = queues - .Select(q => $"'{Escape(q.QueueID.ToString())}'") - .ToList(); - - string nameList = string.Join(", ", escapedNames); - string idList = string.Join(", ", escapedIds); - - switch (queueoption) - { - // 1 = entweder keine Queue oder eine der übergebenen Queues (Name oder ID) - case 1: - filter += - $" AND (" + - "Queue IS NULL" + - $" OR Queue.Name IN ({nameList})" + - $" OR Queue.ID IN ({idList})" + - ")"; - break; - - // 2 = nur die übergebenen Queues (Name oder ID) - case 2: - filter += - $" AND (" + - "Queue IS NOT NULL" + - $" AND (Queue.Name IN ({nameList})" + - $" OR Queue.ID IN ({idList}))" + - ")"; - break; - - // 3 = nur Objekte ohne Queue - case 3: - filter += " AND Queue IS NULL"; - break; - - // 0 oder andere = keine zusätzliche Einschränkung - default: - break; - } - } - else if (queueoption == 3) - { - // Ausnahme: wenn keine Queues übergeben, aber Option 3 = nur ohne Queue - filter += " AND Queue IS NULL"; - } - - return filter; - } internal async Task getDirectLinkCreateTicket(string sid, string assetname) { @@ -1929,21 +1400,21 @@ namespace C4IT.F4SD } } - internal async Task> GetJournalEntries(Guid activityEOID) + internal async Task> GetJournalEntries(Guid activityEOID) { var CM = MethodBase.GetCurrentMethod(); LogMethodBegin(CM); try { await Task.Delay(0); - List journalEntries = new List(); + List journalEntries = new List(); var entries = F4SDM42WebApiController.defaultInstance.GetJournalService().GetJournalList(activityEOID, false, 0, 50, "{2}", 120); LogEntry($"{entries.Length} Journal entries found for ObjectID={activityEOID}", LogLevels.Debug); for (int i = 0; i < entries.Length; i++) { Matrix42.Contracts.Platform.Data.JournalEntryInfo item = entries[i]; LogEntry($"Journal entry {i + 1}/{entries.Length}: ID={item.Id}, CreatedDate={item.CreatedDate}, CreatedBy={item.Creator}, Header={item.Header}", LogLevels.Debug); - journalEntries.Add(new cF4SDTicket.cTicketJournalItem() + journalEntries.Add(new cTicketJournalItem() { JournalId = item.Id, ActivityObjectId = activityEOID, @@ -2311,7 +1782,7 @@ namespace C4IT.F4SD } zipArchive.Dispose(); memoryStream.Position = 0L; - HttpResponseMessage httpResponseMessage = HttpResponseExtensions.CreateResponse(request, HttpStatusCode.OK); + HttpResponseMessage httpResponseMessage = request.CreateResponse(HttpStatusCode.OK); httpResponseMessage.Content = new StreamContent(memoryStream); httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { @@ -2500,7 +1971,7 @@ namespace C4IT.F4SD try { await Task.Delay(0); - var asqlFilter = string.Format("Members.Id = '{0}'", UserId.ToString("D")); + var asqlFilter = string.Format("Members.Id = '{0}'", UserId.ToString()); LogEntry($"ASql Filter: {asqlFilter}"); FragmentRequestBase fragmentRequestBase = new FragmentRequestBase(SPSSecurityClassRole, ColumnSelectOption.List, "Id as Id" + @@ -2721,41 +2192,4 @@ namespace C4IT.F4SD Roles = new List(); } } - - internal sealed class UrlQueryBuilder - { - private readonly List> _values = new List>(); - - private UrlQueryBuilder() - { - } - - public static UrlQueryBuilder Parse(string query) - { - var builder = new UrlQueryBuilder(); - var normalizedQuery = (query ?? string.Empty).TrimStart('?'); - - foreach (var pair in normalizedQuery.Split(new[] { '&' }, StringSplitOptions.RemoveEmptyEntries)) - { - var separatorIndex = pair.IndexOf('='); - var key = separatorIndex >= 0 ? pair.Substring(0, separatorIndex) : pair; - var value = separatorIndex >= 0 ? pair.Substring(separatorIndex + 1) : string.Empty; - - builder.Add(WebUtility.UrlDecode(key), WebUtility.UrlDecode(value)); - } - - return builder; - } - - public void Add(string key, string value) - { - if (!string.IsNullOrEmpty(key)) - _values.Add(new KeyValuePair(key, value ?? string.Empty)); - } - - public override string ToString() - { - return string.Join("&", _values.Select(pair => $"{WebUtility.UrlEncode(pair.Key)}={WebUtility.UrlEncode(pair.Value)}")); - } - } } diff --git a/F4SDM42WebApi/F4SDM42WebApiController.cs b/F4SDM42WebApi/F4SDM42WebApiController.cs index f810c49..f431431 100644 --- a/F4SDM42WebApi/F4SDM42WebApiController.cs +++ b/F4SDM42WebApi/F4SDM42WebApiController.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Data; using System.Linq; @@ -23,6 +23,7 @@ using C4IT.F4SDM; using C4IT.FASD.Base; using C4IT.Logging; +using static C4IT.FASD.Base.cF4SDTicket; using static C4IT.Logging.cLogManager; namespace C4IT.F4SD @@ -31,7 +32,6 @@ namespace C4IT.F4SD public partial class F4SDM42WebApiController : ApiController { public static bool IsInitialized { get; private set; } = false; - public static F4SDM42WebApiController defaultInstance; //private readonly IIncidentService _incidentService; internal readonly GlobalConfigurationProvider _globalConfigurationProvider; @@ -48,6 +48,10 @@ namespace C4IT.F4SD { _resolver = resolver ?? throw new ArgumentNullException(nameof(resolver)); defaultInstance = this; + //_objectService = objectService; + //_fragmentService = fragmentService; + //_incidentService = Guard.NullArgument(incidentService, "incidentService"); + _globalConfigurationProvider = GlobalConfigurationProvider.Instance; _f4stHelperService = new F4SDHelperService(); EnsureInitialized(); @@ -58,6 +62,7 @@ namespace C4IT.F4SD { try { + //System.Diagnostics.Debugger.Launch(); lock (initLock) { if (IsInitialized || F4SDM42LogsWebApiController.IsInitialized) @@ -99,14 +104,12 @@ namespace C4IT.F4SD await Task.Delay(0); return await _f4stHelperService.getDirectLinkCreateTicket(sid, assetname); } - [Route("getDirectLinkF4SD"), HttpGet] public async Task getDirectLinkF4SD(Guid EOID, string Type) { return await _f4stHelperService.getDirectLinkF4SD(EOID, Type); } - [Obsolete] [Route("getTicketList"), HttpGet] public async Task> getTicketList( string sid, @@ -126,28 +129,9 @@ namespace C4IT.F4SD ); } - [Route("getTicketListForUser"), HttpGet] - public async Task> getTicketListForUser( - Guid userId, - int hours, - int queueoption = 0, - string queues = "" - ) - { - var decodedPairs = ParseQueues(queues); - - // Nun weiterreichen an Service - return await _f4stHelperService.getTicketListByUser( - userId, - hours, - queueoption, - decodedPairs - ); - } - [Route("getTicketDetails"), HttpGet] - public async Task getTicketDetails(Guid objectId) + public async Task getTicketDetails(Guid objectId) { var tickets = await _f4stHelperService.getTicketDetails(new List() { objectId }); if (tickets.Count > 0) @@ -157,12 +141,11 @@ namespace C4IT.F4SD } [Route("getTicketHistory"), HttpGet] - public async Task> getTicketHistory(Guid objectId) + public async Task> getTicketHistory(Guid objectId) { return await _f4stHelperService.GetJournalEntries(objectId); } - [Obsolete] [Route("getTicketOverviewCounts"), HttpGet] public async Task getTicketOverviewCounts( string sid, @@ -182,27 +165,6 @@ namespace C4IT.F4SD return await _f4stHelperService.getTicketOverviewCounts(sid, scope, parsedKeys, queueoption, decodedQueues); } - - [Route("getTicketOverviewCountsForUser"), HttpGet] - public async Task getTicketOverviewCountsForUser( - Guid userId, - string scope = "personal", - string keys = "", - int queueoption = 0, - string queues = "" - ) - { - var parsedKeys = (keys ?? string.Empty) - .Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries) - .Select(key => key.Trim()) - .Where(key => !string.IsNullOrWhiteSpace(key)) - .ToList(); - - var decodedQueues = ParseQueues(queues); - return await _f4stHelperService.getTicketOverviewCounts(userId, scope, parsedKeys, queueoption, decodedQueues); - } - - [Obsolete] [Route("getTicketOverviewCountsByRoles"), HttpPost] public async Task getTicketOverviewCountsByRoles([FromBody] TicketOverviewCountsByRolesRequest request) { @@ -227,31 +189,6 @@ namespace C4IT.F4SD ); } - [Route("getTicketOverviewCountsByRolesForUser"), HttpPost] - public async Task getTicketOverviewCountsByRolesForUser([FromBody] TicketOverviewCountsByRolesForUserRequest request) - { - var parsedKeys = (request?.Keys ?? new List()) - .Where(key => !string.IsNullOrWhiteSpace(key)) - .Select(key => key.Trim()) - .Distinct(StringComparer.OrdinalIgnoreCase) - .ToList(); - - var roleGuids = (request?.RoleGuids ?? new List()) - .Where(roleId => roleId != Guid.Empty) - .Distinct() - .ToList(); - - var decodedQueues = ParseQueues(request?.Queues ?? string.Empty); - return await _f4stHelperService.getTicketOverviewCountsByRoles( - request.userId, - roleGuids, - parsedKeys, - request?.QueueOption ?? 0, - decodedQueues - ); - } - - [Obsolete] [Route("getTicketOverviewRelations"), HttpGet] public async Task> getTicketOverviewRelations( string sid, @@ -265,22 +202,6 @@ namespace C4IT.F4SD var decodedQueues = ParseQueues(queues); return await _f4stHelperService.getTicketOverviewRelations(sid, scope, key, count, queueoption, decodedQueues); } - - [Route("getTicketOverviewRelationsForUser"), HttpGet] - public async Task> getTicketOverviewRelations( - Guid userId, - string scope = "personal", - string key = "", - int count = 0, - int queueoption = 0, - string queues = "" - ) - { - var decodedQueues = ParseQueues(queues); - return await _f4stHelperService.getTicketOverviewRelations(userId, scope, key, count, queueoption, decodedQueues); - } - - /* [Route("updateActivitySolution/{objectId}"), HttpPost] public async Task updateActivitySolution(Guid objectId, [FromBody] string SolutionHtml) @@ -293,14 +214,10 @@ namespace C4IT.F4SD */ [Route("getPickup/{name}"), HttpGet] //[CacheOutput(UseETAG = true)] - public async Task getPickup(string name, [FromUri] EntityEnumerationVisibilityMode mode = EntityEnumerationVisibilityMode.None, [FromUri] Int32 group = -1) + public async Task getPickup(string name, [FromUri] EntityEnumerationVisibilityMode mode = EntityEnumerationVisibilityMode.None, [FromUri] Int32 group = -1) { await Task.Delay(0); - var pickupName = ResolvePickupName(name); - if (string.IsNullOrWhiteSpace(pickupName)) - throw new ArgumentException("Pickup name is required.", nameof(name)); - - EntityEnumeration enumerationTemp = GetEnumeration(pickupName, mode); + EntityEnumeration enumerationTemp = GetEnumeration(name, mode); var vals = enumerationTemp.Values; if (group > -1) @@ -320,22 +237,22 @@ namespace C4IT.F4SD Values = vals.ToArray() }; //CacheOutputAttribute.RegisterResponseEtag($"enum_{enumeration.Name}_{(int)mode}", $"{enumeration.Name}_{(int)mode}", cultureInvariant: false, userInvariant: true, val); - return enumeration; + return Request.CreateResponse(HttpStatusCode.OK, enumeration); } [Route("getMyRoleMemberships"), HttpGet] - public async Task getMyRoleMemberships() + public async Task getMyRoleMemberships() { var userId = GetCurrentUserId(); if (userId == Guid.Empty) throw new UnauthorizedAccessException("Cannot determine the interactive Matrix42 user."); var filter = AsqlHelper.BuildInCondition("ID", new Guid[] { userId }); - return await _f4stHelperService.UserPermissionsInfo(filter); + return Request.CreateResponse(HttpStatusCode.OK, await _f4stHelperService.UserPermissionsInfo(filter)); } - - [Route("getRoleMemberships"), HttpGet] - public async Task getRoleMemberships([FromUri] GetRoleMembershipsRequest req) + [HttpGet] + [Route("getRoleMemberships")] + public async Task getRoleMemberships([FromUri] GetRoleMembershipsRequest req) { var filter = ""; @@ -354,7 +271,7 @@ namespace C4IT.F4SD if (!string.IsNullOrEmpty(filter)) { - return await _f4stHelperService.UserPermissionsInfo(filter); + return Request.CreateResponse(HttpStatusCode.OK, await _f4stHelperService.UserPermissionsInfo(filter)); } else { @@ -362,14 +279,6 @@ namespace C4IT.F4SD } } - [Route("getRoleMemberships/{userId}"), HttpGet] - public async Task getRoleMembershipForUser(Guid userId) - { - var filter = AsqlHelper.BuildInCondition("ID", new Guid[] { userId }); - return await _f4stHelperService.UserPermissionsInfo(filter); - } - - [Obsolete] public class GetRoleMembershipsRequest { public Guid? Id { get; set; } @@ -378,7 +287,6 @@ namespace C4IT.F4SD public GetRoleMembershipsRequest() { } } - [Obsolete] public class TicketOverviewCountsByRolesRequest { public string Sid { get; set; } @@ -388,15 +296,6 @@ namespace C4IT.F4SD public string Queues { get; set; } } - public class TicketOverviewCountsByRolesForUserRequest - { - public Guid userId { get; set; } - public List RoleGuids { get; set; } = new List(); - public List Keys { get; set; } = new List(); - public int? QueueOption { get; set; } - public string Queues { get; set; } - } - private EntityEnumeration GetEnumeration(string name, EntityEnumerationVisibilityMode mode) { name = name?.Trim(); @@ -432,20 +331,6 @@ namespace C4IT.F4SD }; } - private string ResolvePickupName(string routeName) - { - if (!string.IsNullOrWhiteSpace(routeName)) - return routeName.Trim(); - - var pathSegment = Request?.RequestUri?.Segments? - .LastOrDefault(segment => !string.IsNullOrWhiteSpace(segment) && segment != "/"); - - if (string.IsNullOrWhiteSpace(pathSegment)) - return null; - - return Uri.UnescapeDataString(pathSegment.Trim('/')); - } - private static bool? GetVisibilityFilter(EntityEnumerationVisibilityMode mode) { switch (mode) @@ -506,46 +391,25 @@ namespace C4IT.F4SD .Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries) .Select(part => { - var decodedPart = WebUtility.UrlDecode(part)?.Trim(); - if (string.IsNullOrWhiteSpace(decodedPart)) + var segments = part.Split(':'); + if (segments.Length != 2) return null; - var name = decodedPart; - string idStr = null; + var name = WebUtility.UrlDecode(segments[0]); + var idStr = WebUtility.UrlDecode(segments[1]); - var separatorIndex = decodedPart.IndexOf(':'); - if (separatorIndex >= 0) - { - name = decodedPart.Substring(0, separatorIndex).Trim(); - idStr = decodedPart.Substring(separatorIndex + 1).Trim(); - } - - var queue = new cApiM42TicketQueueInfo(); - if (!string.IsNullOrWhiteSpace(name)) - queue.QueueName = name; - - if (!string.IsNullOrWhiteSpace(idStr)) - { - if (!Guid.TryParse(idStr, out var guid)) - return null; - - queue.QueueID = guid; - queue.QueueName = null; - } - - if (queue.QueueID == Guid.Empty && string.IsNullOrWhiteSpace(queue.QueueName)) - return null; - - return queue; + return Guid.TryParse(idStr, out var guid) + ? new cApiM42TicketQueueInfo { QueueName = name, QueueID = guid } + : null; }) .Where(q => q != null) .ToList(); } [Route("isAlive"), HttpGet] - public IHttpActionResult isAlive() + public HttpResponseMessage isAlive() { - return new StatusCodeResult(HttpStatusCode.NoContent); + return new HttpResponseMessage(HttpStatusCode.NoContent); } [Route("loglevel"), HttpGet] @@ -584,7 +448,6 @@ namespace C4IT.F4SD } } } - [RoutePrefix("api/C4ITF4SDWebApi/Logs")] public partial class F4SDM42LogsWebApiController : ApiController { @@ -613,6 +476,8 @@ namespace C4IT.F4SD catch { }; } + + public F4SDM42LogsWebApiController() { _f4stHelperService = new F4SDHelperService(); @@ -660,12 +525,6 @@ namespace C4IT.F4SD (entry.Theme?.IndexOf(filter, StringComparison.OrdinalIgnoreCase) ?? -1) >= 0); } - if (queryOptions?.Skip != null) - result = result.Skip(queryOptions.Skip.Value); - - if (queryOptions?.Top != null) - result = result.Take(queryOptions.Top.Value); - return result; } }