using System;
using System.Collections.Generic;
using System.Web;
using Newtonsoft.Json;
namespace C4IT.API
{
public enum ViewType
{
New, // open create dialog
Preview, // open preview
Action, // run action/wizard
Edit // open edit dialog
}
public class DeepLinkBuilder
{
public string Host { get; set; }
public string AppName { get; set; }
// Parameter für view-options
public bool? Embedded { get; set; }
public Guid? DialogId { get; set; }
public Guid? ObjectId { get; set; }
public int? Archived { get; set; }
public string Type { get; set; }
public ViewType? ViewType { get; set; }
public Guid? ActionId { get; set; }
public Guid? ViewId { get; set; }
public object PresetParams { get; set; }
///
/// Baut die URL basierend auf den gesetzten Eigenschaften.
///
/// Die erstellte DeepLink-URL als string.
public string BuildUrl()
{
var viewOptions = new Dictionary();
if (Embedded.HasValue)
viewOptions["embedded"] = Embedded.Value;
if (DialogId.HasValue)
viewOptions["dialogId"] = DialogId.Value;
if (ObjectId.HasValue)
viewOptions["objectId"] = ObjectId.Value;
if (Archived.HasValue)
viewOptions["archived"] = Archived.Value;
if (!string.IsNullOrEmpty(Type))
viewOptions["type"] = Type;
if (ViewType.HasValue)
viewOptions["viewType"] = ViewType.Value.ToString().ToLower();
if (ActionId.HasValue)
viewOptions["actionId"] = ActionId.Value;
if (ViewId.HasValue)
viewOptions["viewId"] = ViewId.Value;
string viewOptionsJson = JsonConvert.SerializeObject(viewOptions);
string encodedViewOptions = HttpUtility.UrlEncode(viewOptionsJson);
string url = $"{Host.TrimEnd('/')}/wm/app-{AppName}/?view-options={encodedViewOptions}";
if (PresetParams != null)
{
string presetParamsJson = JsonConvert.SerializeObject(PresetParams);
string encodedPresetParams = HttpUtility.UrlEncode(presetParamsJson);
url += $"&presetParams={encodedPresetParams}";
}
return url;
}
///
/// Statische Methode zur Erstellung eines DeepLinks basierend auf den angegebenen Parametern.
///
/// Der Hostname der Anwendung.
/// Der Name der Anwendung.
/// Der Typ der Ansicht.
/// Der Type des Objekts.
/// Die erstellte DeepLink-URL als string.
public static string CreateDeepLink(string host, string appName, ViewType viewType, string type)
{
var builder = new DeepLinkBuilder
{
Host = host,
AppName = appName,
ViewType = viewType,
Type = type,
};
return builder.BuildUrl();
}
}
}