preliminary implementation of preview widgets and related tasks in the .NET OMS

This commit is contained in:
Michael Becker 2024-12-21 00:23:54 -05:00
parent 7beeffd9ad
commit d4b41e14ff
3 changed files with 140 additions and 1 deletions

View File

@ -23,7 +23,7 @@ namespace Mocha.Core.UI.Server.Commands;
public class InstanceDetailsCommand : InstanceCommand
{
public override IEnumerable<string> UriPatterns => new string[] { "/tenants/{tenantName}/instances/{instanceKey}" };
public override IEnumerable<string> UriPatterns => new string[] { "/tenants/{tenantName}/instances/{iid}" };
protected override void ProcessInternal(WebServerProcessRequestEventArgs e, Core.Oms oms, Core.OmsContext ctx, StreamWriter sw)
{
JsonObject obj = JsonRenderer.InstanceToJson(oms, ProcessingInstance);

View File

@ -0,0 +1,135 @@
// Copyright (C) 2024 Michael Becker <alcexhim@gmail.com>
//
// This file is part of Mocha.NET.
//
// Mocha.NET is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Mocha.NET is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Mocha.NET. If not, see <https://www.gnu.org/licenses/>.
using System.Text.Json.Nodes;
using MBS.Web;
using Mocha.Core;
namespace Mocha.Core.UI.Server.Commands;
public class InstancePreviewCommand : InstanceCommand
{
public override IEnumerable<string> UriPatterns => new string[] { "/tenants/{tenantName}/instances/{iid}/preview" };
private struct RelatedTask
{
public InstanceKey InstanceKey;
public string Label;
}
protected override void ProcessInternal(WebServerProcessRequestEventArgs e, Core.Oms oms, Core.OmsContext ctx, StreamWriter sw)
{
JsonObject obj = new JsonObject();
obj.Add("result", "success");
JsonObject objPreview = new JsonObject();
objPreview.Add("widget", "root");
obj.Add("preview", objPreview);
JsonObject objRelatedTasks = new JsonObject();
objRelatedTasks.Add("widget", "relatedTasks");
Dictionary<string, List<RelatedTask>> relatedTasks = new Dictionary<string, List<RelatedTask>>();
InstanceHandle parentClass = oms.GetParentClass(ProcessingInstance);
IEnumerable<InstanceHandle> relatedTasksIH = oms.GetRelatedInstances(parentClass, oms.GetInstance(KnownRelationshipGuids.Class__has_related__Task));
foreach (InstanceHandle ih in relatedTasksIH)
{
InstanceHandle taskCategory = oms.GetRelatedInstance(ih, oms.GetInstance(KnownRelationshipGuids.Task__has__Task_Category));
string categoryTitle = oms.GetInstanceText(taskCategory);
if (!relatedTasks.ContainsKey(categoryTitle))
{
relatedTasks[categoryTitle] = new List<RelatedTask>();
}
relatedTasks[categoryTitle].Add(new RelatedTask() { InstanceKey = oms.GetInstanceKey(ih), Label = oms.GetInstanceText(ih) });
}
/*
relatedTasks["Preferences"] = new List<RelatedTask>();
relatedTasks["Preferences"].Add(new RelatedTask() { InstanceKey = new InstanceKey(1924, 264), Label = "Export Preferences" });
relatedTasks["System Account"] = new List<RelatedTask>();
*/
InstanceKey ik = oms.GetInstanceKey(ProcessingInstance);
JsonArray aryTaskGroups = new JsonArray();
foreach (KeyValuePair<string, List<RelatedTask>> kvp in relatedTasks)
{
JsonObject objTaskGroup = new JsonObject();
objTaskGroup.Add("widget", "relatedTaskGroup");
objTaskGroup.Add("renderDifferently", false);
JsonArray aryTaskGroups1 = new JsonArray();
{
JsonObject objTaskGroup1 = new JsonObject();
objTaskGroup1.Add("widget", "relatedTaskGroup");
objTaskGroup1.Add("label", kvp.Key);
objTaskGroup1.Add("renderDifferently", false);
JsonArray aryTaskGroups2 = new JsonArray();
foreach (RelatedTask task in kvp.Value)
{
JsonObject objTaskGroup2 = new JsonObject();
objTaskGroup2.Add("widget", "relatedTaskGroup");
JsonArray aryTasks = new JsonArray();
JsonObject objTask = new JsonObject();
objTask.Add("widget", "relatedTask");
objTask.Add("uri", String.Format("/{0}/inst/{1}/rel-task/{2}", "super", ik, task.InstanceKey));
objTask.Add("view", true);
objTask.Add("webService", false);
objTask.Add("iid", task.InstanceKey.ToString());
objTask.Add("label", task.Label);
aryTasks.Add(objTask);
objTaskGroup2.Add("tasks", aryTasks);
objTaskGroup2.Add("renderDifferently", false);
aryTaskGroups2.Add(objTaskGroup2);
}
objTaskGroup1.Add("taskGroups", aryTaskGroups2);
aryTaskGroups1.Add(objTaskGroup1);
}
objTaskGroup.Add("taskGroups", aryTaskGroups1);
aryTaskGroups.Add(objTaskGroup);
}
objRelatedTasks.Add("taskGroups", aryTaskGroups);
JsonObject objInstance = new JsonObject();
objInstance.Add("instanceId", ik.ToString());
objInstance.Add("label", oms.GetInstanceText(parentClass));
objRelatedTasks.Add("instance", objInstance);
obj.Add("relatedTasks", objRelatedTasks);
e.Context.Response.ResponseCode = 200;
e.Context.Response.ResponseText = "OK";
e.Context.Response.ContentType = "application/json";
sw.Write(obj.ToJsonString());
}
}

View File

@ -170,6 +170,10 @@ public class OmsServer
private IEnumerable<OmsServerCommandEntry> Flatten(IEnumerable<OmsServerCommand> cmds)
{
// !FIXME! this returns weird results if the length of the one string is longer
// ! ! INCLUDING length of {variable} names, when these should be replaced
// ! ! by a single char '?' (like in Phast) to avoid this issue
List<OmsServerCommandEntry> list = new List<OmsServerCommandEntry>();
foreach (OmsServerCommand cmd in cmds)
{